#!/usr/bin/env perl
#
# krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use strict;

print <<'EOT';
krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
EOT

if (! defined $ENV{'KDEDIR'}) {
	print "Error: No KDEDIR set!\n";
	exit 1;
}

if ($> == 0) {
	print "Since this program is unstable, please do not run it as root.\n";
	exit 1;
}

# Used to show program is not "hung".
$| = 1;
my @nicelines = ("|", "/", "-", "\\");

my $argnum;

if ($#ARGV >= 0) {
	for ($argnum = 0; $argnum <=$#ARGV; $argnum++) {
		unless (-f $ARGV[$argnum]) {
			print "\n$0: no such file $ARGV[$argnum]\n";
			print "\nUsage: $0 [filename...]\n";
			exit 1;
		}
	}
}

my $exitcode = 0;
my $tmpdir = "/tmp/krumple_$$";

for ($argnum = 0; $argnum <= $#ARGV || $argnum == 0; $argnum++) {

# Process a file

my $extractsteps = 0;
my $makesteps = 0;
my $makeinstallsteps = 0;
my $configuresteps = 0;
my $licensefound = 0;
my $license = "";
my $readmefound = 0;
my $readme = "";
my $configurefound = 0;
my $section = "";
my $fullname = "";
my $comment = "";
my @makefiles = ();
my $progdir = "";
my $progname = "";
my $progver = 0;


(0xffff & system("mkdir $tmpdir")) and die "can't make directory /tmp/$$";

my $origunzip = "$tmpdir/archive.tar";
my $origfile = "$tmpdir/archive.tgz";

if ($#ARGV >= 0) {
        if ($#ARGV > 0) {
                print "\nProcessing $ARGV[$argnum]...";
        }
        print "\n";
	(0xffff & system("cp $ARGV[$argnum] $origfile")) and die "can't copy $ARGV[$argnum] to $origfile";
}

else {
	print "\nReading archive from stardard input...\n";
	if (!open(ARCHIVE, ">$origfile")) {
		print "unable to open $origfile for writing.\n";
		(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
		exit 1;
	}
	else {
		print ARCHIVE <STDIN>;
	}
	close ARCHIVE;
	print "done.";
}

# Extract TAR to a temp dir while checking for COPYING and configure files
# and counting number of files in archive. Also create a list of makefiles
# for later use.

print "Extracting to temporary directory...";
print $nicelines[$extractsteps % $#nicelines];
unless (open(TAR, "gunzip -c $origfile | tar xCvf $tmpdir/ - |")) {
	print "Unable to execute gunzip or tar.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

my $isfirst = 1;
while (<TAR>) {
	print "\b", $nicelines[$extractsteps % ($#nicelines+1)];
	chomp;
	if ($isfirst) {
		my $garbage = $_;
		$garbage =~ s/^\.\///;
		($progdir, $garbage) = split (/\//, $garbage);
		($progname, $progver) = split('-', $progdir);
		$isfirst = 0;
	}
	if (/^(\.\/)?$progdir\/LICENSE$/) {
		$license = "LICENSE";
	}
	elsif ($license ne "LICENSE" && /^(\.\/)?$progdir\/COPYING$/) {
		$license = "COPYING";
	}
	elsif (/^(\.\/)?$progdir\/configure$/) {
		$configurefound = 1;
	}
	elsif (/^(\.\/)?$progdir\/(README)$/i) {
		$readme = $+;
	}
	elsif (/Makefile.in$/) {
		$makefiles[++$#makefiles] = substr($_, 0, -3);
	}
	$extractsteps++;
}
print "\b";

close TAR;
if ($?) {
	print "gunzip or tar failed.\n";
	exit 1;
}
else {
	print "done, found $extractsteps files in archive.\n";
}

if (!$progname || !$progver) {
	print "Unable to identify program name or version.\n" .
	      "Please make sure that the main directory inside the archive is named as follows:\n<name>-<version>\nwhere <name> is the program name and <version> is the program version.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}
else {
	print "Identified program as $progname, version $progver.\n";
}

if ($configurefound == 0) {
	print "No configure file found.\n";
	$exitcode = 1;
	next;
}
if ($license eq "") {
	print "No COPYING or LICENSE file found.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}
else {
	print "License file found is $license.\n";
}
if ($readme eq "") {
	print "Warning: No README file found.\n";
}
else {
	print "Readme file found is $readme.\n";
}


# Run "configure" while counting "steps" for percentage meter.

print "Trying to configure...";
print $nicelines[$configuresteps % $#nicelines];
unless (open(CONFIGURE, "cd $tmpdir/$progdir/; ./configure |")) {
	print "unable to execute 'configure'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
        next;
}

while (<CONFIGURE>) {
	print "\b", $nicelines[$configuresteps % ($#nicelines+1)];
	$configuresteps++;
}
print "\b";

close CONFIGURE;
if ($?) {
	print "failed.\n";
	$exitcode = 1;
        next;
}
else {
	print "success, determined $configuresteps configure steps.\n";
}


# Run "make" while counting "steps" for percentage meter.

print "Trying to make...";
print $nicelines[$makesteps % ($#nicelines+1)];
# Redirection of stderr to /dev/null ignores warning messages.
unless (open(MAKE, "cd $tmpdir/$progdir/; make 2>/dev/null |")) {
	print "unable to execute 'make'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

while (<MAKE>) {
	print "\b", $nicelines[$makesteps % ($#nicelines+1)];
	$makesteps++;
}
print "\b";

close MAKE;
if ($?) {
	print "failed.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}
else {
	print "success, determined $makesteps make steps.\n";
}


# Check required disk space for compilation

print "Calculating disk space needed for compilation...";
unless (open(DU, "cd $tmpdir/$progdir/; du -s . |")) {
	print "unable to execute 'du'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

my $compilespace = <DU>;
close DU;

if ($?) {
	print "du failed.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}
else {
	my $garbage;
	($compilespace, $garbage) = split(/\s/, $compilespace);
	print "determined $compilespace kbytes.\n";
}


# Modify makefiles to contain a temporary directory instead of /opt/kde as
# The target directory to install to.

print "Modifying makefiles to install to temporary directory...";
if (0xffff & system("mkdir $tmpdir/kde")) {
	print "can't make directory $tmpdir/kde.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

my $kdeprefix = $ENV{'KDEDIR'};
my $makefile;
foreach $makefile (@makefiles) {
	if (0xffff & system("mv $tmpdir/$makefile $tmpdir/$makefile.krumple")) {
		print "can't move $tmpdir/$makefile to $tmpdir/$makefile.krumple.\n";
		(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
		exit 1;
	}
	unless (open(OLDMAKE, "$tmpdir/$makefile.krumple")) {
		print "unable to read from $tmpdir/$makefile.krumple.\n";
		(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
		exit 1;
	}
	unless (open(NEWMAKE, ">$tmpdir/$makefile")) {
		print "unable to write to $tmpdir/$makefile.\n";
		(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
		exit 1;
	}
	while (<OLDMAKE>) {
		s/$kdeprefix/$tmpdir\/kde/;
		print NEWMAKE;
	}
	close OLDMAKE;
	close NEWMAKE;
	if (0xffff & system("rm -rf $tmpdir/$makefile.krumple")) {
		print "can't delete $tmpdir/$makefile.krumple.\n";
		(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
		exit 1;
	}
}
print "done.\n";


# Run "make install" while counting "steps" for percentage meter.

print "Trying to install...";
print $nicelines[$makeinstallsteps % ($#nicelines+1)];
unless (open(MAKEINSTALL, "cd $tmpdir/$progdir/; make install 2>&1 |")) {
	print "unable to execute 'make install'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

while (<MAKEINSTALL>) {
	print "\b", $nicelines[$makeinstallsteps % ($#nicelines+1)];
	unless (/^mkdir $tmpdir\/kde/ and !(/$progname/)) {
		$makeinstallsteps++;
	}
}
print "\b";

close MAKEINSTALL;
if ($?) {
	print "failed.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}
else {
	print "success, determined $makeinstallsteps make install steps.\n";
}


# Make a list of files that were installed

print "Checking for installed files...";
unless (open(FIND, "cd $tmpdir/kde/; find . -name \"*\" -print |")) {
	print "unable to execute 'find'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

my @installedfiles = ();
my $notsettingsfound = 0;
my $kdelnkname = "";
while (<FIND>) {
	chomp;
	if (!$notsettingsfound &&
	    /^\.\/share\/applnk\/(.+)\/([^\/]+\.kdelnk)$/) {
		$section = $1;
		$kdelnkname = $2;
		$notsettingsfound = 1 unless ($section =~ /^Settings\//);
	}
	unless (/^\.\/share\/apps\/$progname\/kiss.uninstall$/ || -d "$tmpdir/kde/$_") {
		$installedfiles[++$#installedfiles] = $_;
	}
}
close FIND;
if ($?) {
	print "find failed.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}
else {
	print "found ", $#installedfiles+1, " installed files.\n";
}

if ($section) {
	print "Classified program in section $section.\n";
}
else {
	print "The program does not install a kdelnk file in the K menu.\n" .
	      "Unable to classify program.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	$exitcode = 1;
	next;
}


# Get full program name from .kdelnk file

print "Analyzing $kdelnkname file...";
unless (open(KDELNK, "<$tmpdir/kde/share/applnk/$section/$kdelnkname")) {
	print "Unable to read from file $tmpdir/kde/$_\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}
while (<KDELNK>) {
	if (/^Name=(.+)$/) {
		$fullname = $1;
	}
	elsif (/^Comment=(.+)$/) {
		$comment = $1;
	}
}
close KDELNK;

if ($fullname) {
	print "identified program as \"$fullname\".\n";
}
else {
	print "\nWarning: unable to find \"Name=\" statement in $kdelnkname\n" ,
	      "Using \"$progname\" as full program name.\n";
	$fullname = $progname;
}
if ($comment) {
	print "Comment found for program is \"$comment\".\n";
}
else {
	print "\nWarning: unable to find \"Comment=\" statement in $kdelnkname\n",
	      "A command for the program will not be available in the KISS database.\n";
}
	

# Check required disk space for installation

print "Calculating disk space needed for installation...";
unless (open(DU, "cd $tmpdir/kde/; du -s . |")) {
	print "unable to execute 'du'.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

my $installspace = <DU>;
close DU;

if ($?) {
	print "du failed.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}
else {
	my $garbage;
	($installspace, $garbage) = split(/\s/, $installspace);
	print "determined $installspace kbytes.\n";
}


# Generate kiss_removeold script

print "Generating kiss_removeold script...";
unless (open(KISSREMOVEOLD, ">$tmpdir/$progdir/kiss_removeold")) {
	print "unable to write to $tmpdir/$progdir/kiss_removeold.";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

print KISSREMOVEOLD <<'EOF';
#!/bin/sh
# This program was generated by
# krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
#
# This program is
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

if [ "${KDEDIR}" = "" -o ! -x ${KDEDIR}/bin/kiss ]; then
  echo "No KDEDIR set or KISS is not installed!";
  exit 1;
fi

EOF
print KISSREMOVEOLD <<"EOF";
PKGNAME='$progname';
FULLNAME='$fullname';
VERSION='$progver';

EOF
print KISSREMOVEOLD <<'EOF';
if  [ -z "${KDEDIR}/bin/kiss -i $PKGNAME" ]; then
	echo "$FULLNAME not installed.";
	exit 0;
fi

if [ "$1" = "-DO_NOT_PROMPT" ]; then
	${KDEDIR}/bin/kiss -d $PKGNAME;
	exit 0;
fi

CURRENTVERSION="`${KDEDIR}/bin/kiss -q $PKGNAME | grep '^version: ' | sed 's/^version: //'`";

echo "$FULLNAME version $CURRENTVERSION is installed on your system.";
echo "The version of $FULLNAME that you will be installing is $VERSION.";

UNINSTALL="";
until [ "$UNINSTALL" = "n" -o "$UNINSTALL" = "y" ]; do
	echo "Do you wish to uninstall $FULLNAME version $CURRENTVERSION? which is currently";
	echo -n "installed on your system [y/n]?  ";
	read UNINSTALL;
done

if [ "$UNINSTALL" = "y" ]; then
	echo "Uninstalling...";
	${KDEDIR}/bin/kiss -d $PKGNAME;
else
	echo "Leaving $FULLNAME version $CURRENTVERSION installed.";
fi

# EOF
EOF

close(KISSREMOVEOLD);
unless (chmod 0755, "$tmpdir/$progdir/kiss_removeold") {
        print "could not make $tmpdir/$progdir/kiss_removeold executable.\n";
        (0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
        exit 1;
}

print ("done.\n");


# Generate kiss_update script

print "Generating kiss_update script...";
unless (open(KISSUPDATE, ">$tmpdir/$progdir/kiss_update")) {
	print "unable to write to $tmpdir/$progdir/kiss_update.";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

print KISSUPDATE <<'EOF';
#!/bin/sh
# This program was generated by
# krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
#
# This program is
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

if [ "${KDEDIR}" = "" -o ! -x ${KDEDIR}/bin/kiss ]; then
  echo "No KDEDIR set or KISS is not installed!";
  exit 1;
fi
EOF

print KISSUPDATE <<"EOF";
(
echo 'comment';
echo '$comment';
echo 'name';
echo '$fullname';
echo 'section';
echo '$section';
echo 'version';
echo '$progver';
echo '';
EOF

my $file;
foreach $file (@installedfiles) {
	$file =~ s/^\.//;
	print KISSUPDATE 'echo ${KDEDIR}\'', $file, "';\n";
}

print KISSUPDATE <<"EOF";
) | \${KDEDIR}/bin/kiss -e '$progname';

# EOF
EOF

close(KISSUPDATE);
unless (chmod 0755, "$tmpdir/$progdir/kiss_update") {
        print "could not make $tmpdir/$progdir/kiss_update executable.\n";
        (0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
        exit 1;
}

print ("done.\n");


#### To be implemented (gui generation):
## Generate kissrun script
#
#print "Generating kissrun script...";
#unless (open(KISSRUN, ">$tmpdir/$progdir/kissrun")) {
#	print "unable to write to $tmpdir/$progdir/kissrun.";
#	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
#	exit 1;
#}
#
#print KISSRUN <<'EOF';
##!/usr/bin/env perl
##
## This program was generated by
## krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
## Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
##
## This program is
## Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo@leyada.jlm.k12.il>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2 
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
#
#use strict;
#
#EOF
#
#print KISSRUN <<"EOF";
#my \$readme = '$readme';
#my \$license = '$license';
#my \$makesteps = $makesteps;
#my \$makeinstallsteps = $makeinstallsteps;
#my \$configuresteps = $configuresteps;
#my \$section = '$section';
#my \$fullname = '$fullname';
#my \$comment = '$comment';
#my \$shortname = '$progname';
#my \$version = '$progver';
#my \$kdelnk = \$ENV{'KDEDIR'} . '/share/applnk/$section/$kdelnkname';
#
#EOF
#
#
#print KISSRUN <<'EOF';
#
###### ALL GUI STUFF SHOULD GO HERE!!! #####
#
## system('kiss_update') should be used to install the entry for the package
## EOF
#EOF
#
#close(KISSRUN);
#unless (chmod 0755, "$tmpdir/$progdir/kissrun") {
#        print "could not make $tmpdir/$progdir/kissrun executable.\n";
#        (0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
#        exit 1;
#}
#
#print ("done.\n");


# Generate installation script

my $scriptname = "${progname}-${progver}.installer";
my $encodename = "${progname}-${progver}.tgz";
my $encodeunzip = "${progname}-${progver}.tar";

print "Generating installation script (${scriptname})...";

(0xffff & system("cd $tmpdir; gunzip $origfile; tar rf $origunzip $progdir/kiss_removeold $progdir/kiss_update; gzip -c $origunzip > $origfile; rm -f $origunzip")) and die "can't add files to archive $origfile";


unless (open(SCRIPT, ">$scriptname")) {
	print "unable to open $scriptname for writing.\n";
	(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
	exit 1;
}

# Print starts
print SCRIPT <<"EOF";
#!/usr/bin/env perl
#
# The expression "this code" in the following statement applies only to the
# files "kiss_removeold" and "kiss_update" located inside the directory
# $progdir inside the encoded compressed archive contained in this file
# and to the data preceeding the line containing only the string "__END__"
# in this file.
#
# This code was generated by:
# krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>
#
# This code is
# Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use strict;

if (! defined \$ENV{'KDEDIR'}) {
        print "Error: No KDEDIR set!\n";
        exit 1;
}

# Script-generated variables 
my \$fileNum = $extractsteps;
my \$fileName = '$encodename';
my \$unZipped = '$encodeunzip';
my \$mainDirName = '$progdir';
my \$readme = '$readme';
my \$license = '$license';
my \$makeSteps = $makesteps;
my \$makeInstallSteps = $makeinstallsteps;
my \$configureSteps = $configuresteps;
my \$section = '$section';
my \$fullName = '$fullname';
my \$comment = '$comment';
my \$shortName = '$progname';
my \$version = '$progver';
my \$kdelnk = \$ENV{'KDEDIR'} . '/share/applnk/$section/$kdelnkname';

EOF
# Print ends

# Print starts
print SCRIPT <<'EOF';
# General variables
my $kissStatus = getKissStatus();
my $tmpDir = "/tmp/krumple_$$";
my $title = "$fullName $version";
my $notice = 
"This installer was generated by:\n" .
"krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)\n" .
"Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>\n" .
"\n" .
"This installer is\n" .
"Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>\n" .
"\n" .
"This program is free software; you can redistribute it and/or\n" .
"modify it under the terms of the GNU General Public License\n" .
"as published by the Free Software Foundation; either version 2\n" .
"of the License, or (at your option) any later version.\n" .
"" .
"This program is distributed in the hope that it will be useful,\n" .
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" .
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" .
"GNU General Public License for more details.\n" .
"\n" .
"You should have received a copy of the GNU General Public License\n" .
"along with this program; if not, write to the Free Software\n" .
"Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n" .
"\n" .
"This installer uses kiss - the KDE Installation SyStem database, which is:\n" .
"Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>\n" .
"                         and Antal Novak <afn\@weevil.net>\n" .
"Type 'kiss' at the command prompt for more information about kiss.";

# Variables related to text mode
my $progressSymbol = '.';
my $lineWidth = 80;
my $niceHeight = 40;

# Installation-time-determined variables
my $homeDirectory = `echo -n ~`;
my $language = getLang();


unless (createTempDir()) {
	exit 1;
}

my $exitValue;

if (!($#ARGV >= 0)) {
	if (defined $ENV{'DISPLAY'}) {
		# Will be gui() in later versions.
		# $exitValue = ! gui();
		$exitValue = ! textwin();
	}
	else {
		$exitValue = ! text();
	}
}
elsif ($#ARGV == 0) {
	if ($ARGV[0] eq "-gui") {
		$exitValue = ! gui();
	}
	elsif ($ARGV[0] eq "-text") {
		$exitValue = ! text();
	}
	elsif ($ARGV[0] eq "-textwin") {
		$exitValue = ! textwin();
	}
	elsif ($ARGV[0] eq "-tgz") {
		$exitValue = ! tgz();
	}
	elsif ($ARGV[0] eq "-f") {
		$exitValue = ! printFiles();
	}
	elsif ($ARGV[0] eq "-q") {
		$exitValue = ! printInfo();
	}
	else {
		usage();
		$exitValue = 1;
	}
}
else {
	usage();
	$exitValue = 1;
}

if (!cleanUpTempDir()) {
	$exitValue = 1;
}

exit $exitValue;

sub printInfo() {
	print <<"EOT";
package: $shortName
comment: $comment
name: $fullName
section: $section
version: $version

EOT
	return 1;
}

sub printFiles() {
EOF
# Print ends
foreach $file (@installedfiles) {
	print SCRIPT "\tprint \$ENV{'KDEDIR'}, '", $file, "', \"\\n\";\n";
}

#Print starts
print SCRIPT <<'EOF';
	return 1;
}

sub isLangSupported($) {
	my $query = shift;
	return ($query eq "he"	or
		$query eq "en"	or
		$query eq "C");
}

sub getLang() {
	my $lang = "";
	if (open(KDERC, "<$homeDirectory/.kderc")) {
		READFROMFILE: while (<KDERC>) {
			chomp;
			if ($_ eq '[Locale]') {
				while(<KDERC>) {
					chomp;
					if (/^Language=(.+)$/) {
						my $priority;
						foreach $priority (split /:/, $1) {
							if (isLangSupported($priority)) {
								$lang = $priority;
								last READFROMFILE;
							}
						}
						print "Warning: none of the languages from your KDE configuration file\n" .
						      "($homeDirectory/.kderc) are supported - using English.\n\n";
						$lang = "C";
						last READFROMFILE;
					}
					elsif (/^\[.+\]$/) {
						last READFROMFILE;
					}
				}
			}
		}
		close(KDERC);
	}
	# Default to the "default locale"
	if ($lang eq "") {
		print "Warning: unable to read language settings from you KDE configuration file\n" .
 		      "($homeDirectory/.kderc) - using English.\n";
		$lang = "C";
	}

	return $lang;	
}

sub printAligned(@) {
	my $last = $_[$#_];
	my $oldflush = $|;
	unless (chomp $last) {
		$| = 1;
	}
	if (isRtoL()) {
		my $newLine;
		my $line;
		foreach $line (split /^/m, join '', @_) {
			$newLine = chomp $line;
			print " " x ($lineWidth-length $line), $line;
			print "\n" if $newLine;
		}
		unless ($newLine) {
			print "\r";
		}
	}
	else {
		print @_;
	}
	$| = $oldflush;
}

sub tgz() {
	unless (open (DECODE, "| uudecode")) {
		printAligned translate("Unable to execute '%s'.", "uudecode");
		return 0;
	}
	print DECODE <DATA>;
	close DECODE;
	if ($?) {
		printAligned translate("'%s' failed.", "uudecode");
		return 0;
	}
#	system("gunzip $filename; tar --delete -f $unzipped $maindirname/kissrun 2>&1 > /dev/null; gzip -c $unzipped > $filename; rm -f $unzipped");
	printAligned translate("Archive file reconstructed into '%s'.", $fileName), "\n";

	print <<"EOT";

Please note:

The files '$mainDirName/kiss_removeold' and '$mainDirName/kiss_update'
contained in this archive were generated by:
krumple - KDE Installation SyStem Installer Generator 0.1 (Jul 15, 1999)
Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>

These files are
Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>

These files are free software; you can redistribute them and/or
modify them under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

These files are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

These files use kiss - the KDE Installation SyStem database, which is:
Copyright (C) 1998-1999  Yannai A. Gonczarowski <yannaigo\@leyada.jlm.k12.il>
			 and Antal Novak <afn\@weevil.net>
Type 'kiss' at the command prompt for more information about kiss.
EOT
	return 1;
}

sub gui() {
	printAligned translate("GUI not implemented yet."), "\n";
	return 0;
#	if (extractToTempDir()) {
#		return guiInstall();
#	}
#	else {
#		return 0;
#	}
}

sub guiInstall() {
	return ! (0xffff & system("cd $tmpDir/$mainDirName; kissrun"));
}

sub text() {
	if (extractToTempDir()) {
		return textInstall();
	}
	else {
		return 0;
	}
}

sub textInstall() {
####
	textBanner();
	printAligned translate("The license for %s will now be displayed.", $fullName), "\n\n";
	printAligned translate("Please read it carefully. After reading the license you will be asked to \n" . "acknowledge your acceptance to it."), "\n\n";
	printAligned translate("While reading %s:\n" . 
			"Pressing the 'enter' key will scroll one line down,\n" .
			"and pressing the 'space' key will scroll one page down.\n" .
			"When you have finished reading, press the 'q' key.",
			translate("the license")), "\n\n";
	printAligned translate("Press the 'enter' key to continue...");
	scalar <STDIN>;

	if (0xffff & system("more $tmpDir/$mainDirName/$license")) {
		printAligned translate("Unable to execute '%s'.", "more"), "\n";
		return 0;
	}

	print "\n";

	if (textChoice(translate("Do you accept the license you just read? [accept/do not accept]? "),
			translate("accept"), translate("do not accept")) eq
	    translate("do not accept")) {
		return 1;
	}

	textBanner();
	printAligned translate("%s...", translate("Initiating installation process")), "\n";

	print "\n";
	printAligned translate("%s...", translate("Configuring makefiles")), "\n";
	unless (open(CONFIGURE, "cd $tmpDir/$mainDirName; ./configure |")) {
		printAligned translate("Unable to execute '%s'.", "configure", "\n");
		return 0;
	}
	textProgress(\*CONFIGURE, $configureSteps);
	close CONFIGURE;
	if ($?) {
		printAligned translate("%s failed. Please try to install the package manually.", translate("Configuring makefiles")), "\n";
		return 0;
	}

	print "\n";
	printAligned translate("%s...", translate("Compiling")), "\n";
	# Redirection of stderr to /dev/null ignores warning messages.
	unless (open(MAKE, "cd $tmpDir/$mainDirName; make 2>/dev/null |")) {
		printAligned translate("Unable to execute '%s'.", "make"), "\n";
		return 0;
	}
	textProgress(\*MAKE, $makeSteps);
	close MAKE;
	if ($?) {
		printAligned translate("%s failed. Please try to install the package manually.", translate("Compiling")), "\n";
		return 0;
	}

	# Build the command to be run as root:
	my $installCommand;
	if ($> == 0) {
		$installCommand = "(";
	}
	else {
		$installCommand = "su root -c 'echo \"\"; ";
	}
	$installCommand .= "cd $tmpDir/$mainDirName; ";
	if ($kissStatus > 1) {
		$installCommand .= "./kiss_removeold -DO_NOT_PROMPT; ";
	}
	$installCommand .= "make install; ";
	if ($kissStatus > 0) {
		$installCommand .= "./kiss_update; ";
	}
	if ($> == 0) {
		$installCommand .= ")";
	}
	else {
		$installCommand .= "'";
	}

	unless (open(INSTALL, "$installCommand 2>&1 |")) {
		printAligned translate("Unable to execute '%s', '%s', or '%s'.", "kiss_removeold", "make install",  "kiss_update"), "\n";
		return 0;
	}

	if ($> != 0) {
		print "\n";
		# The root password is read directly by the "su" command,
		# to avoid security holes.
		printAligned translate("Please enter the root password for installation: ");

		# This waits until the password was given.
		# (skips the Password: line).
		scalar <INSTALL>;
		print "\n";

		# If the next line is not empty, su failed
		while (<INSTALL> ne "\n") {
			# Don't close the pipe until the command exits
			while(<INSTALL>) { }
			close INSTALL;
			unless (open(INSTALL, "$installCommand 2>&1 |")) {
				printAligned translate("Unable to execute '%s', '%s', or '%s'.", "kiss_removeold", "make install",  "kiss_update"), "\n";
				return 0;
			}
			printAligned translate("Incorrect password. %s", translate("Please enter the root password for installation: "));
			# Wait for the psasword again...
			scalar <INSTALL>;
			print "\n";
		}
	}

	print "\n";
	printAligned translate("%s...", translate("Installing")), "\n";

	textProgress(\*INSTALL, $makeInstallSteps);
	close INSTALL;
	if ($?) {
		printAligned translate("%s failed. Please try to install the package manually.", translate("Installing")), "\n";
		return 0;
	}

	print "\n";
	if (textChoice(translate("Do you wish to create a %s on %s? [y/n]? ",
				  translate("shortcut icon"),
				  translate("the desktop")),
			translate("y"), translate("n")) eq translate("y")) {
		unless (createDesktopShortcut()) {
			printAligned translate("Unable to create %s, please try to do so manually later.", translate("shortcut icon")), "\n";
		}
	}
	if (textChoice(translate("Do you wish to create a %s on %s? [y/n]? ",
				  translate("shortcut button"),
				  translate("the panel")),
			translate("y"), translate("n")) eq translate("y")) {
		unless (createPanelShortcut()) {
			printAligned translate("Unable to create %s, please try to do so manually later.", translate("shortcut button")), "\n";
		}
	}

	if (defined $ENV{'DISPLAY'}) {
		print "\n";
		textRefreshDesktopandKPanel();
	}

	print "\n";
	printAligned translate("%s installed successfully.", $title), "\n";

	print "\n";
	if ($readme ne "") {
		if (textChoice(translate("Do you wish to read %s? [y/n]? ",
					  translate("the readme file")),
				translate("y"), translate("n")) eq
		    translate("y")) {
			print "\n";
			printAligned translate("While reading %s:\n" . 
					"Pressing the 'enter' key will scroll one line down,\n" .
					"and pressing the 'space' key will scroll one page down.\n" .
					"When you have finished reading, press the 'q' key.", translate("the readme file")), "\n\n";
			printAligned translate("Press the 'enter' key to continue...");
			scalar <STDIN>;
			if (0xffff & system("more $tmpDir/$mainDirName/$readme")) {
				printAligned translate("Unable to execute '%s'.", "more"), "\n";
				return 0;
			}
		}
	}

	return 1;
}

sub createDesktopShortcut() {
	return ! (0xffff & system("cp '$kdelnk' $homeDirectory/Desktop/'${fullName}.kdelnk'"));
}

sub createPanelShortcut() {
	my $success = 0;
	my $kpanelConfig = $homeDirectory . "/.kde/share/config/kpanelrc";
	
	if (0xffff & system("cp '$kpanelConfig' '$kpanelConfig.krumplebak'")) {
		printAligned translate("Unable to copy '%s' to '%s'.", $kpanelConfig, "$kpanelConfig.krumplebak"), "\n";
		return 0;
	}
	unless (open(OLDRC, "<$kpanelConfig.krumplebak")) {
		printAligned translate("Unable to read from '%s'.", "$kpanelConfig.krumplebak"), "\n";
		return 0;
	}
	unless (open(NEWRC, ">$kpanelConfig")) {
		printAligned translate("Unable to write to '%s'.",$kpanelConfig), "\n";
		return 0;
	}
	while (<OLDRC>) {
		print NEWRC;
		chomp;
		if ($_ eq '[kpanelButtons]') {
			while(<OLDRC>) {
				chomp;
				print NEWRC;
				if (/^Buttons=/) {
					$success = 1;
					if (/,$/) {
						print NEWRC $kdelnk, ",";
					}
					else {
						print NEWRC ",", $kdelnk;
					}
				}
				elsif (/^ButtonDelta=/) {
					if (/,$/) {
						print NEWRC "0,";
					}
					else {
						print NEWRC ",0";
					}
				}
				print NEWRC "\n";
				if (/^\[.+\]$/) {
					last;
				}
			}
		}
	}

	close OLDRC;
	close NEWRC;

#	if (0xffff & system("rm -rf $kpanelConfig.krumplebak")) {
#		print "can't delete $kpanelConfig.krumplebak.\n";
#		return 0;
#	}
#	print "Your old kpanel configuration file was backed up\nto the file $kpanelConfig.krumplebak\n";

	return $success;
}

sub textBanner() {
	system("clear");
	print "-" x $lineWidth, "\n";
	printf "%-" . ($lineWidth-length(translate("Installer"))) . "s" . translate("Installer") . "\n", $title;
	print "-" x $lineWidth, "\n\n";
}

sub textRefreshDesktopandKPanel() {
	printAligned translate("%s...", translate("Refreshing the desktop")), "\n";
	system('kfmclient refreshDesktop');
	printAligned translate("%s...", translate("Refreshing panel menus and buttons (this could take a few seconds)")), "\n";
	system('kwmcom kpanel:restart');
}

sub textwin() {
	# Some languages need special fonts (maybe other options as well?)
	my $additionaloptions;
	if ($language eq "he") {
		$additionaloptions = "-fn heb6x13";
	}
	else {
		$additionaloptions = "";
	}

	my $closewindowtext;
	$closewindowtext = translate("Press the '\"'enter'\"' key to close the window...");
	if (isRtoL()) {
		$closewindowtext = " " x ($lineWidth-length($closewindowtext)+($closewindowtext =~ s/'/'/g)) . $closewindowtext . "\r";
	}
	if (0xffff & system("xterm +sb -name '$title " . translate("Installer") . "' -title '$title " . translate("Installer") . "' -geometry ${lineWidth}x${niceHeight} $additionaloptions -e sh -c \"'\"'$0'\"'\"' -text; echo \"\"; /bin/echo -n \"$closewindowtext\"; read'")) {
		printAligned translate("Unable to execute '%s'.", "xterm"), "\n";
		return 0;
	}
	return 1;
}

sub getKissStatus() {
	if (! -x "$ENV{'KDEDIR'}/bin/kiss") {
		return 0;
	}

	if (`$ENV{'KDEDIR'}/bin/kiss -i $shortName` eq "") {
		return 1;
	}
	else {
		my $curversion = `$ENV{'KDEDIR'}/bin/kiss -q $shortName`;
		$curversion =~ /^(version: .+)$/m;
		$curversion = $1;
		$curversion =~ s/^version: //;
		if ($curversion lt $version) {
			return 2;
		}
		elsif ($curversion eq $version) {
			return 3;
		}
		else {
			return 4;
		}
	}
}

sub confirmBegin() {
	if ($ARGV[0] eq "-gui") {
		#### To be implemented
	}
	elsif ($ARGV[0] eq "-text") {
		textBanner();
		print $notice . "\n\n";
		printAligned translate("This installer will install %s on your system.", $title), "\n\n";
		if ($kissStatus == 0) {
			printAligned translate("Warning: The KDE Installation SyStem (kiss) is not installed on your computer.\n" . "You will not be able to uninstall %s once it is installed!", $fullName), "\n\n";
		}
		elsif ($kissStatus == 2) {
			printAligned translate("An older version of %s is installed on your system.", $fullName), "\n";
		}
		elsif ($kissStatus == 3) {
			printAligned translate("Version %s of %s is already installed on your system.", $version, $fullName), "\n";
		}
		elsif ($kissStatus == 4) {
			printAligned translate("A newer version of %s is already installed on your system.", $fullName), "\n";
		}
		if ($kissStatus > 1) {
			printAligned translate("Installing will delete that version from your computer.\n" . "It will not be possible to restore it later!"), "\n\n";
		}
	
		return (textChoice(translate("Do you wish to continue? [y/n]? "),
				    translate("y"), translate("n")) eq
			translate("y"));
	}
}

sub textChoice($@) {
	my $prompt = shift;
	my %options = ();
	my $option;
	foreach $option (@_) {
		$options{$option} = 1;
	}
	my $input = "";
	while (! defined $options{$input}) {
		printAligned $prompt;
		$input = <STDIN>;
		chomp $input;
	}
	return $input;
}

sub createTempDir() {
	if (0xffff & system("mkdir $tmpDir")) {
		printAligned translate("Unable to make directory '%s'.", $tmpDir), "\n";
		return 0;
	}
	else {
		return 1;
	}
}

sub extractToTempDir() {
	if (!confirmBegin()) {
		return 0;
	}

	unless (open (DECODE, "| (cd $tmpDir; uudecode)")) {
		printAligned translate("Unable to execute '%s'.", 'uudecode'), "\n";
		return 0;
	}
	print DECODE <DATA>;
	close DECODE;

	if ($?) {
		print "uudecode failed.\n";
		return 0;
	}

	unless (open (TAR, "gunzip -c $tmpDir/$fileName | tar xCvf $tmpDir/ - |")) {
		printAligned translate("Unable to execute '%s' or '%s'.", "gunzip", "tar"), "\n";
		return 0;
	}

	displayProgress(\*TAR);
	close TAR;

	if ($?) {
		printAligned translate("Failed to extract archive."), "\n";
		return 0;
	}
	if (0xffff & system("rm -f $tmpDir/$fileName")) {
		printAligned translate("Unable to delete '%s'.", $tmpDir/$fileName), "\n";
		return 0;
	}
	return 1;
}

sub displayProgress($) {
	my $inputStream = shift;
	if ($ARGV[0] eq "-text") {
		print "\n";
		printAligned translate("Please wait while the installer prepares the KDE Installation SyStem wizard\n". "which will guide you through the rest of the installation process..."), "\n";

		textProgress($inputStream, $fileNum);
	}
	elsif ($ARGV[0] eq "-gui") {
		#### To be implemented
	}
}

sub textProgress($$) {	
	my $inputStream = shift;
	my $numSteps = shift;
	my $progress = 0;
	my $onScreen = 0;
	my $oldflush = $|;
	print "0%", " " x (($lineWidth-9)/2), "50%",
	            " " x (($lineWidth-8)/2), "100%\n";
	$| = 1;
	while (<$inputStream>) {
		$progress += $lineWidth/$numSteps;
		if ($lineWidth >= int $progress) {
			print $progressSymbol x (int ($progress-$onScreen));
			$onScreen = int $progress;
		}
	}
	if ($onScreen < $lineWidth) {
		print $progressSymbol x ($lineWidth-$onScreen);
	}
	$| = $oldflush;
	print "\n";
}

sub cleanUpTempDir() {
	if (0xffff & system("rm -rf $tmpDir")) {
		printAligned translate("Unable to delete '%s'.", $tmpDir), "\n";
		return 1;
	}
	else {
		return 0;
	}
}

# Translating methods:
sub usage() {
	if ($language eq "he") {
		printAligned <<"EOT";
$0 [ -gui | -text | -textwin | -tgz | -f | -q ] :

.(  )           :-gui
.(X -     )          :-text
.( )          :-textwin
.   $fileName           :-tgz
.('kiss -f'  )              :-f
.('kiss -q'  )            :-q
EOT
	}
	else {
		printAligned <<"EOT";
Usage: $0 [ -gui | -text | -textwin | -tgz | -f | -q ]

-gui:     Run the installer in GUI mode (not yet implemented).
-text:    Run the installer in text mode (default when not running under X).
-textwin: Run the installer in text mode, in a new terminal window (default).
-tgz:     Reconstruct the file $fileName to the current directory.
-f:       Print files contained in the package (in the format of "kiss -f")
-q:       Print information about the package (in the format of "kiss -q")
EOT
	}
}

sub isRtoL() {
	return ($language eq "he");
}

sub translate(@) {
	my $inEnglish = shift;
	my @subStrings;
	if (isRtoL()) {
		@subStrings = reverse @_;
	}
	else {
		@subStrings = @_;
	}

	# Use english as default:
	my $translated = $inEnglish;

	# I18N Begins here.
	# Hebrew start.
	if ($language eq "he") {
		if ($inEnglish eq "Unable to execute '%s'.") {
			$translated = ".'%s'    ";
		}
		elsif ($inEnglish eq "Unable to execute '%s' or '%s'.") {
			$translated = ".'%s'   '%s'    ";
		}
		elsif ($inEnglish eq "Unable to execute '%s', '%s', or '%s'.") {
			$translated = ".'%s'   '%s'  ,'%s'    ";
		}
		elsif ($inEnglish eq "'%s' failed.") {
			$translated = ".  '%s' ";
		}
		elsif ($inEnglish eq "Archive file reconstructed into '%s'.") {
			$translated = ".'%s'     ";
		}
		elsif ($inEnglish eq "GUI not implemented yet.") {
			$translated = ".    ";
		}
		elsif ($inEnglish eq "The license for %s will now be displayed.") {
			$translated = ".  %s   ";
		}
		elsif ($inEnglish eq "Please read it carefully. After reading the license you will be asked to \n" . "acknowledge your acceptance to it.") {
			$translated = ".    /   .  / "
		}
		elsif ($inEnglish eq "While reading %s:\n" .
		       "Pressing the 'enter' key will scroll one line down,\n" .
		       "and pressing the 'space' key will scroll one page down.\n" .
		       "When you have finished reading, press the 'q' key.") {
			$translated = ":%s  \n" .
			  ",      'enter'-   \n" .
			    ".      ''-   \n" .
			      ".'q'-   /  ";
		}
		elsif ($inEnglish eq "the license") {
			$translated = "  ";
		}
		elsif ($inEnglish eq "Press the 'enter' key to continue...") {
			$translated = "...  'enter'-   /";
		}
		elsif ($inEnglish eq "Do you accept the license you just read? [accept/do not accept]? ") {
			$translated = " ?[accept/do not accept]     / / ";
		}
		elsif ($inEnglish eq "accept") {
			$translated = "accept";
		}
		elsif ($inEnglish eq "do not accept") {
			$translated = "do not accept";
		}
		elsif ($inEnglish eq "Initiating installation process") {
			$translated = "  ";
		}
		elsif ($inEnglish eq "%s...") {
			$translated = "...%s";
		}
		elsif ($inEnglish eq "Configuring makefiles") {
			$translated = "   ";
		}
		elsif ($inEnglish eq "Compiling") {
			$translated = " ";
		}
		elsif ($inEnglish eq "Installing") {
			$translated = " ";
		}
		elsif ($inEnglish eq "%s failed. Please try to install the package manually.") {
			$translated = ".    /  .%s   ";
		}
		elsif ($inEnglish eq "Please enter the root password for installation: ") {
			$translated = " :  (root)     / ";
		}
		elsif ($inEnglish eq "Incorrect password. %s") {
			$translated = "%s . ";
		}
		elsif ($inEnglish eq "Do you wish to create a %s on %s? [y/n]? ") {
			$translated = " ?[y/n] ?%s  %s   ";
		}
		elsif ($inEnglish eq "shortcut icon") {
			$translated = "  ";
		}
		elsif ($inEnglish eq "the desktop") {
			$translated = " ";
		}
		elsif ($inEnglish eq "y") {
			$translated = "y";
		}
		elsif ($inEnglish eq "n") {
			$translated = "n";
		}
		elsif ($inEnglish eq "Unable to create %s, please try to do so manually later.") {
			$translated = ".     /  .%s    ";
		}
		elsif ($inEnglish eq "shortcut button") {
			$translated = "  ";
		}
		elsif ($inEnglish eq "the panel") {
			$translated = " ";
		}
		elsif ($inEnglish eq "%s installed successfully.") {
			$translated = ".  %s ";
		}
		elsif ($inEnglish eq "Do you wish to read %s? [y/n]? ") {
			$translated = " ?[y/n] ?%s    ";
		}
		elsif ($inEnglish eq "the readme file") {
			$translated = "readme- ";
		}
		elsif ($inEnglish eq "Unable to copy '%s' to '%s'.") {
			$translated = ".'%s'- '%s'    ";
		}
		elsif ($inEnglish eq "Unable to read from '%s'.") {
			$translated = ".'%s'-   ";
		}
		elsif ($inEnglish eq "Unable to write to '%s'.") {
			$translated = ".'%s'-   ";
		}
		elsif ($inEnglish eq "Installer") {
			$translated = "";
		}
		elsif ($inEnglish eq "Refreshing the desktop") {
			$translated = "   ";
		}
		elsif ($inEnglish eq "Refreshing panel menus and buttons (this could take a few seconds)") {
			$translated = "(     )      ";
		}
		# '\"' in the following string means a single quote. These
		# must pair.
		elsif ($inEnglish eq "Press the '\"'enter'\"' key to close the window...") {
			$translated = "...    '\"'enter'\"'-   /";
		}
		elsif ($inEnglish eq "This installer will install %s on your system.") {
			$translated = ".   %s      ";
		}
		elsif ($inEnglish eq "Warning: The KDE Installation SyStem (kiss) is not installed on your computer.\n" . "You will not be able to uninstall %s once it is installed!") {
			$translated = "   (kiss - The KDE Installation SyStem) KDE    :\n" . ".  %s       .  ";
		}
		elsif ($inEnglish eq "An older version of %s is installed on your system.") {
			$translated = ".    %s     ";
		}
		elsif ($inEnglish eq "Version %s of %s is already installed on your system.") {
			$translated = ".     %s   %s ";
		}
		elsif ($inEnglish eq "A newer version of %s is already installed on your system.") {
			$translated = ".     %s     ";
		}
		elsif ($inEnglish eq "Installing will delete that version from your computer.\n" . "It will not be possible to restore it later!") {
			$translated = "!       .      ";
		}
		elsif ($inEnglish eq "Do you wish to continue? [y/n]? ") {
			$translated = " ?[y/n] ?  ";
		}
		elsif ($inEnglish eq "Unable to make directory '%s'.") {
			$translated = ".'%s'      ";
		}
		elsif ($inEnglish eq "Failed to extract archive.") {
			$translated = ".   ";
		}
		elsif ($inEnglish eq "Unable to delete '%s'.") {
			$translated = ".'%s'    ";
		}
		elsif ($inEnglish eq "Please wait while the installer prepares the KDE Installation SyStem wizard\n". "which will guide you through the rest of the installation process...") {
			$translated = "KDE Installation SyStem-         / \n" .
				      "...     ";
		}
	}
	# Hebrew end.
	# Translation template start.
	elsif ($language eq "YOUR LANGUAGE HERE") {
		if ($inEnglish eq "Unable to execute '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to execute '%s' or '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to execute '%s', '%s', or '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "'%s' failed.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Archive file reconstructed into '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "GUI not implemented yet.") {
			$translated = "";
		}
		elsif ($inEnglish eq "The license for %s will now be displayed.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Please read it carefully. After reading the license you will be asked to \n" . "acknowledge your acceptance to it.") {
			$translated = ".    /   .  / "
		}
		elsif ($inEnglish eq "While reading %s:\n" .
		       "Pressing the 'enter' key will scroll one line down,\n" .
		       "and pressing the 'space' key will scroll one page down.\n" .
		       "When you have finished reading, press the 'q' key.") {
			$translated = ":%s  \n" .
			  ",      'enter'-   \n" .
			    ".      ''-   \n" .
			      ".'q'-   /  ";
		}
		elsif ($inEnglish eq "the license") {
			$translated = "";
		}
		elsif ($inEnglish eq "Press the 'enter' key to continue...") {
			$translated = "";
		}
		elsif ($inEnglish eq "Do you accept the license you just read? [accept/do not accept]? ") {
			$translated = "";
		}
		elsif ($inEnglish eq "accept") {
			$translated = "";
		}
		elsif ($inEnglish eq "do not accept") {
			$translated = "";
		}
		elsif ($inEnglish eq "Initiating installation process") {
			$translated = "";
		}
		elsif ($inEnglish eq "%s...") {
			$translated = "";
		}
		elsif ($inEnglish eq "Configuring makefiles") {
			$translated = "";
		}
		elsif ($inEnglish eq "Compiling") {
			$translated = "";
		}
		elsif ($inEnglish eq "Installing") {
			$translated = "";
		}
		elsif ($inEnglish eq "%s failed. Please try to install the package manually.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Please enter the root password for installation: ") {
			$translated = "";
		}
		elsif ($inEnglish eq "Incorrect password. %s") {
			$translated = "";
		}
		elsif ($inEnglish eq "Do you wish to create a %s on %s? [y/n]? ") {
			$translated = "";
		}
		elsif ($inEnglish eq "shortcut icon") {
			$translated = "";
		}
		elsif ($inEnglish eq "the desktop") {
			$translated = "";
		}
		elsif ($inEnglish eq "y") {
			$translated = "";
		}
		elsif ($inEnglish eq "n") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to create %s, please try to do so manually later.") {
			$translated = "";
		}
		elsif ($inEnglish eq "shortcut button") {
			$translated = "";
		}
		elsif ($inEnglish eq "the panel") {
			$translated = "";
		}
		elsif ($inEnglish eq "%s installed successfully.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Do you wish to read %s? [y/n]? ") {
			$translated = "";
		}
		elsif ($inEnglish eq "the readme file") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to copy '%s' to '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to read from '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to write to '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Installer") {
			$translated = "";
		}
		elsif ($inEnglish eq "Refreshing the desktop") {
			$translated = "";
		}
		elsif ($inEnglish eq "Refreshing panel menus and buttons (this could take a few seconds)") {
			$translated = "";
		}
		# '\"' in the following string means a single quote. These
		# must pair.
		elsif ($inEnglish eq "Press the '\"'enter'\"' key to close the window...") {
			$translated = "";
		}
		elsif ($inEnglish eq "This installer will install %s on your system.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Warning: The KDE Installation SyStem (kiss) is not installed on your computer.\n" . "You will not be able to uninstall %s once it is installed!") {
			$translated = "";
		}
		elsif ($inEnglish eq "An older version of %s is installed on your system.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Version %s of %s is already installed on your system.") {
			$translated = "";
		}
		elsif ($inEnglish eq "A newer version of %s is already installed on your system.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Installing will delete that version from your computer.\n" . "It will not be possible to restore it later!") {
			$translated = "";
		}
		elsif ($inEnglish eq "Do you wish to continue? [y/n]? ") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to make directory '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Failed to extract archive.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Unable to delete '%s'.") {
			$translated = "";
		}
		elsif ($inEnglish eq "Please wait while the installer prepares the KDE Installation SyStem wizard\n". "which will guide you through the rest of the installation process...") {
			$translated = "";
		}
	}
	# Translation template end.
	# I18N Ends here.

	return sprintf($translated, @subStrings);
}

__END__
EOF
# Print ends

unless (open (UUENCODE, "uuencode $origfile $encodename |")) {
	print "error executing uuencode.\n";
	close SCRIPT;
	(0xffff & system("rm -rf $tmpdir $scriptname")) and die "can't delete directory $tmpdir or can't delete file $scriptname";
	exit 1;
}

print SCRIPT <UUENCODE>;	
close UUENCODE;

if ($?) {
	print "uuencode failed.\n";
	close SCRIPT;
	(0xffff & system("rm -rf $tmpdir $scriptname")) and die "can't delete directory $tmpdir or can't delete file $scriptname";
	exit 1;
}

close SCRIPT;
unless (chmod 0755, $scriptname) {
	print "Could not make $scriptname executable.\n";
	(0xffff & system("rm -rf $tmpdir $scriptname")) and die "can't delete directory $tmpdir or can't delete file $scriptname";
	exit 1;
}

print "done.\n";
print "Deleting temporary directory...";
(0xffff & system("rm -rf $tmpdir")) and die "can't delete directory $tmpdir";
print "done.\n";

}
$| = 0;
exit $exitcode;
