#!/usr/bin/perl

$printcap="/etc/printcap";
$filters="/usr/doc/examples/magicfilter/filters/filters.tar.gz";
$filterdir="/usr/sbin";

$force=($ARGV[0] = "--force");

sub input
{
  local($message)=$_[0];
  local($defanswer)=$_[1];

  printf "%-39s [%-20s]: ",$message,$defanswer; 
  $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; }
 
  return $reply;
}

sub yninput
{
  local($message)=$_[0];
  local($defanswer)=$_[1];

  printf "%s? (y/n) [%s]: ",$message,$defanswer; 
  $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; }
 
  return $reply;
}

sub readfilters
{
  if ( ! -e $filters )
  {
  print<<EOT;

The magicfilter package is wrongly installed; can't find $filters.

EOT

    die "'$filters' missing, stopped";
  }
  
  open(FILTERS, "tar tzf $filters|") || die "Can't find $filters.";
  $i=0;
  while (<FILTERS>)
  {
    chop;
    s/(.*)-filter/$1/g;
    $filters[$i]=$_; $i++;
  }
  close(FILTERS);

  foreach $entry (@filters) { $entry =~ s/-filter//g; }
  return sort(@filters);
}

if ( -e $printcap && !$force) 
{
  print<<EOT;

You already have an $printcap file installed.
I assume that it is already correctly configured and won't change anything
(if $printcap is not correctly configured, delete the file and re-run 
 $0).

EOT
  exit 0;
} 

@filters=&readfilters;

$full[0]='HP Laserjet 4L'; $short[0]='hplj4l'; $alias[0]='lj';
$dev[0]='lp1'; $ifilter[0]='ljet4m';
$ok="no"; $m=-1; $d=0;
while ($ok !~ /ye*s*/)
{
  print<<EOT;
  
You will have to supply the following items for each printer to be installed
(example values in parentheses):

 - it's short name, it's full name and the alias (hpjlet4, HP LaserJet 4,lj)
 - the device where the printer is attached      (/dev/lp1)
 - and the input filter you want to use          (ljet4m)

EOT

  $p=-1;
  do
  {
    $p++; $m++; $full[$m+1]='done';

    printf "Printer #%d:\n", $p+1;
    printf "------------\n";

       $full[$p]=&input("Full name, `done' when done", $full[$p]);
 
    if ($full[$p] !~ /done\s*/)
    { 
      $short[$p]=&input("Short name, name of the spool directory",$short[$p]);
      $alias[$p]=&input("Alias                                  ",$alias[$p]);
      $dev[$p]  =&input("Device (without leading /dev/)         ",$dev[$p]);

      if ($#filters == 0)
      {
        print "There aren't any filters."; $full[$p]='done';
      }
      else
      {
        print "\nThe following filters are available:\n\n";
	$l=0;
	foreach $filter (@filters)
	{
	  $l += (length($filter)+1);
	  if ($l > 72-length($filter)) { print "\n"; $l=0; }
	  print "$filter ";
	}
        print "\n\n";

    $ifilter[$p]=&input("The input filter to be used", $ifilter[$p]);

        if ($dev[$p] !~ /dev/) { $dev[$p]="/dev/$dev[$p]" }
        $spooldir[$p]="/var/spool/lpd/$short[$p]";

        print "\n";
      }
    }
  }
  while ($full[$p] !~ /done\s*/);

  if ($p > 0)
  {
    if ($p == 1) { $d=0; }
    do
    {
      format top=

                       Printer configuration

Default printer (alias `lp') is marked with an asterisk.

Printer name  Short name Alias Spool dir            Device   Input filter
------------------------------------------------------------------------------
.

    format STDOUT=
@<<<<<<<<<<<< @<<<<<<<<< @<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<@<
$full,        $short,    $alias,$spooldir,          $dev,    $ifilter,       $a
.

      for($i=0;$i<$p;$i++)
      {
        $full=$full[$i]; $short=$short[$i]; $alias=$alias[$i];
        $spooldir=$spooldir[$i]; $dev=$dev[$i]; $ifilter=$ifilter[$i];
        $a= ($i == $d) ? '*' : ' ';

        write;
      }
      $ans="n"; $-=0;

      print "\n";
      if ($p > 1)
      {
        $ans=&yninput("Do you want to change the default printer","n");
        if ($ans =~ /ye*s*/)
        {
          $def=&input("New default printer (type it's alias)","");
          $d=0;
          while (($d < $p) && ($alias[$d] !~ /$def/)) { $d++; }
          if ($d == $p) { $d = 0; }
        }
      }
    }
    while ($ans eq "y");
    $ok=&yninput("Is this ok","y");
  }
  else
  {
    $ok="yes";
  } 
}

print "\n";
if ($p > 0)
{
  open(PRINTCAP,">$printcap") || die "Can't write $printcap: $!";
  print PRINTCAP <<EOH;
#
# Copyright (c) 1983 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that this notice is preserved and that due credit is given
# to the University of California at Berkeley. The name of the University
# may not be used to endorse or promote products derived from this
# software without specific prior written permission. This software
# is provided ``as is'' without express or implied warranty.
#
#	@(#)etc.printcap	5.2 (Berkeley) 5/5/88
#
# > This file was generated by $0. <
#
EOH

  $alias[$d]="lp|$alias[$d]";
  for($i=0;$i<$p;$i++)
  {
    print PRINTCAP <<"EOE";
$alias[$i]|$short[$i]|$full[$i]:\\
\t:lp=$dev[$i]:sd=$spooldir[$i]:\\
\t:sh:pw#80:pl#72:px#1440:mx#0:\\
\t:if=/usr/sbin/$ifilter[$i]-filter:\\
\t:af=/var/log/lp-acct:lf=/var/log/lp-errs:
EOE
  }
  close(PRINTCAP);

  for($i=0;$i<$p;$i++)
  {
    $ifilter[$i]="$ifilter[$i]-filter";
    if ( grep($ifilter[$i], @filters) )
    {
      chdir($filterdir); system("tar xzf $filters $ifilter[$i]");
    }
    else
    {
      print "$ifilter[$i] does not exist, assuming you'll write your own.\n";
    }

    $lpgrp=(getpwnam("lp"))[2];
    if ( ! -e $spooldir[$i] )
    {
      $ans=&yninput("Spool directory doesn't exist; create $spooldir[$i]","y");
      if ($ans eq "y") 
      { 
        umask 002; mkdir($spooldir[$i],02775); chown(0,$lpgrp,$spooldir[$i]);
        
      }
    }
  }

  print<<EOT;
$printcap generated.

You'll probable want to finetune your newly created $printcap file.
Read the printcap(5), lpr(1), lpq(1), lprm(1) and lpc(1) manual pages
and the PRINTING-HOWTO before doing this.

EOT
}
else
{
  print<<EOT;

Nothing to install.
You're going to write your $printcap file yourself.

EOT
}

exit 0;
