#! /usr/bin/perl

my $lexmarkdir = '/usr/local/lexmark/';
my $lexmarkdevice = '/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 $perl = '/usr/bin/perl';
my $config = '/usr/bin/foomatic-configure';

# Check whether the Lexmark driver package is installed

if (! -d $lexmarkdir) {
  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 Z22, Z23, Z32, Z33, Z52 or Z53 on this system, you\n";
  print "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);
}

# 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 = "";

do {
  print "\n";
  print "Currently the following Lexmark printers are supported\n";
  print "\n";
  print "     1 Lexmark Z22\n";
  print "     2 Lexmark Z23\n";
  print "     3 Lexmark Z32\n";
  print "     4 Lexmark Z33\n";
  print "     5 Lexmark Z52\n";
  print "     6 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 <= 6)) {
      $driver = "lexmarkinkjet";
      if ($number == 1) {
	$id =  "327401";
      } elsif ($number == 2) {
	$id =  "Lexmark-Z23";
      } elsif ($number == 3) {
	$id =  "317129";
      } elsif ($number == 4) {
	$id =  "Lexmark-Z33";
      } elsif ($number == 5) {
	$id =  "328553";
      } elsif ($number == 6) {
	$id =  "Lexmark-Z52";
      }
    }
  } 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 $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:$lexmarkdevice -o Port=$port -d $driver -p $id") {
  die "Could not install your printer with Foomatic!";
}

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