#!/usr/bin/perl
# Copyright (C) 1994,1995 Florian La Roche
#
# This script will take a list of files and package them into a
# gzip'ped tar-file. Just put each filename into one line and say
# 'PkgTool /absolute/path/to/list' to get list.tgz in your current directory.
# (So you have to use an absolute path for the file list.)
# Do this as root to get a tar-file with exactly the same owners/perms
# as the original files.
# All directories must have a trailing "/". Do not put a leading "/" into
# the listing for the root directory.
#
# You can also take old Slackware/SuSe description files of tar-packages
# to make new tar-files. This script will automatically detect them
# and skip the beginning lines until the listing of included files begins.
#
# Here is a sample file that could be used to package some files:
# etc/termcap
# etc/hosts
# tmp/
# bin/false
#
# Here is a listing of those files, if put into the tar file.
# (This listing can also be used to make packages...) (tar tzvf file.tgz)
# drwxr-xr-x root/root         0 Sep 28 21:48 1995 bin/
# -rwxr-xr-x root/root       331 Aug  7 18:15 1995 bin/false
# drwxr-xr-x root/root         0 Oct 25 15:22 1995 etc/
# -rw-r--r-- root/root    397764 Jul 13 20:10 1995 etc/termcap
# -rw-r--r-- root/root       131 Sep 25 12:49 1995 etc/hosts
# drwxrwxrwt root/root         0 Oct 26 16:06 1995 tmp/

# Save cuurent directory in variable $cwd and change to root dir "/".
chop($cwd = `pwd`);
chdir "/";

# All files will be copied into this temporary directory and then
# tar is called to make the tar-file.
$tmpdir = "/tmp/PkgTool.$$";

$scriptdir = "/var/lib/scripts/";

# A filename that will never exist. (Small hack to copy directories.)
$nix = '______thisisnofilename';

$package = pop(@ARGV);
exit 0 if ! $package;
$package = "/local/ftp/pub/jurix/source/filelist/$package" unless $package =~ /^\//;

system "rm -fr $tmpdir";
system "mkdir -m755 -p $tmpdir";

chop($basename = `basename $package`);

print "Extracting package $basename, please wait...\n";

open(FILE, "$package") || die "Cannot open $package\n";
# Make a loop over each filename.
while (<FILE>) {
    # Remove the last character, this should be '\n'.
    chop;
    # If this is a Slackware/SuSe description file of a tar-file, then
    # skip the beginning of descriptional text.
    if (/^PACKAGE NAME/) {
	while (<FILE>) {
	    if (/^FILE LIST:$/) {
		$_ = <FILE>;
		last;
	    }
	}
    }
    # skip comment lines
    next if ( /^#/ );

    if ( /^dev\/initctl$/ ) {
	system "mkdir -m755 -p $tmpdir/dev";
	system "mknod -m 600 $tmpdir/dev/initctl p";
	next;
    }

    # If this list was generated from "tar xzvvf file.tgz > file", then
    # remove the leading information. (Date, owner, perms, ...)
    if (/\s\w\w\w\s.\d\s\d\d:\d\d\s\d\d\d\d\s(.*)/) {
	$_ = $1;
    }
    # Remove anything past a space character. This is probably also from
    # a tar-listing that tells us where a symlink is pointing to.
    while (/^(.*)\s/) {
	$_ = $1;
    }
    next if ( /^install\/$/ );
    if ( /^install\/doinst.sh$/ ) {
	system "mkdir -m755 -p $tmpdir/install";
	system "cp $scriptdir$basename $tmpdir/install/doinst.sh";
	next
    }

    if (/\/$/) {	# Take this for a directory name...
	# system "touch $_$nix";
	# This is hack, "cp" won't find the file, but will make the
	# directory anyway. By doing this, we are able to put empty
	# dirs into a tar file.
	system "cp -dpP $_$nix $tmpdir 2>/dev/null 1>/dev/null";
	# system "rm -f $_$nix $tmpdir/$_$nix";
    } else {		# Take this for all other files.
	system "cp -dpP '$_' $tmpdir";
    }
}
close FILE;

# Go into the temporary dir with all files.
chdir "$tmpdir";
# Invoke a shell script to delete all symlinks and let them be
# generated by a shell script.
system "ChangeSymlinks";
# Make a tar file.
system "tar -clpf - * | gzip -9 > $cwd/$basename.tgz";

# Go into the original directory.
chdir $cwd;

# Remove all files from the temp dir.
system "rm -fr $tmpdir";

exit 0;
