#! /usr/bin/perl

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

my $lexmarkdir = '/usr/local/lexmark/';
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';

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

# Ask the user which printer model he has

print "\n";
print "Lexmark inkjet printer maintainance 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 $model = "";

do {
  print "\n";
  print "Currently the following Lexmark printers are supported\n";
  print "\n";
  print "     1  Lexmark Z13\n";
  print "     2  Lexmark Z22 or Z32\n";
  print "     3  Lexmark Z52\n";
  print "     4  Lexmark Z23 or Z33\n";
  print "     5  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 <= 5)) {
      if ($number == 1) {$model = "z13";} 
      elsif ($number == 2) {$model = "z32";} 
      elsif ($number == 3) {$model = "z52";} 
      elsif ($number == 4) {$model = "z33";} 
      else {$model = "z53";}
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until ($model ne "");

my $connection = "";

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 te port your printer is connected on\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) {
	$number --;
        $connection = "/dev/lp$number";
      } else {
	$number -= 4;
        $connection = "/dev/usb/lp$number";
      }
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until ($connection ne "");

if (($model eq "z32") || ($model eq "z52") || ($model eq "z53")) {

do {
  print "\n";
  print "You have the following options now:\n";
  print "\n";
  print "     1  Print head alignment page for Colour + Black Cartridges\n";
  print "     2  Print head alignment page for Colour + Photo Cartridges\n";
  print "     3  Print nozzle cleaning page for Colour + Black Cartridges\n";
  print "     4  Print nozzle cleaning page for Colour + Photo Cartridges\n";
  print "     5  Print Lexmark test page (all cartridge types)\n";
  print "\n";
  print "     0  Quit\n";
  print "\n";
  print "Please enter the number of your choice.\n";
  print "\n";

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

  if ( $input =~ m/^\s*(\d+)\D*/ ) {
    my $number = $1;
    if (($number >= 0) && ($number <= 5)) {
      if ($number == 0) {
	exit(0);
      } elsif ($number == 1) {
        print "\n";
        print "Printing head alignment page for Colour + Black Cartridges...\n";
        print "
You will see four or six series of lines (A-D on the Z22 or Z32, A-F
on the Z52 or Z53). The lines are more or less interrupted. Choose the
most straight line from each series and mark its number. Now enter a
command line as follows:

   foomatic-configure -n <printer> -o AlignA=<A> ... -o AlignF=<F>

where <A> to <F> are the numbers of the most straight lines. If you use 
CUPS as printing system you can also use the Printer Manager in
the Control Center of KDE 2.2.x or newer, \"kups\", \"xpp\", or
\"qtcups\", if you use PDQ you can use \"xpdq\", and under Mandrake
Linux 8.1 or newer you can use \"printerdrake\" as a graphical
interface to do the alignment settings.
The settings of E and F do not matter when you have no lines for them
on the test page. Repeat this procedure whenever you change a
cartridge.\n";

        if (system "$cat $lexmarkdir$model/lx??algn.out > $connection") {
	    die "Cannot print head alignment page.";
        }
      } elsif ($number == 2) {
        print "\n";
        print "Printing head alignment page for Colour + Photo Cartridges...\n";
        print "
You will see four or six series of lines (A, B on the Z22 or Z32, A-D
on the Z52 or Z53). The lines are more or less interrupted. Choose the 
most straight line from each series and mark its number. Now enter a
command line as follows:

   foomatic-configure -n <printer> -o AlignA=<A> ... -o AlignD=<D>

where <A> to <F> are the numbers of the most straight lines. If you use 
CUPS as printing system you can also use the Printer Manager in
the Control Center of KDE 2.2.x or newer, \"kups\", \"xpp\", or
\"qtcups\", if you use PDQ you can use\"xpdq\", and under Mandrake 
Linux 8.1 or newer you can use \"printerdrake\" as a graphical 
interface to do the alignment settings.
The settings of C, D, E, F do not matter when you have no lines for
them on the test page. Repeat this procedure whenever you change a
cartridge.\n";

        if (system "$cat $lexmarkdir$model/lx??phal.out > $connection") {
	    die "Cannot print head alignment page.";
        }
      } elsif ($number == 3) {
        print "\n";
        print "Printing nozzle cleaning page for Colour + Black Cartridges...\n";
        print "\n";
        print "This page prints solid bars using all the nozzles of the cartridges to get\n";
        print "very much ink through the nozzles to clean them. Before and after the bars\n";
        print "you see diagonal lines to check the effect of the cleaning. When they are\n";
        print "not interrupted, all nozzles are clean.\n";
        print "Print this page up to three times to clean your nozzles. If there are still\n";
        print "some nozzles clogged, take out the cartridge and clean the nozzles and the\n";
        print "contacts with toilet paper or paper towels. Do not touch the contacts or\n";
        print "nozzles with your fingers. Do a head alignment (points 1 and 2 of the main\n";
        print "menu of this program) after putting the cartridge back in.\n";
        if (system "$cat $lexmarkdir$model/lx??cln.out > $connection") {
	    die "Cannot print nozzle cleaning page.";
        }
      } elsif ($number == 4) {
        print "\n";
        print "Printing nozzle cleaning page for Colour + Photo Cartridges...\n";
        print "\n";
        print "This page prints solid bars using all the nozzles of the cartridges to get\n";
        print "very much ink through the nozzles to clean them. Before and after the bars\n";
        print "you see diagonal lines to check the effect of the cleaning. When they are\n";
        print "not interrupted, all nozzles are clean.\n";
        print "Print this page up to three times to clean your nozzles. If there are still\n";
        print "some nozzles clogged, take out the cartridge and clean the nozzles and the\n";
        print "contacts with toilet paper or paper towels. Do not touch the contacts or\n";
        print "nozzles with your fingers. Do a head alignment (points 1 and 2 of the main\n";
        print "menu of this program) after putting the cartridge back in.\n";
        if (system "$cat $lexmarkdir$model/lx??phcl.out > $connection") {
	    die "Cannot print nozzle cleaning page.";
        }
      } elsif ($number == 5) {
        print "\n";
        print "Printing Lexmark test page ...\n";
        print "\n";
        print "This is just a test image. You can print it with a colour and a black cartridge\n";
        print "or with a colour and a photo cartridge.\n";
        if (system "$cat $lexmarkdir$model/lx??tstc.out > $connection") {
	    die "Cannot print test page.";
        }
      }
      print "\n";
      print "Press <Enter> to get back to the main menu.";
      my $input = <STDIN>;
      print "\n";
      print "\n";
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until (0);

} elsif ($model eq "z33") {

do {
  print "\n";
  print "You have the following options now:\n";
  print "\n";
  print "     1  Print head alignment page for Colour + Black Cartridges\n";
  print "     2  Print head alignment page for only the Colour Cartridge\n";
  print "     3  Print head alignment page for only the Black Cartridge\n";
  print "     4  Print nozzle cleaning page\n";
  print "\n";
  print "     0  Quit\n";
  print "\n";
  print "Please enter the number of your choice.\n";
  print "\n";

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

  if ( $input =~ m/^\s*(\d+)\D*/ ) {
    my $number = $1;
    if (($number >= 0) && ($number <= 4)) {
      if ($number == 0) {
	exit(0);
      } elsif (($number == 1) || ($number == 2) || ($number == 3)) {
        print "\n";
        print "Printing head alignment page...\n";
        print "\n";
        print "
You will see several lines of arrows marked with letters (A-F), their
number depends on the cartridges you are using. The arrows appear more
or less dark, choose the darkest arrow in each line. Now enter a
command line as follows:

   foomatic-configure -n <printer> -o AlignA=<A> ... -o AlignF=<F>

where <A> to <F> are the numbers of the darkest arrows. If you use
CUPS as printing system you can also use the Printer Manager in the
Control Center of KDE 2.2.x or newer, \"kups\", \"xpp\", or \"qtcups\",
if you use PDQ you can use \"xpdq\", and under Mandrake Linux 8.1 or
newer you can use \"printerdrake\" as a graphical interface to do the
alignment settings.
The settings of C, D, E, F do not matter when you have no lines for
them on the test page. Repeat this procedure whenever you change a
cartridge.\n";
	my $pagename;
	if ($number == 1) {
	    $pagename = "lxaialgn.out";
	} elsif ($number == 2) {
	    $pagename = "lxaicaln.out";
	} elsif ($number == 3) {
	    $pagename = "lxaikaln.out";
	}
        if (system "$cat $lexmarkdir$model/$pagename > $connection") {
	    die "Cannot print head alignment page.";
        }
      } elsif ($number == 4) {
        print "\n";
        print "Printing nozzle cleaning page...\n";
        print "\n";
        print "This page prints solid bars using all the nozzles of the cartridges to get\n";
        print "very much ink through the nozzles to clean them. Before and after the bars\n";
        print "you see diagonal lines to check the effect of the cleaning. When they are\n";
        print "not interrupted, all nozzles are clean.\n";
        print "Print this page up to three times to clean your nozzles. If there are still\n";
        print "some nozzles clogged, take out the cartridge and clean the nozzles and the\n";
        print "contacts with toilet paper or paper towels. Do not touch the contacts or\n";
        print "nozzles with your fingers. Do a head alignment (points 1 and 2 of the main\n";
        print "menu of this program) after putting the cartridge back in.\n";
        if (system "$cat $lexmarkdir$model/lx??cln.out > $connection") {
	    die "Cannot print nozzle cleaning page.";
        }
      }
      print "\n";
      print "Press <Enter> to get back to the main menu.";
      my $input = <STDIN>;
      print "\n";
      print "\n";
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until (0);

} elsif ($model eq "z13") {

do {
  print "\n";
  print "You have the following options now:\n";
  print "\n";
  print "     1  Print head alignment page for the Colour Cartridge\n";
  print "     2  Print head alignment page for the Black Cartridge\n";
  print "     3  Print nozzle cleaning page for the Colour Cartridge\n";
  print "     4  Print nozzle cleaning page for the Black Cartridge\n";
  print "\n";
  print "     0  Quit\n";
  print "\n";
  print "Please enter the number of your choice.\n";
  print "\n";

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

  if ( $input =~ m/^\s*(\d+)\D*/ ) {
    my $number = $1;
    if (($number >= 0) && ($number <= 4)) {
      if ($number == 0) {
	exit(0);
      } elsif (($number == 1) || ($number == 2)) {
        print "\n";
        print "Printing head alignment page...\n";
        print "\n";
        print "
You will see several lines of arrows marked with letters (A-F), their
number depends on the cartridges you are using. The arrows appear more
or less dark, choose the darkest arrow in each line. Now enter a
command line as follows:

   foomatic-configure -n <printer> -o AlignA=<A> ... -o AlignF=<F>

where <A> to <F> are the numbers of the darkest arrows. If you use
CUPS as printing system you can also use the Printer Manager in the
Control Center of KDE 2.2.x or newer, \"kups\", \"xpp\", or \"qtcups\",
if you use PDQ you can use \"xpdq\", and under Mandrake Linux 8.1 or
newer you can use \"printerdrake\" as a graphical interface to do the
alignment settings.
The settings of C, D, E, F do not matter when you have no lines for
them on the test page. Repeat this procedure whenever you change a
cartridge.\n";
	my $pagename;
	if ($number == 1) {
	    $pagename = "lxajcaln.out";
	} elsif ($number == 2) {
	    $pagename = "lxajkaln.out";
	}
        if (system "$cat $lexmarkdir$model/$pagename > $connection") {
	    die "Cannot print head alignment page.";
        }
      } elsif (($number == 3) || ($number == 4)) {
        print "\n";
        print "Printing nozzle cleaning page...\n";
        print "\n";
        print "This page prints solid bars using all the nozzles of the cartridges to get\n";
        print "very much ink through the nozzles to clean them. Before and after the bars\n";
        print "you see diagonal lines to check the effect of the cleaning. When they are\n";
        print "not interrupted, all nozzles are clean.\n";
        print "Print this page up to three times to clean your nozzles. If there are still\n";
        print "some nozzles clogged, take out the cartridge and clean the nozzles and the\n";
        print "contacts with toilet paper or paper towels. Do not touch the contacts or\n";
        print "nozzles with your fingers. Do a head alignment (points 1 and 2 of the main\n";
        print "menu of this program) after putting the cartridge back in.\n";
	my $pagename;
	if ($number == 3) {
	    $pagename = "lxajccln.out";
	} elsif ($number == 4) {
	    $pagename = "lxajkcln.out";
	}
        if (system "$cat $lexmarkdir$model/$pagename > $connection") {
	    die "Cannot print nozzle cleaning page.";
        }
      }
      print "\n";
      print "Press <Enter> to get back to the main menu.";
      my $input = <STDIN>;
      print "\n";
      print "\n";
    }
  } else {
    print "\nWrong input, try again!\n";
  }
} until (0);

}
