Tuesday, July 27, 2004

Self extracting installer in Perl

Self-extracting installation is a great feature of windows applications. Clicking on a Setup.exe extracts all files and starts the configuration of the application. No unzipping, no README. I wanted to use the same approach for packaging a PHP-based web application meant for Apache running on a Linux box.

Perl seemed a good choice as platform for the installer because of its compact syntax, multiple platform support and huge number of libraries. The basic idea is very simple - Append a perl script with the zipped archive. This perl script reads itself and extracts the zipped archive, unzips and calls the configuration script. The problem is how to append the zipped archive and still not confuse the interpreter. The trick of using multiline comments in Perl can do the job.

Still reading on OOPS in Perl starting with this handy introduction but the basic Perl script is as follows :

use Shell;

# Change these values for your desired installation
# Parameter 1 - Name of this file
# Parameter 2 - Name of temporary file
# Parameter 2 - Size of the zip file to be appended to script (in bytes)
# Parameter 3 - OS Command for unzip
# Parameter 4 - OS Command to run the configuration script

install("setup.pl", "temp.zip", 200048, "unzip temp.zip -d /usr/local/", "perl /usr/local/configure.pl");

sub install {
my $SETUP = $_[0];
my $TEMP = $_[1];
my $ZIP_SIZE = $_[2];
my $UNZIP_CMD = $_[3];
my $CONFIGURE_CMD = $_[4];

open(PROG, $SETUP) or die "Could not open file : " + $!;
seek(PROG, -$ZIP_SIZE, 2);

open(BINFILE, ">$TEMP") or die "Could not open file : " + $!;
while (read(PROG, $buff, 65536))
print BINFILE $buff;

close(PROG) or die "Could not close file : ";
close(BINFILE) or die "Could not close file :";

print "Extraction complete\n";


# Force the interpreter to think of the binary file as comment.
=for comment

For using the script, you need to modify the size of the zipped archive, the unzipping command and the configuration command. Concatenate the zipped archive to the modified script and you are ready to run the setup using the command : perl -w setup.pl


Post a Comment

<< Home