#! /usr/bin/perl
# Copyright (C) 2010-2016 by Dirk Krause
# All rights reserved.
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided
# that the following conditions are met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
# * Redistributions in binary form must reproduce the above
# opyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of the Dirk Krause nor the names of
# contributors may be used to endorse or promote
# products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
# Hash table containing dirname=1 for directory
# names to ignore (to avoid conflicts).
#
my %dirs_to_ignore;
$dirs_to_ignore{"/usr"} = 1;
$dirs_to_ignore{"/usr/share"} = 1;
$dirs_to_ignore{"/usr/share/man"} = 1;
$dirs_to_ignore{"/usr/share/man/man1"} = 1;
$dirs_to_ignore{"/usr/share/man/man2"} = 1;
$dirs_to_ignore{"/usr/share/man/man3"} = 1;
$dirs_to_ignore{"/usr/share/man/man4"} = 1;
$dirs_to_ignore{"/usr/share/man/man5"} = 1;
$dirs_to_ignore{"/usr/share/man/man6"} = 1;
$dirs_to_ignore{"/usr/share/man/man7"} = 1;
$dirs_to_ignore{"/usr/share/applications"} = 1;
$dirs_to_ignore{"/usr/include"} = 1;
$dirs_to_ignore{"/usr/bin"} = 1;
$dirs_to_ignore{"/usr/sbin"} = 1;
$dirs_to_ignore{"/usr/lib"} = 1;
$dirs_to_ignore{"/usr/lib/pkgconfig"} = 1;
$dirs_to_ignore{"/usr/libexec"} = 1;
$dirs_to_ignore{"/usr/libexec/filters"} = 1;
$dirs_to_ignore{"/etc"} = 1;
$dirs_to_ignore{"/var"} = 1;
$dirs_to_ignore{"/var/run"} = 1;
$dirs_to_ignore{"/var/spool"} = 1;
$dirs_to_ignore{"/var/log"} = 1;
$dirs_to_ignore{"/var/tmp"} = 1;
$dirs_to_ignore{"/usr/local"} = 1;
$dirs_to_ignore{"/usr/local/share"} = 1;
$dirs_to_ignore{"/usr/local/share/man"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man1"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man2"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man3"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man4"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man5"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man6"} = 1;
$dirs_to_ignore{"/usr/local/share/man/man7"} = 1;
$dirs_to_ignore{"/usr/local/share/applications"} = 1;
$dirs_to_ignore{"/usr/local/include"} = 1;
$dirs_to_ignore{"/usr/local/bin"} = 1;
$dirs_to_ignore{"/usr/local/sbin"} = 1;
$dirs_to_ignore{"/usr/local/lib"} = 1;
$dirs_to_ignore{"/usr/local/lib/pkgconfig"} = 1;
$dirs_to_ignore{"/usr/local/libexec"} = 1;
$dirs_to_ignore{"/usr/local/libexec/filters"} = 1;
$dirs_to_ignore{"/usr/local/etc"} = 1;
$dirs_to_ignore{"/usr/local/var"} = 1;
$dirs_to_ignore{"/usr/local/var/run"} = 1;
$dirs_to_ignore{"/usr/local/var/spool"} = 1;
$dirs_to_ignore{"/usr/local/var/log"} = 1;
$dirs_to_ignore{"/usr/local/var/tmp"} = 1;
sub getpwd() {
my $back = undef;
my $line;
if(open(INPUT, "pwd |")) {
while() {
$line = $_; chomp $line;
if($line =~ /\S+/o) {
$back = $line;
}
}
close(INPUT);
}
return $back;
}
my $cwd = getpwd();
my @files = (
"compver", "depend", "space", "copyright", "request",
"checkinstall", "preinstall", "postinstall",
"preremove", "postremove", "pkginfo"
);
if(open(OUTPUT, ">prototype")) {
if( -f "pkginfo") {
system("chmod 644 pkginfo");
print OUTPUT "i pkginfo=$cwd/pkginfo\n";
foreach my $i (@files) {
if( -f "$i" ) {
if ( "$i" ne "pkginfo" ) {
system("chmod 644 $i");
print OUTPUT "i $i=$cwd/$i\n";
}
}
}
if(open(INPUT, "find . -type d -print |")) {
my $line;
while() {
$line = $_; chomp $line;
if("$line" ne ".") {
if($line =~ /^\.(.*)$/o) {
$line = $1;
}
if ( ! ( exists( $dirs_to_ignore{"$line"} ) ) ) {
print OUTPUT "d none $line 0755 bin bin\n";
}
}
}
close(INPUT);
}
if(open(INPUT, "find . -type f |")) {
my $mode = "0644";
my $line;
my $must_use_this;
while() {
$line = $_; chomp $line;
$must_use_this = 1;
$mode = "0644";
foreach my $i (@files) {
if ("$i" eq "$line") {
$must_use_this = 0;
}
if ("/$i" eq "$line") {
$must_use_this = 0;
}
if ("./$i" eq "$line") {
$must_use_this = 0;
}
}
if($line =~ /^\.(.*)$/o) {
$line = $1;
}
if("$line" ne "prototype") {
if("$line" ne "/prototype") {
if($line =~ /\/bin\//o) {
$mode = "0755";
}
if($line =~ /\/sbin\//o) {
$mode = "0755";
}
if($line =~ /\/libexec\//o) {
$mode = "0755";
}
if($must_use_this) {
print OUTPUT "f none $line=$cwd$line";
print OUTPUT " $mode bin bin\n";
}
}
}
}
close(INPUT);
}
if(open(INPUT, "find . -type l |")) {
my $mode = "0644";
my $line;
my $must_use_this;
my $target;
my $l2;
my $origline;
while() {
$line = $_; chomp $line;
$origline = $line;
$must_use_this = 1;
$mode = "0644";
$target = undef;
foreach my $i (@files) {
if ("$i" eq "$line") {
$must_use_this = 0;
}
if ("/$i" eq "$line") {
$must_use_this = 0;
}
if ("./$i" eq "$line") {
$must_use_this = 0;
}
}
if($line =~ /^\.(.*)$/o) {
$line = $1;
}
if("$line" ne "prototype") {
if("$line" ne "/prototype") {
if($line =~ /\/bin\//o) {
$mode = "0755";
}
if($line =~ /\/sbin\//o) {
$mode = "0755";
}
if($line =~ /\/libexec\//o) {
$mode = "0755";
}
if (open(IN2, "ls -l \"$origline\" |")) {
while() {
$l2 = $_; chomp $l2;
if ("$l2" =~ /.*\->\s+(\S.*)/o) {
$target = $1;
}
}
close(IN2);
if(($must_use_this) && (defined($target))) {
print OUTPUT "s none $line=$target";
print OUTPUT " $mode bin bin\n";
}
}
}
}
}
close(INPUT);
}
} else {
print "dkmkproto: ERROR: Missing pkginfo file!\n";
}
close(OUTPUT);
}