#! /usr/bin/perl

#  === ADAPT THIS TO FIT TO YOUR ENVIRONMENT ===============================

my $lexmarkpath = '/usr/local/lexmark';
my $nulldevice = '/dev/null';
my $grep = '/bin/grep';
my $cat = '/bin/cat';
my $cut = '/usr/bin/cut';
my $head = '/usr/bin/head';
my $tail = '/usr/bin/tail';
my $wc = '/usr/bin/wc';
my $ls = '/bin/ls';
my $config = '/usr/bin/foomatic-configure';

# =========================================================================

# Auto-correct the path for "foomatic-configure"

if (! -x $config) {
    $config = '/usr/local/bin/foomatic-configure';
} elsif (! -x $config) {
    $config = '/opt/foomatic/bin/foomatic-configure';
}

# Check whether the Lexmark driver package is installed

if (! -d $lexmarkpath) {
  print "\n";
  print "Lexmark inkjet printer installation program\n";
  print "-------------------------------------------\n";
  print "\n";
  print "(C) 2001 by Till Kamppeter\n";
  print "Free software under the terms of the GNU General Public License (GPL)\n";
  print "\n";

  print "To install your Lexmark Z13, Z22, Z23, Z32, Z33, Z52 or Z53 on this system\n";
  print "you need to install the drivers provided by Lexmark at first. Please download\n";
  print "them from the Lexmark home page (http://www.lexmark.com/)\n";
  print "If you have a Z22 use the Z32 driver, these two printers\n";
  print "are the same hardware. The Z23 uses the driver for the Z33. You do not need\n";
  print "to install LPD for installing the Lexmark drivers, just use the printing\n";
  print "system which is already installed. When a window shows up during the\n";
  print "the installation of the Lexmark driver, close it. If you get an error\n";
  print "message in the end of the installation of the Lexmark driver, ignore it.\n";
  print "Start this program again afterwards to configure your print.\n";
  exit(0);
}

system("chmod -R 0755 $lexmarkpath");

# Ask the user which printer model he has

print "\n";
print "Lexmark inkjet printer installation program\n";
print "-------------------------------------------\n";
print "\n";
print "(C) 2001 by Till Kamppeter\n";
print "Free software under the terms of the GNU General Public License (GPL)\n";

my $driver = "";
my $id = "";
my $printertype = 0; # 1: Z22, Z32, Z52, Z53;  2: Z13, Z23, Z33

do {
  print "\n";
  print "Currently the following Lexmark printers are supported\n";
  print "\n";
  print "     1 Lexmark Z13\n";
  print "     2 Lexmark Z22\n";
  print "     3 Lexmark Z23\n";
  print "     4 Lexmark Z32\n";
  print "     5 Lexmark Z33\n";
  print "     6 Lexmark Z52\n";
  print "     7 Lexmark Z53\n";
  print "\n";
  print "Please enter the number of your printer and make sure, that it is\n";
  print "connected to your computer and turned on.\n";
  print "\n";

  print "Number: ";
  my $input = <STDIN>;

  if ( $input =~ m/^\s*(\d+)\D*/ ) {
    my $number = $1;
    if (($number > 0) && ($number <= 7)) {
      $driver = "lexmarkinkjet";
      if ($number == 1) {
	$id =  "Lexmark-Z13";
	$printertype = 2;
      } elsif ($number == 2) {
	$id =  "327401";
	$printertype = 1;
      } elsif ($number == 3) {
	$id =  "Lexmark-Z23";
	$printertype = 2;
      } elsif ($number == 4) {
	$id =  "317129";
	$printertype = 1;
      } elsif ($number == 5) {
	$id =  "Lexmark-Z33";
	$printertype = 2;
      } elsif ($number == 6) {
	$id =  "328553";
	$printertype = 1;
      } elsif ($number == 7) {
	$id =  "Lexmark-Z53";
	$printertype = 1;
      }
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until ($driver ne "");

my $connection = "";
my $port = "";

do {
  print "\n";
  print "The following ports to connect your printer are supported:\n";
  print "\n";
  print "     1  First parallel port (/dev/lp0)\n";
  print "     2  Second parallel port (/dev/lp1)\n";
  print "     3  Third parallel port (/dev/lp2)\n";
  print "     4  First printer on the USB (/dev/usb/lp0)\n";
  print "     5  Second printer on the USB (/dev/usb/lp1)\n";
  print "     6  Third printer on the USB (/dev/usb/lp2)\n";
  print "\n";
  print "Please enter the number of the port your printer is connected to\n";
  print "\n";

  print "Number: ";
  my $input = <STDIN>;

  if ( $input =~ m/^\s*(\d+)\D*/ ) {
    my $number = $1;
    if (($number > 0) && ($number <= 6)) {
      if ($number < 4) {
	$port = "ParPort$number";
	$number --;
        $connection = "/dev/lp$number";
      } else {
	$number -= 3;
	$port = "USB$number";
	$number --;
        $connection = "/dev/usb/lp$number";
      }
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until ($connection ne "");

my $ink = "";

if ($printertype == 1) { # Z32, Z33, Z52, Z53
    do {
	print "\n";
	print "Which type of cartridges have you installed?\n";
	print "\n";
	print "     1  Black cartridge only\n";
	print "     2  Black cartridge and colour cartridge\n";
	print "     3  Photo cartridge and colour cartridge\n";
	print "\n";
	print "Please enter the number of your cartridge configuration.\n";
	print "\n";
	
	print "Number: ";
	my $input = <STDIN>;
	
	if ( $input =~ m/^\s*(\d+)\D*/ ) {
	    my $number = $1;
	    if (($number > 0) && ($number <= 3)) {
		if ($number == 1) {
		    $ink = "Monochrome";
		} elsif ($number == 2) {
		    $ink = "Colour";
		} elsif ($number == 3) {
		    $ink = "Photo";
		}
	    }
	} else {
	    print "\nWrong input, try again!\n";
	}
    } until ($ink ne "");
} else { # Z13, Z23, Z33
    do {
	print "\n";
	print "Which type of cartridges have you installed?\n";
	print "\n";
	print "     1  Black cartridge only\n";
	print "     2  Colour cartridge only\n";
	if ($id ne "Lexmark-Z13") {
	    print "     3  Black cartridge and colour cartridge\n";
	}
	print "\n";
	print "Please enter the number of your cartridge configuration.\n";
	print "\n";
	
	print "Number: ";
	my $input = <STDIN>;
	
	if ( $input =~ m/^\s*(\d+)\D*/ ) {
	    my $number = $1;
	    my $max = 3;
	    $max = 2 if ($id eq "Lexmark-Z13");
	    if (($number > 0) && ($number <= $max)) {
		if ($number == 1) {
		    $ink = "Monochrome";
		} elsif ($number == 2) {
		    $ink = "Colour3";
		} elsif ($number == 3) {
		    $ink = "Colour";
		}
	    }
	} else {
	    print "\nWrong input, try again!\n";
	}
    } until ($ink ne "");
}

my $printer = "";

do {
  print "\n";
  print "Please enter a name for your printer: ";
  my $input = <STDIN>;
  if ( $input =~ m/^\s*(\w+)\s*$/ ) {
    $printer = $1;
  } else {
    print "\nA printer name can only contain letters, digits, or underscores, try again!\n";
  }
} until ($printer ne "");

print "\nInstalling your printer under the name $printer ...\n";

if (system "$config -n $printer -c file:$nulldevice -o Port=$port -d $driver -p $id -o Mode=$ink") {
  die "Could not install your printer with Foomatic!";
}

print "\nYour printer is successfully installed\n\n";
