#! /usr/bin/perl # # kl-scripts version 1.7 # # kl-scripts $Id: kl-extract.pl,v 1.5 1999/11/24 03:56:21 knight Exp $ # # Extraction script for kl-scripts files. # use Getopt::Long; my $Version = '1.7'; ($PROG = $0) =~ s(.*/)(); # basename $USAGE = "Usage: $PROG [-hnoqvVw] [-d dir] [-m mode] file|release target [...] --c pass -c to diff --diff diff the current files against package files --dir dir install into specified directory --help display help text --mode mode specify permissions for installed files --noexec don't execute commands, only print them --overwrite overwrite existing files, don't save as file.OLD --quiet quiet, don't print commands --verbose list files when extracting --Version display version information --web fetch from the web"; my $dir = $ENV{HOME}; my $diff = 0; my $exec = 1; my $print = 1; my $tarflags = '-xf'; my $web = 0; my $diff_flags; my $ndir; my $nmode; my $quiet; Getopt::Long::Configure("bundling"); GetOptions( "c" => sub { $diff_flags .= ' -c' }, "diff" => sub { $diff = 1 }, "d=s" => \$ndir, "dir=s" => \$ndir, "n" => sub { if ($seen_exec) { $exec = 0; } else { $makeflags = " -n"; $seen_exec++; } }, "exec!" => sub { if ($seen_exec) { $exec = 0; } else { $makeflags = " -n"; $seen_exec++; } }, "h" => sub { print "$USAGE\n"; exit 0 }, "help" => sub { print "$USAGE\n"; exit 0 }, "m=s" => \$mode, "mode=s" => \$mode, "o" => sub { $klflags .= " --overwrite" }, "overwrite" => sub { $klflags .= " --overwrite" }, "q+" => \$quiet, "quiet+" => \$quiet, "verbose" => sub { $tarflags = '-xvf' }, "Version" => sub { print "kl-scripts version $Version\n"; exit 0 }, "w" => \$web, "web" => \$web, ) || die "Use --h or --help to get help text.\n"; if ($quiet) { $klflags .= " --quiet"; if ($quiet > 1) { $print = 0; } } if (defined $ndir) { if (! $ndir) { print STDERR "$PROG: Ignoring null argument to --dir.\n"; } else { $dir = $ndir; } } if (defined $nmode) { if (! $nmode) { print STDERR "$PROG: Ignoring null argument to --mode.\n"; } else { $mode = $nmode; } } if (@ARGV < 2) { print STDERR "$PROG: Must specify a file|release and one or more targets.\n"; print STDERR "Execute \"$PROG --help\" for help text.\n"; exit 1; } my $tmpdir = ($ENV{TMPDIR} || "/tmp") . "/kl-extract-$$"; print "mkdir $tmpdir\n" if $print; mkdir($tmpdir, 0700) || die "Could not make temporary directory '$tmpdir': $1\n"; END { system("rm -rf $tmpdir"); } my $file = shift @ARGV; if ($web) { $file = "kl-scripts-$file.tar.gz" if $file =~ /\d+\.\d+/; my $host_dir = "www.baldmt.com/kl-scripts"; require LWP::UserAgent; require URI::URL; my $ua = new LWP::UserAgent; print "GET http://$host_dir/$file\n" if $print; my $req = new HTTP::Request 'GET'; my $url = URI::URL->new("http://$host_dir/$file"); $req->url($url); my $res = $ua->request($req, "$tmpdir/$file"); if (! $res->is_success) { print "$PROG: request failed\n"; exit 1 } $file = "$tmpdir/$file"; } else { if ($file =~ /\d+\.\d+/) { my $glob; foreach $suffix (qw(tar.gz tgz tar)) { my $x = "kl-scripts-$file.$suffix"; $glob = $x, last if -f $x; } if (! $glob) { print STDERR "$PROG: Couldn't find a kl-scripts file for '$file'.\n"; print STDERR "\tLogin scripts not unpacked nor installed.\n"; exit 1 } $file = $glob; } } my $base; my $uncompress; if ($file =~ m#(.*/)?(.*)(\.tar\.gz|\.tgz)$#) { $base = $2; my @gzlist = qw(gzcat zcat); foreach $zcat (@gzlist) { foreach my $d (split(':', $ENV{PATH})) { $uncompress = $zcat, last if -f "$d/$zcat"; } last if $uncompress; } if (! $uncompress) { print STDERR "$PROG: Could not find a way to uncompress '$file'.\n"; print STDERR "\tI couldn't find any of '" . join(' ', @gzlist) . "' on your PATH:\n"; print STDERR "\t${\$ENV{PATH}}\n"; print STDERR "\tLogin scripts not unpacked nor installed.\n"; } } elsif ($file =~ m#(.*/)?(.*)\.tar$#) { $base = $2; $uncompress = 'cat'; } else { print STDERR "$PROG: Unknown file name '$file'.\n"; print STDERR "\tI'm expecting a tar file (.tar)\n"; print STDERR "\tor gzip-tar file (.tar.gz or .tgz).\n"; print STDERR "\tLogin scripts not unpacked nor installed.\n"; print STDERR "$USAGE\n"; exit 1 } chomp($pwd = `pwd`); $dir = "$pwd/$dir" if $dir !~ m#^/#; if ($diff) { $prefix = "$tmpdir/install_$$"; print "mkdir $prefix\n" if $print; mkdir($prefix, 0700) || die "Could not make install directory '$prefix': $1\n"; } else { $prefix = $dir; } my $cmd = "$uncompress $file | (cd $tmpdir && tar $tarflags -)"; print "$cmd\n" if $print; if (system($cmd)) { my $exit = $? >> 8; print STDERR "$PROG: Could not unpack the package as follows:\n"; print STDERR "\t$cmd"; print STDERR "\tLogin scripts not configured, built nor installed.\n"; exit 1; } my $kl_dir = "$tmpdir/$base"; if (! -d $kl_dir) { print STDERR "$PROG: No '$base' directory in this package.\n"; print STDERR "\tLogin scripts not configured, built nor installed.\n"; exit 1; } print "cd $kl_dir\n" if $print; if (!chdir($kl_dir)) { print STDERR "$PROG: Could not chdir to '$kl_dir': $!\n"; print STDERR "\tLogin scripts neither built nor installed.\n"; exit 1; } my $cmd = "./configure --prefix='$prefix'"; print "$cmd\n" if $print; if ($exec && system($cmd)) { my $exit = $? >> 8; print STDERR "$PROG: '$cmd' exited status $exit.\n"; print STDERR "\tLogin scripts neither built nor installed.\n"; exit 1; } $cmd = "make " . join(' ', @ARGV); print "$cmd\n" if $print; if ($exec && system($cmd)) { my $exit = $? >> 8; print STDERR "$PROG: '$cmd' exited status $exit.\n"; print STDERR "\tLogin scripts not installed.\n"; exit 1; } $klflags =~ s/^ //; $klflags = " FLAGS='$klflags'" if $klflags; $cmd = "make$makeflags$klflags " . join(' ', map("install-$_", @ARGV)); print "$cmd\n" if $print; if ($exec && system($cmd)) { my $exit = $? >> 8; print STDERR "${PROG}: '$cmd' exited status $exit.\n"; print STDERR "\tLogin scripts installation is incomplete.\n"; exit 1; } if ($diff) { opendir(DIR, $prefix) || die "Could not open $prefix: $1\n"; @files = sort grep($_ ne '.' && $_ ne '..', readdir(DIR)); closedir(DIR); foreach my $file (@files) { print "$file:\n"; system("diff$diff_flags $dir/$file $prefix/$file"); } } exit 0;