#! /usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); use Getopt::Long; use MIME::Base64; my $title = 'CGI Self Extractor'; my $start_msg = 'starting extraction...'; my $end_msg = 'done'; my $check_files = ''; my $use_nph = undef; my $use_help; GetOptions( 'title=s' => \$title, 'start=s' => \$start_msg, 'end=s' => \$end_msg, nph => \$use_nph, 'check-files=s' => \$check_files, help => \$use_help, ) or exit 1; if ($use_help) { print << "EOT"; Usage: $0 [options] < package.tar.gz > installer.cgi Options: --title=title title when run as a CGI --start=msg start message --end=msg end message --nph create nph CGI --check-files=file1,file2,... do not run the installer if these files exist --help show help EOT exit 0; } print << '__EOT__'; #! /usr/bin/perl use MIME::Base64; __EOT__ ; for my $n (qw(title start_msg end_msg use_nph check_files)) { no strict 'refs'; print 'my $', "$n = ", dump(eval('$'.$n)), ";\n"; } print << '__EOT__'; sub abort { print '' if $ENV{SCRIPT_FILENAME}; print $_[0]; print '' if $ENV{SCRIPT_FILENAME}; exit 1; } if ($ENV{SCRIPT_FILENAME}) { print "HTTP/1.0 200 OK\r\n" if $use_nph; print "Content-Type: text/html; charset=utf-8\r\n\r\n"; print << "EOT"; $title

$start_msg

EOT
    ;
}

for my $f (split /\s*,\s*/, $check_files) {
    abort("file: $f already exists, aborting installation")
	if -e $f;
}

open FH, '| gunzip | tar xvf - 2>&1'
    or abort("failed to start extraction:$!");
print FH decode_base64(<< '__end_of_archive__');
__EOT__
;

binmode STDIN;
my $tgz = '';
while (read(STDIN, my $buf, 1048576) > 0) {
    $tgz .= $buf;
}
print encode_base64($tgz);

print << '__EOT__';
__end_of_archive__
    ;
close FH
    or abort("failed to extract archive($!).");

if ($ENV{SCRIPT_FILENAME}) {
    print << "EOT";
$end_msg

The installer CGI was generated by tgz2cgi.pl, written by Kazuho Oku at Cybozu Labs, Inc.

EOT } __EOT__