#!/bin/sh
# -----------------------------------------------------------------------------
#
#  Printer installation / uninstallation script for UNIX environments
#
#  axinstall is an attempt to write a general script to install axis
#  print servers, actually the connected printer, in Unix environments.
#  If possible the script is using the commandline tools for this purpose
#  that is included in the different UNIX distributions.
#
#  A common requirement for this to work is that the user must have proper
#  permission on the system. Further normally the print server must be known
#  to the system where it is installed, e.g. IP-adderess or DNS name.
#  
# (C) Copyright 1998-, Axis Communications AB, LUND, SWEDEN
#
# -----------------------------------------------------------------------------
REVISION="lprng Mon Nov 24 15:05:40 CET 2003"
FILENAME="axinstall"
CAT_NAME="axis"
PROD_TYPE="Network Print Server"
ARCHIVE=1

# --- Make sure that the programs needed are in these paths
PATH=${PATH}:/bin/
PATH=${PATH}:/usr/bin/:/usr/sbin/:/usr/bsd/:/usr/ucb:/usr/lib/:/usr/local/bin
PATH=${PATH}:/usr/etc:/etc:/etc/sbin

FILE_NAME=`basename $0`

# Make changes here if you want to change any of the default values

# PROSHOME is where filters are put, both pros and ftp
DEF_PROSHOME=/usr/local/lib/$CAT_NAME
DEF_PROSHOME_AIX=/usr/lpd/$CAT_NAME

DEF_PRINTCAP=/etc/printcap
DEF_MODELDIR=/usr/lib/lp/model
DEF_HP_LPDMODELDIR="/etc/lp/interface/netlp.asx"
DEF_SPOOLDIR_EVAL='echo /var/spool/lpd/${PRINTER}'
DEF_PROSDEV_EVAL='echo /dev/${PRINTER}'
DEF_LOGFILE_EVAL='echo $SPOOLDIR/log'
DEF_PROSLOG_EVAL='echo $PROSHOME/prosd-${PRINTER}.log'
DEF_FTP_FILE_EVAL='echo ftp_${PRINTER}'
DEF_PROSB_FILE_EVAL='echo pros_${PRINTER}'
DEF_AIX_COLONDIR=/usr/lpd/pio/predef/

AIX_LOG_FILE=1
AIX_LOG_MAIL=2
DEF_AIX_LOG_MODE=$AIX_LOG_MAIL

clear=clear
mknod=mknod
cut=cut
tee=tee
ftp=ftp

HOSTNAME=
test -f /bin/uname        && HOSTNAME=`/bin/uname -n`
test -f /usr/bin/hostname && HOSTNAME=`/usr/bin/hostname`
test -f /bin/hostname     && HOSTNAME=`/bin/hostname`
test -f /usr/bin/uname    && HOSTNAME=`/usr/bin/uname -n`

#if test $0 != ${0##/*/}; then
#    source_path='echo $0 | sed -e "s:${0##/*/}::g"'
#else
#    source_path=${pwd}
#fi
if test "X${HOME}" != "X"; then
    if test "${HOME}" = "/"; then
	axinstallrc=/.axinstallrc
    else
	axinstallrc=${HOME}/.axinstallrc
    fi
fi

# ============================================================================
#                                Common Code 
# ============================================================================

recursive=
trace=
debug=
suggest_name=NO
advanced=NO
while test -n "$1"; do
  case $1 in
     -recursive)    recursive=YES ;;
     -trace)        trace='-trace' ;;
     -debug)        debug='-debug' ;;
     -suggest_name) suggest_name=YES ;;
     -advanced)     advanced=YES ;;
     -h|-help)   
        cat <<HELP_EOF

${FILE_NAME} - Interactive program to install or uninstall ${PROD_TYPE}s
 
Usage:      ${FILE_NAME} [-trace] [-h]
Options:
  -trace          Make a trace file that can be sent to support if there is any
                  problem when running the script

  -advanced       Ask more detailed questions about installation.
                  By default, many questions are supressed in the 
                  first round of setup to make quick installation easy.

  -suggest_name   Suggest a printer name based on host name and print method 

  -h,-help        Print this message.

HELP_EOF

        exit 
        ;;
     *)          echo "${FILE_NAME}: unknown command '$1'"  ;;
  esac
  shift
done

# This has to run as early as possible, it changes the shell.
# --- Start other shells only if we have no cmd line arguments
# --- New shells started should add an argument to cmd line
if test "X${recursive}" != "XYES"; then

   run_shell=
   # --- If we do have a /bin/sh5 then use that instead 
   test -f /bin/sh5 && run_shell=/bin/sh5 

   # --- If we're running HP-UX, use ksh instead
   uname -sr | grep "HP-UX" >/dev/null
   test $? = 0 && test -f /bin/ksh && run_shell=/bin/ksh 

   if test -n "${trace}"; then
      # Save trace data in a file in /tmp/ and preserve a bareable interface
      # to user by removing lines from output that starts with a '+'
      test -z "${run_shell}" && run_shell=sh
      exec ${run_shell} -x $0 -recursive $trace $debug 2>&1 | \
           tee -a /tmp/${FILE_NAME}_$$.trace | grep -v '^+'
      echo
      echo "Trace file saved in /tmp/${FILE_NAME}_$$.trace"
      echo
      exit 0
   elif test -n "${run_shell}"; then
      exec ${run_shell} $0 -recursive  
      exit 0
   fi
fi

# trap ^C and exit clean
trap 'myexit 0 "Exiting"' 2

# No clear when debug
test -n "$debug" -o -n "$trace" && clear=

# --- cleanup ----------------------------------------------------------------
# --- Clean up things created by initilize
cleanup()
{
   rm -rf $TMPDIR
} # --- End of cleanup


# --- myecho $1 $2 $3 ... ----------------------------------------------------
# --- Echo text with or without newline at end
# --- $1 - optional '-n' to skip newline at end
# --- $* - rest of options passed to echo
echo_n=NO
test X`echo -n` = X && echo_n=YES
myecho()
{
   if test X"$1" = "X-n"; then
      shift
      test $echo_n = YES && echo -n "$*"
      test $echo_n = NO  && echo "$*" '\c'
   else
      echo "$*"
   fi
} # --- End of myecho

# --- lecho $* ---------------------------------------------------------------
# --- Log text and echo to screen
lecho ()
{
   myecho $* | tee -a $instalog
} # --- lecho

# --- logg $* ----------------------------------------------------------------
# --- Put message in log file
logg ()
{
   myecho $* >> $instalog
} # --- logg

# --- strip $1 ---------------------------------------------------------------
# --- Strip spaces from beginning and end of string $1 
# --- Result on standard out; use $a=`strip $b`
# --- Do not add "" around $1, it is because of that it is without
# --- these that the string is stripped.
strip()
{
   echo $1
} # --- End of strip

# --- myexit $1 [$2] --------------------------------------------------------
# --- Exit with an optional message and cleanup files.
# --- The string "Error:" is printed if error code is other than 0
# --- $1 - error code
# --- $2 - optional message
myexit()
{
   if test $# = 2; then
      if test "$1" != "0"; then
         lecho -n "$FILE_NAME: Error: "
      fi
      lecho "$2"
   fi
   cleanup

   if test -n "$instalog" -a -f "$instalog"; then
      echo "A log file is saved in the file $instalog."
   fi

   exit "$1"
} # --- End of myexit

# --- answerP $1 $2 --------------------------------------------------------
# --- Simple prompt with default for a text string answer.
# --- $1 is prompt string.
# --- $2 is default answer.
answerP()
{
   answer=
   if test "X$2" !=  "X"; then
      myecho -n "$1 (default $2): "; read answer
   else
      myecho -n "$1":; read answer
   fi
   test "X$answer" = "X" && answer=$2
}

# --- yes_or_no $1 $2 [$3] --------------------------------------------------
# --- Prompt with default for a yes or no answer.
# --- Leave answer in yes_or_no.
# --- $1 is prompt string.
# --- $2 is default answer (yes/no).
# --- $3 is optional, extra option, can be used for abort
yes_or_noP()
{
   yes_or_no=
   while true; do
      extra_opt=
      test $# = 3 && extra_opt="$3"
      answerP "$1 [yn${extra_opt}] ?" "$2"
      yes_or_no=$answer

      test "X$yes_or_no" = "X" && yes_or_no=$2

      case $yes_or_no in
      y | Y | yes | Yes | YES)
         yes_or_no=YES
         break # out of the while loop
         ;;
      n | N | no | No | NO)
         yes_or_no=NO
         break # out of the while loop
         ;;
      esac

      # Do we match the extra option then?
      if test $# -eq 3 && test "X$yes_or_no" = "X$3"; then
         break
      fi
   done
}

# --- range_answer $1 $2 [$3] ------------------------------------------------
# --- Prompt for one character of a given character class.
# --- $1 is prompt string.
# --- $2 is allowed answer characters class, in range [1-X]. 
# --- $3 is optional default answer (must belong to class in $2).
range_answerP()
{
   range_answer=
   while true; do
      answerP "$1 $2" "$3"
      range_answer=$answer
      test "X$range_answer" = "X" && test $# -eq 3 && range_answer=$3
      if test "X$range_answer" != "X"; then
         # get low and high limit in range, [low-high]
         low=`echo $2 | sed -n -e '/.*/   s/\[\([0-9]*\).*/\1/p'`
         hi=`echo $2 |  sed -n -e '/.*/   s/.*-\([0-9]*\)\]/\1/p'`
         # Accept that high can be nothing, set it to same as low
         test "X$hi" = X && hi=$low

         if test $low -le $range_answer -a $range_answer -le $hi; then
            break # out of the while loop.
        else
            range_answer=
        fi
      fi
   done
} # --- End of range_answerP

# --- get_sys_versionsP [-i] ------------------------------------------------
# --- Set the sys_version and sys_sub_version variables
# --- -i - interactive mode, ask if the version number is correct.
#
# --- static variables only used in this function
   # Extract major version number, there may be no dot.
   # In HP-UX, there is a letter in the beginning, such as 'A.10.20'
   # this is removed with the first [^0-9]* group 
   sed_ver_cmd='sed -n -e "/^[^0-9]*[0-9]\{1,\}/ \
                          s/^[^0-9]*\([0-9]\{1,\}\).*/\1/p"'
   # Extract minor version number, there should be a dot, do not include 
   # minor-minor version numbers, such as 1.2.3 becomes 2 
   sed_subver_cmd="sed -n '/[^0-9]*[0-9]*\.[0-9]\{1,\}/ \
                          s/[^0-9]*[0-9]*\.\([0-9]\{1,\}\)/\1/p'"
get_sys_versionsP ()
{
   uname_ver_cmd='uname -r'
   if test $sys_num -eq $AIX_n; then
      # AIX has their own version of how to answer to uname ...
      aix_uname_ver=`uname -v`
      aix_uname_sub_ver=`uname -r`
      uname_ver_cmd="echo $aix_uname_ver.$aix_uname_sub_ver"
   elif test $sys_num -eq ${SCO_WARE2_n}; then
      # UnixWare 2.x shows in version
      uname_ver_cmd='uname -v'
   fi

   if test "X$1" = "X-i"; then
      answerP "What is the version of the system" `eval $uname_ver_cmd`
      uname_ver_cmd="echo $answer"
   fi

   # These needs eval because of variable substitution
   sys_version=`eval "$uname_ver_cmd | $sed_ver_cmd"`
   sys_sub_version=`eval "$uname_ver_cmd | $sed_subver_cmd"`

   test "X$sys_version" = "X" && sys_version=0
   test "X$sys_sub_version" = "X" && sys_sub_version=0  
} # --- End of get_sys_versionsP


# --- os_type_initP ---------------------------------------------------------
# --- Determine what OS we are running under
# --- Sets the global variables listed in the beginning of the function.
os_type_initP() 
{
   # Supported systems

   SUN4_sys="SunOS 4 (SUN BSD, Solaris 1.x)"
   SUN5_sys="SunOS 5 (SUN SYS V, Solaris 2."
   AIX_sys="AIX (IBM RS/6000, BULL DPX 20)"
   HPUX_sys="HP-UX ("
   BOS_sys="BOS (BULL DPX 2)"
   OSF1_sys="DEC OSF/1 (Digital Equipment, Alpha)"
   ULTRIX_sys="ULTRIX (Digital Equipment, DEC)"
   SGI_sys="IRIX (Silicon Graphics, SGI)"
   SCO_sys="SCO UNIX (Santa Cruz Operation)"
   SCO_WARE2_sys="SCO UnixWare 2.x"
   SCO_WARE7_sys="SCO UnixWare 7"
   SCO_OPEN_sys="SCO OpenServer"
   FreeBSD_sys="FreeBSD (Berkeley UNIX)"
   Linux_sys="Linux"

   # Generic systems
   
   BSD_sys="Generic BSD (Berkeley UNIX)"
   SYSV3_sys="Generic SYS V R3 (UNIX System V Release 3)"
   SYSV4_sys="Generic SYS V R4 (UNIX System V Release 4)"
   
   # Use these constants to identify the systems below
   NO_SYS=0
   SUN4_n=1
   SUN5_n=2
   AIX_n=3
   HPUX_n=4
   BOS_n=5
   OSF1_n=6
   ULTRIX_n=7
   SGI_n=8
   SCO_n=9
   SCO_WARE2_n=10
   SCO_WARE7_n=11
   SCO_OPEN_n=12
   FreeBSD_n=13
   Linux_n=14
   BSD_n=15
   SYSV3_n=16
   SYSV4_n=17
   MAX_SYS_n=$SYSV4_n
    
   # Match beginning of system name and version, print system number
   cat <<END > $TMPDIR/os_opts
   /^SunOS 4/  { s/.*/$SUN4_n/p;    q;}
   /^SunOS 5/  { s/.*/$SUN5_n/p;    q;}
   /^AIX/      { s/.*/$AIX_n/p;     q;}
   /^HP-UX/    { s/.*/$HPUX_n/p;    q;}
   /^B.O.S. /  { s/.*/$BOS_n/p;     q;}
   /^OSF1/     { s/.*/$OSF1_n/p;    q;}
   /^ULTRIX/   { s/.*/$ULTRIX_n/p;  q;}
   /^IRIX/     { s/.*/$SGI_n/p;     q;}
   /^FreeBSD/  { s/.*/$FreeBSD_n/p; q;}
   /^UNIX_SV/  { s/.*/$SCO_WARE2_n/p; q;}
   /^UnixWare/ { s/.*/$SCO_WARE7_n/p; q;}
   /^SCO_SV/   { s/.*/$SCO_OPEN_n/p; q;}
   /^Linux/    { s/.*/$Linux_n/p;   q;}
   /./         { s/.*/$NO_SYS/p;    q;}
END

   sys_num=
   sys_version=
   sys_sub_version=
   sys_num=`uname -sr | sed -n -f $TMPDIR/os_opts`

   get_sys_versionsP

   # let the version number be for now, only change if asked for system
   test "X$sys_num" = "X" && sys_num=$NO_SYS
   
   # BSD has a printcap file but no lpadmin program
   if test $sys_num -eq $NO_SYS && 
      test -f ${DEF_PRINTCAP} && test ! -x /usr/lib/lpadmin; 
   then
      sys_num=$BSD_n
   fi

   # SCO UNIX cannot be found with the uname command
   if test $sys_num -eq $NO_SYS && test -f /boot; 
   then
      grep "SCO " /boot > /dev/null
      test $? = 0 && sys_num=$SCO_n
   fi
   
   # SYSV has lpadmin and no printcap
   if test $sys_num -eq $NO_SYS && 
      test -x /usr/lib/lpadmin && test ! -f ${DEF_PRINTCAP} ; 
   then
      sys_num=$SYSV3_n
      test -f /etc/lp/Systems && sys_num=$SYSV4_n
   fi
   
   if test ! $sys_num -eq $NO_SYS; then
      
      case $sys_num in
         $SUN4_n)    str=$SUN4_sys ;; 
         $SUN5_n)    str=$SUN5_sys$sys_sub_version")" ;; 
         $AIX_n)     str=$AIX_sys ;; 
         $HPUX_n)    str=$HPUX_sys$sys_version"."$sys_sub_version")" ;;
         $BOS_n)     str=$BOS_sys ;;
         $OSF1_n)    str=$OSF1_sys ;;
         $ULTRIX_n)  str=$ULTRIX_sys ;;
         $SGI_n)     str=$SGI_sys  ;; 
         $SCO_n)     str=$SCO_sys  ;;
         $SCO_WARE2_n) str=$SCO_WARE2_sys  ;;
         $SCO_WARE7_n) str=$SCO_WARE7_sys  ;;
         $SCO_OPEN_n)  str=$SCO_OPEN_sys  ;;
         $FreeBSD_n)   str=$FreeBSD_sys ;;
         $Linux_n)     str=$Linux_sys ;;
         $BSD_n)       str=$BSD_sys  ;; 
         $SYSV3_n)     str=$SYSV3_sys  ;;
         $SYSV4_n)     str=$SYSV4_sys  ;;
         *)   myexit 1 "Unknown system $sys_num" ;;
      esac

      echo
      echo "Your system is identified as being a $str"
      echo
      yes_or_noP "Is this correct" y
      test $yes_or_no = "NO" && sys_num=$NO_SYS 
   fi


   if test $sys_num -eq $NO_SYS ; then
	cat << SYS_TYPE_END

              Which of the systems below are you running?
	
          -----------------------------------------------------
             $SUN4_n....$SUN4_sys 
             $SUN5_n....$SUN5_sys
             $AIX_n....$AIX_sys
             $HPUX_n....$HPUX_sys
             $BOS_n....$BOS_sys
             $OSF1_n....$OSF1_sys
             $ULTRIX_n....$ULTRIX_sys
             $SGI_n....$SGI_sys
             $SCO_n....$SCO_sys
             $SCO_WARE2_n...$SCO_WARE2_sys
             $SCO_WARE7_n...$SCO_WARE7_sys
             $SCO_OPEN_n...$SCO_OPEN_sys
             $FreeBSD_n...$FreeBSD_sys
             $Linux_n...$Linux_sys

             $BSD_n...$BSD_sys
             $SYSV3_n...$SYSV3_sys
             $SYSV4_n...$SYSV4_sys

             $NO_SYS....Quit this program
          -----------------------------------------------------
            
SYS_TYPE_END

	range_answerP "             Enter choice" "[$NO_SYS-$MAX_SYS_n]" \
                      $NO_SYS 
	sys_num=$range_answer

        if test $sys_num -eq $NO_SYS; then
           myexit 0 "Exit $FILE_NAME"
        fi
        get_sys_versionsP -i
    fi


    # SGI may have a BSD spooler as well
    if test $sys_num = $SGI_n; then
	if test -x /usr/etc/lpc -a -x /usr/bsd/lpr; then
	    cat << SGI_BSD_END

Your system seems to have BSD lpr spooler installed. 
Do you want to remove the printer from the BSD spool system
or the SYS V spool system ?

              1......Remove from SYS V spool system (lp print command).
              2......Remove from BSD spool system (lpr print command).

SGI_BSD_END

	    class_answerP "Enter choice" '[1-2]' 1 
	    if test $class_answer = 2; then
		sys_num=$BSD_n
		echo "Will use the BSD spool system" >> $instalog
	    else
		echo "Will use the SYS V spool system" >> $instalog 
	    fi
	fi
    fi

    # --- Some default values
    stoplp=NO         # Don't stop scheduler
    HPFIX=" "         # If there should be space between lpadmin's options
    SHELL=/bin/sh     # for the benefit of lpadmin.

    case $sys_num in
	$SUN4_n|$OSF1_n|$BSD_n) 
	    sys_family=bsd
	    sys_type=bsd
	    ;;
	$SUN5_n) # Sun OS 5.x
	    sys_family=sysv
	    sys_type=sysv4
	    ;;
	$AIX_n) # AIX (IBM AIX 3)
	    sys_family=aix
	    sys_type=aix
	    ;;
	$HPUX_n) # HP-UX
	    sys_family=sysv
	    sys_type=sysv4 
	    HPFIX=
	    stoplp=YES
	    ;;
	$BOS_n) # BOS (BULL DPX 2)
	    sys_family=sysv
	    sys_type=sysv3
	    HPFIX=
	    stoplp=YES
	    ;;
	$ULTRIX_n) # ULTRIX
	    sys_family=bsd
	    sys_type=bsd
	    ;;
	$SGI_n) # SGI
	    sys_family=sysv
	    sys_type=sysv3
	    HPFIX=
	    stoplp=YES
	    ;;
	$FreeBSD_n|$Linux_n) # FreeBSD and Linux are generic BSD
	    sys_family=bsd
	    sys_type=bsd
	    lpq -V 2>&1 | grep LPRng >/dev/null
	    if test $? = 0; then
		find_printcapP
	    fi
	    ;;
	$SYSV3_n|$SCO_n|$SCO_OPEN_n) # SYS V3, SCO, SCO OpenServer
	    sys_family=sysv
	    sys_type=sysv3
	    stoplp=YES
	    ;;
	$SYSV4_n|${SCO_WARE2_n}|${SCO_WARE7_n}) # SYS V4, UnixWare 2 and 7
	    sys_family=sysv
	    sys_type=sysv4
	    ;;
	$NO_SYS)  # No system, exit
	    myexit 0 "No system found, Exit $FILE_NAME"
	    ;;
	*) # Other (unknown).
	    myexit 1 "Unknown OS type ($sys_type)"
	    ;;
    esac

} # --- End of os_type_initP

# --- find_printcapP -------------------------------------------------------
# --- Finds the printcap for Linux and FreeBSD when LPRng is used
# --- printcap can have different location depending on how
# --- LPRng was installed
find_printcapP ()
{
##  Here we find out the path to printcap
    test -f /usr/local/etc/lpd.conf
    lpdconf1="$?"
    test -f /etc/lpd.conf
    lpdconf2="$?"

    if test $lpdconf1 != 0; then
	if test $lpdconf2 = 0; then
	    logg "only/etc/lpd.conf is present"
	    DEF_PRINTCAP=`check_lpd_confP /etc`
	else
	    logg "perhaps printcap has to be created in /etc"
	fi
    else
	if test $lpdconf2 = 0; then
	    logg "there are both /etc and /usr/local/etc present"
	     mult_answerP "Would you like to use 1)/usr/local/etc/lpd.conf\
 or 2)/etc/lpd.conf" '12' '1'
	    lpd_chosen="${mult_answer}"
	    if test $lpd_chosen = 1; then
		DEF_PRINTCAP=`check_lpd_confP /usr/local/etc`
	    else
		DEF_PRINTCAP=`check_lpd_confP /etc`
	    fi
	else
	    DEF_PRINTCAP=`check_lpd_confP /usr/local/etc`
	    logg "/usr/local/etc will do"
	fi
    fi
    logg "The path to printcap is now ->$DEF_PRINTCAP<-"
} # --- End of find_printcapP

# --- check_lpd_confP $1 ----------------------------------------------------
# --- $1 - the path to the lpd.conf to be examined
check_lpd_confP ()
{
    sed -e "/^[#]/d" "$1/lpd.conf" > $TMPDIR/lpd.conf
    grep printcap_path $TMPDIR/lpd.conf > /dev/null
    if test $? = 0; then
	check_lpd_conf=`extract_valueP $TMPDIR/lpd.conf printcap_path`
	logg "CLC : There was an entry in $1/lpd.conf"
	logg "CLC = $check_lpd_conf"
    else
	check_lpd_conf="$1/printcap"
	logg "CLC : No relevant entry in $1/lpd.conf"
    fi
    myecho $check_lpd_conf
} # --- End of check_lpd_conf

# --- extract_valueP $1 $2 [$3 [$4]] ---------------------------------------
# --- Extract the value of option $2 in file $1
# --- Extracts B in a line like $2=B, stops on eol or ':'
# --- $3 - optional assign chatacter, default '='
# --- $4 - optional character group for separator, default is ':'
#          the assign character should not be among the separators 
#          If this option is given, then assign character must be given
extract_valueP ()
{
   extract_value=
   ass='='
   sep=':'
   test $# -ge 3 && ass=$3
   test $# -eq 4 && sep=$4
   cat <<- EOF > $TMPDIR/sed_bsd_pcap
     /$2[ 	]\{0,\}$ass/ s/.*$2[ 	]\{0,\}$ass\([^$sep]*\).*/\1/p
EOF
   extract_value=`sed -n -f  $TMPDIR/sed_bsd_pcap $1`
   myecho $extract_value
} # --- End of extract_valueP


# --- initilize --------------------------------------------------------------
clear_init()
{
    $clear
    cat << INIT_EOF

================================================================================

                       $FILE_NAME, version $REVISION.

================================================================================
INIT_EOF
}
# --- Initialize variables and temporary directory etc
initilize()
{
   PROD_TYPES=$PROD_TYPE"s"
   TMPDIR="/tmp/${FILE_NAME}.$$"
   PRINTCAP=$DEF_PRINTCAP
   instalog="/tmp/${FILE_NAME}_$$.log"

   mkdir $TMPDIR
   umask 022

   clear_init
   cat << WELCOME_END

  Welcome to the $PROD_TYPE installation and uninstallation program.

  This program will lead you through a procedure to install or uninstall
  $PROD_TYPES on your system.

  Installation requires that the IP address of your $PROD_TYPE 
  is defined.

  Before installation or uninstallation of each printer you will be able 
  to accept or reject the action.

  You can stop this program with the interrupt key at any time.
  An installation log will be saved in the file $instalog
WELCOME_END

   def_cont='y'
   
   if test "X`id | tr '()' '::' | cut -f2 -d:`" != "Xroot"; then
      cat <<ROOT_EOF

                     ----------------------------
                     You are not running as root!
                     ----------------------------

  It is likely that you have to be root to complete the installation
  and you may get error messages such as 'Cannot create ...' or
  'Permission denied ...' if you continue.

ROOT_EOF

      def_cont='n'
   fi

#clear_init
   cat << START_MSG_END

================================================================================

START_MSG_END

   yes_or_noP "Do you want to continue" $def_cont
   test $yes_or_no = NO && myexit 0

   # ---  create a log file in /tmp   
   cat > $instalog << LOGMSG_END

====================== $PROD_TYPE installation log ==========================

LOGMSG_END

} # --- End of initilize

# --- Simple prompt with default for a file path and name.
# --- The path MUST begin with a /
# --- Leave answer in variable path_answer.
path_answer=
path_answerP()
   # $1 is prompt string.
   # $2 is default answer.
{
   path_answer=
   while true; do
      answerP "$1" "$2"
      path_answer=$answer
      case $path_answer in
      /*) # Valid path.
         break
         ;;
      *)  # missing / at beginning.
         path_answer=
         ;;
      esac
   done
}


# --- Check for needed programs
check_programsP()
{
#  ftp_path=`which ftp`
    if test "X${ftp_path}" != "X" && test -x ${ftp_path} && test ! -d ${ftp_path}; then
	echo "${ftp_path} is used for ftp"
    else
	while true; do
	    path_answerP "Enter the ftp client to use with full path " $ftp_path
	    ftp_path=$path_answer
#	    echo ${ftp_path}
	    if test -x ${ftp_path} && test ! -d ${ftp_path}; then
		break
	    else
		echo "${ftp_path} is either not found,  or found but not executable"
	    fi
	done
    fi
#  if test "X${ftp_path}" = "X"; then
#    myexit 1 "ftp program could not be found"
#  fi
} # --- end of check_programsP

# --- lpshutP -----------------------------------------------------------------
lpshutP ()
{
   lpshut
   if test $sys_num -eq $HPUX_n; then
      while true; do
         test -f /usr/spool/lp/SCHEDLOCK
         if test $? != 0; then
            break
         fi
      done
   fi   
} # --- lpshutP 

# ============================================================================
#                                Installer Code 
# ============================================================================
 

# --- mult_answer $1 $2 [$3] ------------------------------------------------
# --- Prompt for one character in a list of characters.
# --- $1 is prompt string.
# --- $2 is allowed answer characters class, such as 'abft'. 
# --- $3 is optional default answer (must belong to class in $2).
mult_answerP()
{
   mult_answer=
   while true; do
      answerP "$1 [$2]" "$3"
      mult_answer=$answer
      test "X$mult_answer" = "X" && test $# -eq 3 && mult_answer=$3
      
      mult_num=1
      mult_loop=YES
      mult_found=NO
      # Loop through answer class to see if answer is valid
      while test $mult_loop = YES; do
         mult_char=`echo $2 | cut -c$mult_num`
         # cut after end of string results in empty string
         if test "X${mult_char}" = "X"; then
            mult_loop=NO
         elif test "X${mult_char}" = "X$mult_answer"; then
            mult_loop=NO
            mult_found=YES
         else
            mult_num=`expr 1 + $mult_num`
         fi 
      done

      if test $mult_found = YES; then
         break # out of the while loop.
      fi
   done
} # --- End of mult_answerP


# --- Say not if the parameter = NO
say_not()
{
   test "X$1" = "XNO" && echo " NOT"
}

# --- Say y or n when $1 is YES or NO.
say_y_or_n()
{
   test "X$1" = "XYES" && echo y
   test "X$1" = "XNO"  && echo n
}

# --- Say i or o when $1 is if or of.
say_io()
{
   test "X$1" = "Xif" && echo i
   test "X$1" = "Xof" && echo o
}

# --- Say in or out when $1 is if or of.
say_in_or_out()
{
   test "X$1" = "Xif" && echo input
   test "X$1" = "Xof" && echo output
}

# --- Test if we hawe write permission in the directory.
has_permission()
   # $1 is path to test
{
   if test ! -d $1; then
      has_permission `dirname $1`; return
   elif test -w $1; then
      true; return
   else
      false; return
   fi
}

# --- not <command> do boolean negation on return value of <command>.
not()
{
   if $*; then
      false; return
   else
      true; return
   fi
}

# --- ftp_downloadP $1 $2 $3 [$4] --------------------------------------------
# --- Download a file ($2/$3) from a host ($1), error returned in $ftp_download
# --- $1 Host name
# --- $2 Path name to file
# --- $3 file name, relative to path
# --- $4 Optional mode, bin as default
ftp_downloadP ()
{
    if test $ARCHIVE = 1; then
#	if test "X$source_path" = "X"; then
#	    if test $0 != ${0##/*/}; then
#		source_path=$(echo $0 | sed -e "s:${0##/*/}::g")
#	    else
#		source_path=$(pwd)
#	    fi
#	    source_path=$(echo $0 | sed -e "s:${0##/*/}::g")
#	fi
	while true; do
	    path_answerP "Enter the full path to the unpacked archive
    " $source_path
	    source_path=$path_answer
	    echo ${source_path}/${2}/${3}
	    if test -f ${source_path}/${2}/${3}; then
		cp ${source_path}/${2}/${3} .
		ftp_download=0; break
	    else
		echo "${3} is not found in ${source_path}/${2}/"
	    fi
	done
	return
    else
	ftp_mode='bin'
	test $# -eq 4 && ftp_mode=$4
	$ftp -n $1 <<- FTP_END
	user anonymous void
        $ftp_mode
	cd $2
	get $3
	quit
FTP_END
	ftp_download=$? 
	return
    fi
} # --- End of ftp_downloadP

# --- print_methodP $1 -------------------------------------------------------
# --- Prompt for the print method to use, first argument is the preferred.
prompt_print_method=
counter=
# help function
print_single_methodP ()
{
   print_single_method=
   if test $1 = YES && test "X$print_prefix" != "X$2"; then
      echo "                          $counter......$2"
      print_single_method=$counter
      counter=`expr $counter + 1`
   fi
}

prompt_print_methodP ()
{
   counter=1
   # find out preferred method
   case $1 in
      lpd)   print_prefix=LPD;    lpd_method_num=$counter;;
      ftp)   print_prefix=FTP;    ftp_method_num=$counter;;
      prosa) print_prefix=PROSA;  prosa_method_num=$counter;;
      prosb) print_prefix=PROSB;  prosb_method_num=$counter;;
      *) myexit 1 "Print method not supported ($1)" ;;
   esac
   echo
   echo "                        ----------------"
   echo "                          $counter......$print_prefix"
   counter=`expr $counter + 1`
   
   # now scan methods one by one
   print_single_methodP $lpd_enabled LPD
   test "X$print_single_method" != "X" && lpd_method_num=$print_single_method

   print_single_methodP $ftp_enabled FTP
   test "X$print_single_method" != "X" && ftp_method_num=$print_single_method

   print_single_methodP $prosa_enabled PROSA
   test "X$print_single_method" != "X" && prosa_method_num=$print_single_method

   print_single_methodP $prosb_enabled PROSB
   test "X$print_single_method" != "X" && prosb_method_num=$print_single_method

   # counter is alwys one too much here
   counter=`expr $counter - 1`   

   # Read which item that the user selects
   echo "                        ----------------"
   echo
   range_answerP "                Enter choice" "[1-$counter]" 1

   # Match the answer to a method
   case $range_answer in
      $lpd_method_num)    prompt_print_method=lpd ;;
      $ftp_method_num)    prompt_print_method=ftp ;;
      $prosb_method_num)  prompt_print_method=prosB ;;
      $prosa_method_num)  prompt_print_method=prosA ;;
      *)
	 myexit 1 "Bad case in prompt_print_methodP: $range_answer"
	 ;;	
   esac
} # --- prompt_print_methodP


# --- set_rprinterP ----------------------------------------------------
# --- Set remote printer name, can set both logical and physical printer 
# --- names. If physical printer is set, $REM_HOST_PRINTER_NUM is set
# --- to 0. If logical printer is set, $REM_HOST_PRINTER is set to
# --- "pr${REM_HOST_PRINTER_NUM}"
set_rprinterP ()
{
   echo
   echo "Choose the logical printer you want to use. If you are"
   echo "using the default logical printer settings, '1' means"
   echo "straight-through printing to LPT1 or USB1, depending"
   echo "on printserver model. And '5' means printing to LPT1"
   echo "or USB1 with NewLine to CR+LF conversion."
   echo "Or choose '0' to set a physical printer name, such as"
   echo "'LPT1', 'USB1, or 'COM1'."
   echo

   range_answerP "Enter logical printer number" '[0-8]' $REM_HOST_PRINTER_NUM
   REM_HOST_PRINTER_NUM="$range_answer"
   if test $REM_HOST_PRINTER_NUM = '0' ; then
      if test "X$REM_HOST_PRINTER" = "X"; then
         REM_HOST_PRINTER="lpt1"
      fi
      
      answerP "Enter physical printer name" $REM_HOST_PRINTER
      REM_HOST_PRINTER=`strip $answer`
   else
      REM_HOST_PRINTER="pr${REM_HOST_PRINTER_NUM}"
   fi
} # --- End of set_rprinterP


# --- aix_get_colon_fileP -----------------------------------------------------
# --- Get Colon file name in AIX
# --- Sets 'aix_printer' to printer type
# ---      'aix_stream'  to the data stream name and
# ---      'aix_stream_desc' to a description of data stream
# --- Colon files looks like 'printer.data', where printer is the printer
# --- type and data is the printer data that is generated for that printer.
# --- There may be several data types for each printer model.
aix_get_colon_fileP ()
{
   savedir=`pwd`
   cd ${DEF_AIX_COLONDIR}

   # list all colon files, one at each line
   ls -1 > $TMPDIR/pio_list

   # Make a list of all printer models available and print four columns
   # Split fields on ".", parse the lines (one line per file) and 
   # if the current file prefix (printer model) is the same as last one,
   # then don't print it.
   # This method ASSUMES SORTED OUTPUT from ls
   cat << 'AWK_END' > $TMPDIR/awk_get_unique
      BEGIN {name = ""; FS="."; nbr=0}

      {
         newn = $1
         if (newn == name) next
         name = newn
         nbr++
         printf "%18s", name
         if (nbr == 4) { print ""; nbr = 0}
      }

      END {print ""}
AWK_END

   # Make a list of available data types for the printer
   # Split fields by ".", look at suffix of file and print
   # a description of the data type, along with what line
   # in the input file it is in. 
   cat << 'AWK_END1' > $TMPDIR/awk_get_data
      BEGIN {name = ""; FS="."; nbr=0}

      {
              if ($2 ~ /^asc$/)  { Desc = "Extended ASCII" }
         else if ($2 ~ /^ps$/)   { Desc = "PostScript" }
         else if ($2 ~ /^pcl$/)  { Desc = "Hewlett-Packard PCL" }
         else if ($2 ~ /^630$/)  { Desc = "Diablo 630" }
         else if ($2 ~ /^855$/)  { Desc = "Texas Instruments 855" }
         else if ($2 ~ /^gl$/)   { Desc = "Hewlett-Packard GL" }
         else if ($2 ~ /^kji$/)  { Desc = "Kanji" }
         else if ($2 ~ /^lips/)  { Desc = "Canon LIPS" }
         else                    { Desc = "Unknown" }
         printf ("              %s: %25s (%s)\n", NR, Desc, $0);
      }
AWK_END1

   while true; do
      clear_init
      cat <<MODEL_EOF

                       Available printer models:
---------------------------------------------------------------------------
`awk -f $TMPDIR/awk_get_unique $TMPDIR/pio_list`
---------------------------------------------------------------------------

MODEL_EOF

      answerP "              Choose printer model" $aix_printer
      aix_model=$answer

      # check to see if the name was valid
      aix_hit=`grep "$aix_model" $TMPDIR/pio_list`
      if test "X${aix_hit}" = X; then
         echo "No such printer model: ${aix_model}"
      else

         # List all data streams for the printer model, one on each
         # line, starting from line 1
         # Colon file may have no suffix, therefore it the model name
         # is listed separately once and then list all with suffixes.
         ls -1 $aix_model $aix_model.* 2>/dev/null > $TMPDIR/chosen_model

         # What is the line number of the last data stream?
         last_num=`sed -n -e '$ =' $TMPDIR/chosen_model`
         last_num=`strip $last_num`

         # Make a list of valid data streams
         awk -f $TMPDIR/awk_get_data $TMPDIR/chosen_model > $TMPDIR/stream_list

         # Find out which number that the default stream type has
         def_stream=`grep ${aix_stream} $TMPDIR/stream_list | cut -d':' -f1`
         def_stream=`strip ${def_stream}`

         echo 
         echo "                          Available data streams:"
         echo "              --------------------------------------------"
         cat $TMPDIR/stream_list
         echo "              --------------------------------------------"
         echo
         range_answerP "              Number of data stream" "[1-$last_num]" \
                       ${def_stream}
         aix_stream_nbr=${range_answer}

         # Get the file name on line $aix_stream_nbr - this is the
         # colon file name
         aix_colon_file=`sed -n -e "$aix_stream_nbr p" $TMPDIR/chosen_model`

         # Trick to get description of data stream, run the printer through
         # the script above and then extract the stream type
         aix_stream_desc=`echo "${aix_colon_file}"|awk -f $TMPDIR/awk_get_data"`
         aix_stream_desc=`echo ${aix_stream_desc} |cut -d':' -f2 |cut -d'(' -f1`

         if test -f ${DEF_AIX_COLONDIR}/${aix_colon_file}; then
            break
         else
            echo "No such colon file - ${aix_colon_file}"
         fi
      fi
      echo "Press enter to proceed"
      read dummyplug
   done

   aix_stream=`echo ${aix_colon_file} | cut -f2 -d"."`
   aix_printer=`echo ${aix_colon_file} | cut -f1 -d"."`

   cd $savedir

} # --- aix_get_colon_fileP

# --- sysv_set_model_nameP $1 -------------------------------------------------
# --- Ask user for model to use and use $1 as default
sysv_set_model_nameP ()
{
   sysv_set_model_name=$1
   while true; do
      answerP "Enter model script name (? to list models)" $1
      model_answer=$answer
      if test "X$model_answer" = "X?"; then
         clear_init
         cat <<EOF_MODELS

                             Available models:
--------------------------------------------------------------------------------
`ls ${DEF_MODELDIR}`
--------------------------------------------------------------------------------

EOF_MODELS
      elif test -f ${DEF_MODELDIR}/${model_answer}; then
         break;
      else
         echo "The model ${model_answer} does not exist in ${DEF_MODELDIR}"
         yes_or_noP "Do you want to use the model anyway"
         test $yes_or_no = YES && break
      fi
   done
   sysv_set_model_name=${model_answer}
} # --- sysv_set_model_nameP


# --- set_ask_configP $1 ----------------------------------------------------
# --- Set config for printer, directories etc
# --- Some of the questions asked are not needed as they can have
# --- default values, so only ask if the parameter says we should
# --- $1 - optional parameter, should we ask user about options? 
# ---      set as YES or NO, default is NO
set_ask_configP()
{
   ask_config=NO
   if test $# != 0 && test $1 = "YES"; then
      ask_config=YES
   fi

   echo

   if test "X$REM_HOST_NAME" = "X"; then
      while test "X$REM_HOST_NAME" = "X"; do
         answerP "Enter the host name of your $PROD_TYPE"
         REM_HOST_NAME=$answer
      done
   else
      answerP "Enter the host name of your $PROD_TYPE " $REM_HOST_NAME
      REM_HOST_NAME=$answer
   fi
   
   set_rprinterP

   if test X$PRINTER = X; then
      case $sys_family in
         bsd) psep='-' ;;
         *)   psep='_' ;;
      esac

      if test "X${suggest_name}" = XYES; then
         PRINTER="${REM_HOST_NAME}$psep${print_method}"
      else
         PRINTER="${REM_HOST_NAME}${psep}${REM_HOST_PRINTER}"
      fi
   fi

   echo
   answerP "Enter printer name" $PRINTER
   if test "X$answer" != "X$PRINTER"; then
      # Printer name is new, forget old values of spooldir and logfile
      SPOOLDIR=
      LOGFILE=
   fi
   PRINTER=$answer
   
   if test $sys_family = bsd; then
   
# --- If lprng is used - check if ifhp is used too - if so select model
      lpq -V 2>&1 | grep LPRng > /dev/null
      if test $? = 0; then
	 # only allow printer models to be defined in methods that
	 # has inputfilter free for ifhp to use
	 model_answer=""
	 lprng_select_modelP
      fi

      test X$SPOOLDIR = X && SPOOLDIR=`eval ${DEF_SPOOLDIR_EVAL}`
      if test $ask_config = "YES"; then
         path_answerP "Enter spool directory name" $SPOOLDIR
         test $SPOOLDIR != $path_answer && create_spooldir=YES
         SPOOLDIR=$path_answer
      fi 
   
      test X$LOGFILE = X && LOGFILE=`eval ${DEF_LOGFILE_EVAL}`
      if test $ask_config = "YES"; then
         path_answerP "Enter log file name" $LOGFILE
         test $LOGFILE != $path_answer && create_logfile=YES
         LOGFILE=$path_answer
   
         path_answerP "Enter printcap file name" $PRINTCAP
         PRINTCAP=$path_answer
      fi # $ask_config = YES
      test ! -f $PRINTCAP && create_pcap=YES
   fi # $sys_family = bsd
   
   if test X$PROSHOME = X; then
      PROSHOME=$DEF_PROSHOME
      test $sys_family = aix && PROSHOME=$DEF_PROSHOME_AIX
   fi

   # --- Install print methods -------------------
   case $print_method in
   
   # ----- PROSA ----
   prosA)
      if test $sys_family = sysv; then
        echo "The PROS A method allows the use of any printer model script "
        echo "in the '${DEF_MODELDIR}' directory."
        sysv_set_model_nameP ${MODEL}
        MODEL=${sysv_set_model_name}
      fi

      if test $ask_config = "YES"; then
         path_answerP "Enter PROS daemon directory" $PROSHOME
         test X$PROSHOME != X$path_answer && create_proshome=YES
         PROSHOME=$path_answer
      fi
   
      PROSDEV=`eval ${DEF_PROSDEV_EVAL}`
      if test $sys_num -ne $BOS_n && test $ask_config = "YES"; then
         path_answerP "Enter PROS daemon pipe: " $PROSDEV
         test X$PROSDEV != X$path_answer && create_prosdev=YES
         PROSDEV=$path_answer
      fi
   
      PROSLOG=`eval ${DEF_PROSLOG_EVAL}`
      if test $ask_config = "YES"; then
         path_answerP "Enter PROS log file: " $PROSLOG
         test $PROSLOG != $path_answer && create_proslog=YES
         PROSLOG=$path_answer
      fi
   
     if test $ask_config = "YES"; then
         yes_or_noP "Compile the PROS daemon" `say_y_or_n $compile_pros`
         compile_pros=$yes_or_no
   
        yes_or_noP "Start the PROS daemon" `say_y_or_n $start_prosd`
         start_prosd=$yes_or_no
      fi
      ;;      
   
   # ---- PROSB ---- 
   prosB)
      if test $ask_config = "YES"; then
         path_answerP "Enter PROS filter directory: " $PROSHOME
         test X$PROSHOME != X$path_answer && create_proshome=YES
         PROSHOME=$path_answer
   
         # These if_or_of is set outside this function,
         # before the loop where this function is called.
         if test $sys_family = bsd; then
            mult_answerP "Is PROS filter input or output?" 'io' \
                         `say_io $if_or_of`
            if_or_of="${mult_answer}f"
         fi
   
   
         if test $sys_family = aix; then
           cat << AIX_LOG_MODE_END

Put print job info in log file or mail user?

                      ${AIX_LOG_FILE}.........USE LOG FILE
                      ${AIX_LOG_MAIL}.........MAIL USER

AIX_LOG_MODE_END

           range_answerP "Enter choice" "[${AIX_LOG_FILE}-${AIX_LOG_MAIL}]" \
                         $aix_log_mode
           aix_log_mode=$range_answer
	   
           case $aix_log_mode in
           "${AIX_LOG_FILE}")
             create_proslog="YES"
             PROSLOG=`eval ${DEF_PROSLOG_EVAL}`
             path_answerP "Enter PROS log file: " $PROSLOG
             PROSLOG=$path_answer
             ;;
           "${AIX_LOG_MAIL}")
             create_proslog=NO
             PROSLOG=
             ;;
           *)
              myexit 1 "AIX log mode is wrong: '$aix_log_mode'"
           esac
         fi
      fi 
      ;;

   # ---- FTP ----
   ftp)
     if test $ask_config = "YES"; then
        path_answerP "Enter FTP filter directory:  " $PROSHOME
        test X$PROSHOME != X$path_answer && create_proshome=YES
        PROSHOME=$path_answer
        # These if_or_of is set outside this function,
        # before the loop where this function is called.
        if test $sys_family = bsd; then
           mult_answerP "Is PROS filter input or output?" 'io' \
                        `say_io $if_or_of`
           if_or_of="${mult_answer}f"
        fi
     fi
     ;;

   lpd)
     # Special treatment of HP-UX models in LPD
     if test $sys_num -eq $HPUX_n; then
        if test -f ${DEF_MODELDIR}/rmodel.asx; then
           HP_LPDMODEL="rmodel.asx"
           echo "The LPD method allows a model name to be used"
           sysv_set_model_nameP ${MODEL}
           MODEL=${sysv_set_model_name}
        else
           HP_LPDMODEL=rmodel
        fi
     fi
     ;;

   esac # Case print method

   if test $ask_config = "YES" && \
      test $sys_family = sysv && test $sys_num != $HPUX_n; 
   then
      echo 
      echo "Contents type and printer type are used to determine wich filter"
      echo "that should applied, please see lpadmin and lpfilter man pages."
      echo "The pair 'any' and 'unknown' allows any data to be printed,"
      echo "and no filtering is done."
      echo "The types can be changed later with the lpadmin program."
      echo "Example Content type: 'postscript' or 'postscript,pcl,any'"
      echo "        Printer type: 'PS' or 'PS,PCL' or 'PS,PCL,unknown'"
      echo
      answerP "Enter Content types" ${printer_content}
      printer_content=${answer}
      answerP "Enter Printer types" ${printer_ptypes}
      printer_ptypes=${answer}
   fi

   if test $sys_family = aix && test $print_method != lpd; then
     aix_get_colon_fileP
   fi

} # --- set_ask_configP

# --- print_columnP $1 [$2] ------------------------------------------------
# --- Print lines with colon inbetween $1 and $2, where all colons
# --- are centered above eachother.
# --- $1 Text to be right justified before the colon
# --- $2 Optional text after colon, no colon if this is not specified 
print_columnP ()
{
   after_colon=
   test $# -eq 2 && after_colon=": $2"
   echo $1 | sed -n "s/^/                              /; \
                     s| *\(.\{30,\}\)$|\1${after_colon}|p"
} # --- print_columnP 

# --- print_indentedP [-n] string string ...
# --- if -n is given, do not insert newline
print_indentedP ()
{
  option=
  test "$1" = '-n' && option="$1" && shift
  myecho $option "         $*"
} # --- print_indentedP

# --- be_creativeP $1 $2 [$3] ------------------------------------------------
# --- Print a string of what should be created or started.
# --- $1 - What will be created
# --- $2 - YES if it should be created.
be_creativeP ()
{
  created="created"
  test $# -eq 3 && created="$3"
  print_indentedP "$1 will`say_not $2` be ${created}"
} # --- be_creativeP 



# --- barf_setupP ----------------------------------------------------
# --- Print config for printer
barf_setupP()
{
      clear_init
      cat << SETUP_0_END

------------------------------- Printer setup ---------------------------------

SETUP_0_END

      print_columnP "$PROD_TYPE host name"  $REM_HOST_NAME
      print_columnP "Remote printer"  $REM_HOST_PRINTER
      print_columnP "Printer name"  $PRINTER

      if test $sys_family = bsd; then
         print_columnP "Spool directory"  $SPOOLDIR
         print_columnP "Log file"  $LOGFILE
         print_columnP "Printcap file"  $PRINTCAP
	 lpq -V 2>&1 | grep LPRng > /dev/null
	 if test $? = 0  && test "X$model_answer" != "X"; then
	    print_columnP "Input filter" "$ifhpplace/libexec/filters/ifhp"
	    print_columnP "Printer model" "$model_answer"
	 fi
      fi

      case $print_method in
      prosA)
         print_columnP "PROS daemon directory"  $PROSHOME
         print_columnP "PROS daemon pipe"  $PROSDEV
         print_columnP "PROS daemon log file"  $PROSLOG
         ;;

      prosB)
         print_columnP "PROS filter directory"  $PROSHOME
         if test $sys_family = bsd; then
            print_indentedP "PROS filter is `say_in_or_out $if_or_of` filter"
         fi
      
         if test $sys_family = aix; then

            case $aix_log_mode in
            $AIX_LOG_FILE)
              print_columnP "PROS log file" $PROSLOG
              ;;
            $AIX_LOG_MAIL)
              print_columnP "PROS log file" "none, will mail user"
              ;;
            *) 
              myexit 1 "AIX log mode was wrong: '$aix_log_mode'."
            esac
         fi
         ;;

      ftp)
         print_columnP "FTP filter directory"  $PROSHOME
######### Stefan U A
         if test $sys_family = bsd; then
            print_indentedP "PROS filter is `say_in_or_out $if_or_of` filter"
         fi
         ;;
      esac

      if test $sys_family = aix && test $print_method != lpd;
      then
         print_columnP "Printer Type" "${aix_printer}"
         print_columnP "Data Stream Type" "${aix_stream_desc} (${aix_stream})"
         print_columnP "Printer Colon File" "$aix_printer.$aix_stream"
      fi # test $sys_family = aix && test $print_method != lpd;

      if test $sys_family = sysv ; 
      then
        if test "X${MODEL}" != "X"; then
           print_columnP "Printer model"  ${MODEL} 
        fi
        if test $sys_num != $HPUX_n; then
           print_columnP "Contents type" "${printer_content}"
           print_columnP "Printer type" "${printer_ptypes}" 
        fi
      fi

      echo

      if test $sys_family = bsd; then
         pcap_create="modified"
         if test $create_pcap = "YES"; then
            pcap_create=
         fi
         be_creativeP "The printcap file" YES ${pcap_create}

         be_creativeP "The spool directory" $create_spooldir
         be_creativeP "The log file" $create_logfile

      fi # test $sys_family = bsd

     case $print_method in
      prosA)
         be_creativeP "The PROS daemon directory" $create_proshome
         be_creativeP "The PROS daemon pipe" $create_pipe
         be_creativeP "The PROS daemon log file" $create_proslog
         be_creativeP "The PROS daemon" $compile_pros
         be_creativeP "The PROS daemon" $start_prosd "started"
        ;;
      prosB)
         if test $sys_family = aix;
         then
            be_creativeP "The PROS filter log file" $create_proslog
         fi
         be_creativeP "The PROS filter directory" $create_proshome
         ;;
 
      ftp)
         be_creativeP "The FTP filter directory" $create_proshome
         ;;
      esac

      cat << SETUP_5_END

-------------------------------------------------------------------------------

SETUP_5_END

} # --- barf_setupP

# --- check_setupP ----------------------------------------------------
# --- Check that the setup is installable
check_setupP()
{
   echo
   echo "Checking setup, please wait ..."
   
   more_changes=NO # --- Initially assume everything is OK.
   
   ftp_err=${TMPDIR}/ftp_err
   ftp_msg=${TMPDIR}/ftp_msg
   $ftp -nv $REM_HOST_NAME <<- TEST_FTP_END 2> $ftp_err 1> $ftp_msg
                quit
TEST_FTP_END
   err=`cat $ftp_err`

   msg=`grep -i "${PROD_TYPE}" $ftp_msg`
   
   if test "X${err:+mumble}" != "X"; then
   
      more_changes=YES
      echo "The $PROD_TYPE \"$REM_HOST_NAME\" is not responding"
      echo "Check your host table"
   
   elif test "X${msg:+mumble}" = "X"; then
   
      # --- New ftp login did not work, try the old one
      msg=`grep -i "FTP Print Server " $ftp_msg`

      if test "X${msg:+mumble}" = "X"; then
         more_changes=YES
         echo "The host \"$REM_HOST_NAME\" is not recognized as a $PROD_TYPE"
         echo "Check your host table"
      fi
   fi   
 
   rm $ftp_err $ftp_msg
   
   if test $more_changes != "YES"; then
   
      case $sys_family in

         # CASE BSD
         bsd)
            if test $create_spooldir = YES && test -d $SPOOLDIR; then
               echo "The spool directory $SPOOLDIR already exists"

               yes_or_noP "Do you want to use the existing spool directory" n
               if test $yes_or_no = YES; then
                  create_spooldir=NO
               else
                  more_changes=YES
                  myecho "Please choose another spool directory."
               fi
            fi

            if test $create_logfile = YES && test -f $LOGFILE; then
               echo "The log file $LOGFILE already exists"

               yes_or_noP "Do you want to use the existing log file" n
               if test $yes_or_no = YES; then
                  create_logfile=NO
               else
                  myecho "Please choose another log file."
                  more_changes=YES
               fi
            fi

            if test -f $PRINTCAP; then
               sedcmd="/^$PRINTER|.*:/p;/^.*|$PRINTER|.*:/p;/^.*|$PRINTER:/p"
               pcap_entry=`sed -n -e $sedcmd $PRINTCAP`

               if test ! -w $PRINTCAP; then
                  echo "No permission to modify the printcap file $PRINTCAP."
                  more_changes=YES
                  edit_pcap=NO
               elif test X${pcap_entry:+mumble} != X; then
                  echo "There is already an entry in the $PRINTCAP file for \
                        $PRINTER."
                  echo "The $PRINTCAP file will NOT be modified"

                  more_changes=YES
                  edit_pcap=NO
               else
                  edit_pcap=YES
               fi

            elif test $create_pcap = NO; then
               echo
               echo "The $PRINTCAP file does not exists."

               yes_or_noP "Do you want to create the $PRINTCAP file" n
               create_pcap=$yes_or_no
               if test $create_pcap = YES; then
                  if has_permission `dirname $PRINTCAP`; then
                     edit_pcap=YES
                  else

                     echo "No permission to create printcap file $PRINTCAP."
                     create_pcap=NO
                     more_changes=YES
                  fi
               fi
            fi # create_pcap
            ;;
	 # CASE SYSV 
         sysv)
            printer_exists=`/usr/bin/lpstat -p | grep $PRINTER 2> /dev/null`
            if test "X${printer_exists}" != "X"; then
                echo "Printer $PRINTER is already present in the system"
                more_changes=YES
            fi	    
            ;;

	 # CASE AIX
         aix)
            qchk -P$PRINTER 2> /dev/null 1> /dev/null
            if test $? = 0; then
               echo "Printer $PRINTER is already present in the system"
               more_changes=YES
            fi

            if test ! -f $DEF_AIX_COLONDIR/$aix_printer.$aix_stream; then
               echo "Printer Colon File \
                     $DEF_AIX_COLONDIR/$aix_printer.$aix_stream not present."
               more_changes=YES
            fi
            ;;
      esac # Case system
   fi # test $more_changes

   if test $more_changes != "YES"; then
      case $print_method in
         prosA) method_dir_str="PROS daemon" ;;
         prosB) method_dir_str="PROS filter" ;;
         ftp)   method_dir_str="FTP filter"  ;;
      esac

      if test $create_proshome = YES && not has_permission `dirname $PROSHOME`;
      then
         myecho "No permission to create the $method_dir_str directory \
                 $PROSHOME"
         create_proshome=NO
         more_changes=YES
      fi # $create_proshome = YES && not has_permission ...
   fi # test $more_changes

   if test $more_changes != "YES"; then
      if test $create_prosdev = YES && test -p $PROSDEV; then
         echo "The pros daemon pipe $PROSDEV already exists"
      
         yes_or_noP "Do you want to use the existing pipe" n
         if test $yes_or_no = YES; then
            create_prosdev=NO
         else
            more_changes=YES
            myecho "Please choose another pipe name."
         fi
      fi
      
      if test $create_prosdev = YES && \
            not has_permission `dirname $PROSDEV` ; then
         echo "No permission to create the pipe $PROSDEV"
      
         create_prosdev=NO
         more_changes=YES
      fi # test $create_prosdev
   fi # test $more_changes

   if test $more_changes != "YES"; then
      if test $create_proslog = YES && test -f $PROSLOG &&\
            test $more_changes != "YES"; then
         echo "The pros log file $PROSLOG already exists"
      
         yes_or_noP "Do you want to use the existing pros log file" n
         if test $yes_or_no = YES; then
            create_proslog=NO
         else
            myecho "Please choose another log file."
            more_changes=YES
         fi
      fi # test $create_proslog = YES && test -f $PROSLOG

      if test $create_proslog = YES && not has_permission `dirname $PROSLOG`; 
      then
         echo "No permission to create the pros log file $PROSLOG"
         create_proslog=NO
         more_changes=YES
      fi
   fi # test $more_changes
   

   if test $compile_pros = YES && test $more_changes != "YES"; 
   then
      case $print_method in
      prosA)
         if test -f $PROSHOME/prosd; then
            echo "The PROS daemon in directory $PROSHOME already exists"
   
            yes_or_noP "Do you want to use the existing PROS daemon" y
            if test $yes_or_no = YES; then
               compile_pros=NO
            else
	       myecho "Please choose another directory for the daemon." 
               more_changes=YES
            fi
         fi
         ;;
      prosB)
        FILTER_NAME=`eval $DEF_PROSB_FILE_EVAL`
        if test -f ${PROSHOME}/${FILTER_NAME}; then
            echo "The PROS filter ${FILTER_NAME} in directory $PROSHOME ,"
            echo "already exists; remove it or choose another printer name."
            more_changes=YES
         fi

         # In AIX, one prosb filter can be used by many printers, therefore
         # the user should be able to use that filter or update it
         # if it has become old
         if test -f $PROSHOME/prosaix; then
            echo "The PROSB filter help program $PROSHOME/prosaix is used "
            echo "by all prosb filters. If you want, you can update the"
            echo "program, but this might effect all your printers."
            echo "A backup will be put in /tmp/prosaix.$PRINTER."
            echo 
            yes_or_noP "Do you wish to update the program" y
            if test $yes_or_no = YES; then
               compile_pros=YES
               cp $PROSHOME/prosaix $TMPDIR/prosaix.$printer
            else
               myecho "Please choose another filter directory."
               more_changes=YES
            fi
         fi
         ;;
   
      ftp)
         FILTER_NAME=`eval ${DEF_FTP_FILE_EVAL}`
         if test -f ${PROSHOME}/${FILTER_NAME}; then
            echo "The FTP filter ${FILTER_NAME} in directory $PROSHOME "
            echo "already exists; remove it or choose another printer name."
            more_changes=YES
            compile_pros=NO
         fi
         ;;
      esac
   fi # test $compile_pros

   if test $more_changes = NO; then
      echo "No conflicts found"
   fi

} # --- check_setupP

# --- install_initP -----------------------------------------------------------
install_initP ()
{
   if test "X$ftp" = "X"; then
      echo "Cannot find your FTP program."
      path_answerP "Please enter the full path name of your FTP program: "
      ftp=$path_answer
   
      if test "X$ftp" = "X"; then
         myexit 1 "Cannot install without FTP program."
      fi
      echo
   fi
   
   if test "X$HOSTNAME" = "X"; then
      echo "Cannot find out the network name of this host."
      answerP "Please enter host name: "
      HOSTNAME=$answer
   
      if test "X$HOSTNAME" = "X"; then
         myexit 1 "Cannot install any network utilities without a host name."
      fi
      echo
   fi   
   
   # by default, all print methods are enabled
   lpd_enabled=YES
   ftp_enabled=YES
   prosa_enabled=YES
   prosb_enabled=YES
   
   # set system family options
   case $sys_family in
      sysv)
         filt_name=sysv
         ;;
      bsd)
         filt_name=bsd
         # ULTRIXFIX is used by all BSD systems, but only some set it. 
         ULTRIXFIX=
         ;;
      aix)
         filt_name=piobe
         prosa_enabled=NO
         ;;
      *)
         myexit 1 "Bad case in system family, $sys_family"
         ;;
   esac

   # set standard model name
   MODEL=
   case $sys_num in
      $SUN5_n|$SYSV3_n|$SCO_n|$SYSV4_n) DEF_MODEL="standard" ;;
      $HPUX_n|$BOS_n|$SGI_n)            DEF_MODEL="dumb" ;;
   esac
   
   

   # other things, specific to odd systems
   case $sys_num in
   $ULTRIX_n) # ULTRIX
      # A line in the printcap entry for PROS A (OBS no ":" here).
      ULTRIXFIX="of=xf"
      ;;
   $FreeBSD_n) # FreeBSD
      $mknod=mkfifo
      ;;
   esac
   
   # test mkfifo or mknod for prosa availability
   case $sys_num in
      $FreeBSD_n)
         $mknod ${TMPDIR}/nisse p 2>/dev/null
         if test $? != 0; then
            prosa_enabled=NO
            echo
            echo "  Your system does not support FIFOs, PROS A cannot be used."
         fi
         rm ${TMPDIR}/nisse
         ;;
      $SUN5_n)
         if test $sys_sub_version -lt 5; then
            prosa_enabled=NO
         fi
   esac
   
   # Only OpenServer has a bsd spooler for LPD in a sysv3 system
   sysv3_bsd_lpd=NO
   
   # Sysv3 does not have lpd
   if test $sys_type = sysv3; then
      # But SCO OpenServer has a BSD spooler for LPD sometimes...
      if test $sys_num -eq $SCO_OPEN_n && test -f $PRINTCAP; then
         sysv3_bsd_lpd=YES
      else
         lpd_enabled=NO
      fi
   fi

   saved_pcap=NO  # Not saved yet.
   
   REM_HOST_NAME=
   PROSHOME=

} # --- install_initP

# --- install_init_methodP ----------------------------------------------------
# --- Set common variables that depends on the printing method
install_init_methodP ()
{
   # --- Common variables.
   
   # REM_HOST_NAME & PROSHOME are initialized outside more_printers loop
   #                    so we remember their values.
   
   REM_HOST_PRINTER_NUM=1
   PRINTER=
   
   SPOOLDIR=               # BSD only
   LOGFILE=                # BSD only
   PRINTCAP=${DEF_PRINTCAP}  # BSD only
   
   PROSDEV=
   PROSLOG=
   PROSPWD=netprinter      # Neither asked for nor changed.

   MODEL=
   
   # --- Common control variables.
   
   case $sys_family in
      bsd)
         create_pcap=NO
         edit_pcap=YES
         create_spooldir=YES
         create_logfile=YES
         ;;
      sysv)
         create_pcap=NO
         edit_pcap=NO
         create_spooldir=NO
         create_logfile=NO
         printer_content=any
         printer_ptypes=unknown
         ;;
      aix)
         create_pcap=NO
         edit_pcap=NO
         create_spooldir=NO
         create_logfile=NO
         aix_data_type=1
         if test $sys_version -eq 4; then
           aix_printer="generic"    
         else
           aix_printer="printer"      
         fi
         aix_stream="asc"
         ;;
   esac

  
   # --- Print method specific control variables.

   case $print_method in
      prosA)
         compile_pros=YES
         create_proshome=YES
         create_proslog=YES
         create_prosdev=YES
         start_prosd=YES
         MODEL=${DEF_MODEL}
         ;;
      prosB)
         compile_pros=YES
         create_proshome=YES
         create_proslog=NO
         if test $sys_family = aix; then
           aix_log_mode=$DEF_AIX_LOG_MODE
         fi
         create_prosdev=NO
         start_prosd=NO
         if_or_of=if
         ;;
      ftp)
         compile_pros=YES
         create_proshome=YES
         create_proslog=NO
         create_prosdev=NO
         start_prosd=NO
         if_or_of=if
         ;;

      lpd)
         compile_pros=NO
         create_proshome=NO
         create_proslog=NO
         create_prosdev=NO
         start_prosd=NO
         if test $sys_num -eq $HPUX_n && test -f ${DEF_MODELDIR}/rmodel.asx
         then
            MODEL=${DEF_MODEL}
         fi
         ;;
   esac
      
} # --- install_init_methodP

# --- lprng_correct_rightsP $1  ------------------------------------------------
# --- $1 is the file to be corrected
lprng_correct_rightsP ()
{
    lprng_dir="/var/spool/lpd"
    lprng_userid=`ls -ldn $lprng_dir | cut -c16-24`
    lprng_groupid=`ls -ldn $lprng_dir | cut -c25-32`
    lprng_user=`ls -ldn $lprng_dir | cut -c2-4 | sed -e s/[^rwx]//g`
    lprng_group=`ls -ldn $lprng_dir | cut -c5-7 | sed -e s/[^rwx]//g`
    lprng_other=`ls -ldn $lprng_dir | cut -c8-10 | sed -e s/[^rwx]//g`

    chown `echo $lprng_userid`:`echo $lprng_groupid` $1
    chmod u=`echo $lprng_user`,g=`echo $lprng_group`,o=`echo $lprng_other` $1
}
# --- bsd_install_printerP -----------------------------------------------------
bsd_install_printerP ()
{

   if test $create_pcap = YES; then
      if test ! -d `dirname $PRINTCAP`; then
         mkdir -p `dirname $PRINTCAP`
         test $? != 0 && error=YES
      fi
      cat <<- PCAP_HEADER_END > $PRINTCAP
# --- This printcap was created by the $PROD_TYPE installation script. ---
#
PCAP_HEADER_END
      test $? != 0 && error=YES
   
   else # test $create_pcap != YES
      # --- It is wise to save the printcap.
      saved_pcap_file=/tmp/$$_saved_pcap
      cp $PRINTCAP $saved_pcap_file
      test $? != 0 && error=YES
      
      logg "saved file $PRINTCAP in file $saved_pcap_file." 
   fi # test $create_pcap = YES

   if test $edit_pcap = YES ; then
      logg -n "Modifying printcap file $PRINTCAP, "
      logg      "installed following printcap entry:"
      logg
   
      case $print_method in
      prosA) # PROS A
	 lpq -V 2>&1 | grep LPRng > /dev/null
	 if test $? = 0  && test "X$model_answer" != "X"; then
         cat <<- PROSA_PCAP_ENTRY_ENDL | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $REM_HOST_NAME using PROS A:\\
                 :lp=$PROSDEV:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$ULTRIXFIX:\\
                 :if=$ifhpplace/libexec/filters/ifhp:\\
                 :ifhp=model=$model_answer:\\
                 :sh:
PROSA_PCAP_ENTRY_ENDL
         test $? != 0 && error=YES
	 else
         cat <<- PROSA_PCAP_ENTRY_END | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $REM_HOST_NAME using PROS A:\\
                 :lp=$PROSDEV:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$ULTRIXFIX:\\
                 :sh:
PROSA_PCAP_ENTRY_END
         test $? != 0 && error=YES
	 fi
         ;; 


      prosB) # PROS B
	 lpq -V 2>&1 | grep LPRng > /dev/null
	 if test $? = 0  && test "X$model_answer" != "X"; then
         cat <<- PROSB_PCAP_ENTRY_END_B | $tee -a $PRINTCAP >> $instalog
$PRINTER| bounce queue printer using b_$PRINTER on $PROD_TYPE $REM_HOST_NAME using PROS B :\\
                 :lp=$PRINTER@$HOSTNAME:\\
                 :bq=b_$PRINTER@$HOSTNAME:\\
                 :sd=$SPOOLDIR:\\
                 :if=$ifhpplace/libexec/filters/ifhp:\\
                 :ifhp=model=$model_answer:
b_$PRINTER| bounced from $PRINTER Network printer on $PROD_TYPE $REM_HOST_NAME using PROS B:\\
                 :lp=$SPOOLDIR/null:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$if_or_of=$PROSHOME/${FILTER_NAME}:\\
                 :ff=\\r\\f:\\
                 :sh:
PROSB_PCAP_ENTRY_END_B
         test $? != 0 && error=YES
	 else
         cat <<- PROSB_PCAP_ENTRY_END | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $PROD_TYPE $REM_HOST_NAME using PROS B:\\
                 :lp=$SPOOLDIR/null:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$if_or_of=$PROSHOME/${FILTER_NAME}:\\
                 :ff=\\r\\f:\\
                 :sh:
PROSB_PCAP_ENTRY_END
         test $? != 0 && error=YES
	 fi
         ;;


      ftp) # FTP
	 lpq -V 2>&1 | grep LPRng > /dev/null
	 if test $? = 0  && test "X$model_answer" != "X"; then
	 cat <<- FTP_PCAP_ENTRY_END_B | $tee -a $PRINTCAP >> $instalog
$PRINTER| bounce queue printer using b_$PRINTER on $PROD_TYPE $REM_HOST_NAME using FTP:\\
                 :lp=$PRINTER@$HOSTNAME:\\
                 :bq=b_$PRINTER@$HOSTNAME:\\
                 :sd=$SPOOLDIR:\\
                 :if=$ifhpplace/libexec/filters/ifhp:\\
                 :ifhp=model=$model_answer:
b_$PRINTER| bounced from $PRINTER Network printer on $PROD_TYPE $REM_HOST_NAME using FTP:\\
                 :lp=$SPOOLDIR/null:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$if_or_of=${PROSHOME}/${FILTER_NAME}:\\
                 :ff=\\r\\f:\\
                 :sh:
FTP_PCAP_ENTRY_END_B
         test $? != 0 && error=YES
	 else
         cat <<- FTP_PCAP_ENTRY_END | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $PROD_TYPE $REM_HOST_NAME using FTP:\\
                 :lp=$SPOOLDIR/null:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :$if_or_of=${PROSHOME}/${FILTER_NAME}:\\
                 :ff=\\r\\f:\\
                 :sh:
FTP_PCAP_ENTRY_END
         test $? != 0 && error=YES
         fi 
         ;;


      lpd) # LPD
	 lpq -V 2>&1 | grep LPRng > /dev/null
	 if test $? = 0  && test "X$model_answer" != "X"; then
	 cat <<- LPD_PCAP_ENTRY_ENDL | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $PROD_TYPE $REM_HOST_NAME:\\
                 :lp=:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :rm=$REM_HOST_NAME:\\
                 :if=$ifhpplace/libexec/filters/ifhp:\\
                 :ifhp=model=$model_answer:\\
                 :rp=$REM_HOST_PRINTER:
LPD_PCAP_ENTRY_ENDL
         test $? != 0 && error=YES
 	 else
         cat <<- LPD_PCAP_ENTRY_END | $tee -a $PRINTCAP >> $instalog
$PRINTER| Network printer on $PROD_TYPE $REM_HOST_NAME:\\
                 :lp=:\\
                 :sd=$SPOOLDIR:\\
                 :lf=$LOGFILE:\\
                 :rm=$REM_HOST_NAME:\\
                 :rp=$REM_HOST_PRINTER:
LPD_PCAP_ENTRY_END
         test $? != 0 && error=YES
	 fi
         ;;
      esac
      logg
   fi

   if test $create_spooldir = YES; then
      mkdir -p $SPOOLDIR
      test $? != 0 && error=YES
      logg "Created spool directory $SPOOLDIR."
      if test $sys_type = freebsd; then
         chown bin $SPOOLDIR 
         chmod 744 $SPOOLDIR
      fi
############### 7 Lines written by Stefan U Andersson 1999
      if test $sys_num = $Linux_n; then
        lpq -V 2>&1 | grep LPRng >/dev/null
	if test $? = 0; then
	    lprng_correct_rightsP $SPOOLDIR
#	    chown --reference=/var/spool/lpd $SPOOLDIR
#	    chmod --reference=/var/spool/lpd $SPOOLDIR
	fi
      fi
   
      case $print_method in
      prosB|ftp) # PROS B and FTP need dummy devices.
         touch $SPOOLDIR/null
         test $? != 0 && error=YES
         logg "Created dummy device $SPOOLDIR/null."
############### 7 lines written by Stefan U Andersson 1999
         if test $sys_num = $Linux_n; then
	    lpq -V 2>&1 | grep LPRng >/dev/null
	    if test $? = 0; then
	       lprng_correct_rightsP $SPOOLDIR/null
#	       chown --reference=/var/spool/lpd $SPOOLDIR/null
	       chmod 664 $SPOOLDIR/null
	       logg "Changed owner and rights of $SPOOLDIR/null."
             fi
         fi
         if test $sys_num -eq $FreeBSD_n ; then
            chown bin $SPOOLDIR/null 
            chmod 744 $SPOOLDIR/null
         fi
         ;;
      esac
   fi
  
   if test $create_logfile = YES; then
      if not test -d `dirname $LOGFILE`; then
         mkdir -p `dirname $LOGFILE`
         test $? != 0 && error=YES
         logg "Created directory `dirname $LOGFILE`."
         if test $sys_num -eq $FreeBSD_n ; then
            chown bin `dirname $LOGFILE`
            chmod 744 `dirname $LOGFILE`
         fi   
      fi
      touch $LOGFILE
      test $? != 0 && error=YES
      logg "Created log file $LOGFILE."
############### 7 lines written by Stefan U Andersson 1999
      if test $sys_num = $Linux_n; then
	 lpq -V 2>&1 | grep LPRng >/dev/null
	 if test $? = 0; then
	    lprng_correct_rightsP $LOGFILE
#	    chown --reference=/var/spool/lpd $LOGFILE
	    chmod 664 $LOGFILE
	    logg "Changed owner and rights of $LOGFILE."
	 fi
      fi
      if test $sys_num -eq $FreeBSD_n; then
         chown bin $LOGFILE 
         chmod 744 $LOGFILE
      fi
   fi  

} # --- bsd_install_printerP

# --- lprng_select_modelP ------------------------------------------------------
# the result will be in $model_answer
# if ifhp.conf is non existant $model_answer will be an empty string
lprng_select_modelP ()
{
    if test -f /etc/ifhp.conf; then
        ifhpconf="/etc/ifhp.conf"
	ifhpplace="/usr"
    else
        if test -f /usr/local/etc/ifhp.conf; then
                ifhpconf="/usr/local/etc/ifhp.conf"
		ifhpplace="/usr/local"
        else
                ifhpconf=
		ifhpplace=
        fi
    fi
 
    if test "X$ifhpconf" != "X"; then
	sed -ne "/^\[/ p" $ifhpconf > $TMPDIR/ifhp.one
	sed -ne "s/\[//p" $TMPDIR/ifhp.one > $TMPDIR/ifhp.two
	sed -ne "s/\]//p" $TMPDIR/ifhp.two > $TMPDIR/ifhp.one
	tr ' ' '\012' <  $TMPDIR/ifhp.one > $TMPDIR/ifhp.two
	sed -e "/^$/d" $TMPDIR/ifhp.two > $TMPDIR/ifhp.one
	echo "none" >> $TMPDIR/ifhp.one
	lprng_model_select_done=1
	while test "$lprng_model_select_done" != 0; do
	    logg "lprng_model_select_done = $lprng_model_select_done"
	    print_four_columnsP $TMPDIR/ifhp.one
	    answerP "Enter a model name from this list" "none"
	    model_answer=$answer
	    grep -w $model_answer $TMPDIR/ifhp.one > /dev/null
	    lprng_model_select_done="$?"
	done
	if test "$model_answer" = "none"; then
	    model_answer=""
	fi
    else
	model_answer=""
    fi
} # --- end of lprng_select_model --- auth Stefan U Andersson 1999

# --- print_four_columnsP -------------------------------------------------------
# $1 = infile with all options in one column
print_four_columnsP() 
{
    cat << 'SLUT' > awk_test
	BEGIN {name = ""; nbr=0}
	{
	    name = $1
	    nbr++
	    printf "%-18s", name
	    if (nbr == 4) {print ""; nbr = 0}
	}
	END {print ""}
SLUT

    awk -f awk_test $1
} # End of print_four_columnsP -- auth Stefan U Andersson 1999
# --- sysv_install_printerP ----------------------------------------------------
sysv_install_printerP ()
{
   test $stoplp = YES && lpshutP
  
   if test $sys_num -eq $BOS_n; then
      logg "Modifying /etc/config.lp with the following entries:"
   fi
   case $print_method in
      prosA)
         lpadmin -p$HPFIX$PRINTER -m$HPFIX$MODEL -v$HPFIX$PROSDEV 2>> $instalog
         if test $? != 0; then
            lperror=YES
         else
            lperror=NO
            logg "installed $PRINTER with lpadmin using "$MODEL\
                 " model on device $PROSDEV."
         fi

         if test $sys_num -eq $BOS_n; then
            cat <<- PROSA_BOS_ENTRY_END | tee -a /etc/config.lp >> $instalog
$PRINTER on $CAT_NAME $REM_HOST_NAME.$REM_HOST_PRINTER - m $MODEL - - - -
PROSA_BOS_ENTRY_END
         fi
         ;;

      prosB)
         lpadmin -p$HPFIX$PRINTER -i$HPFIX$PROSHOME/${FILTER_NAME} \
                 -v$HPFIX/dev/null 2>> $instalog
         if test $? != 0; then
            lperror=YES
         else
            lperror=NO
            logg "installed $PRINTER with lpadmin using interface \
                  $PROSHOME/${FILTER_NAME}."
            logg "device used is /dev/null (dummy device)."
         fi

         if test $sys_num -eq $BOS_n; then
            cat <<- PROSB_BOS_ENTRY_END | tee -a /etc/config.lp >> $instalog
$PRINTER on $CAT_NAME null - i $PROSHOME/${FILTER_NAME} - - - -
PROSB_BOS_ENTRY_END
         fi
         ;;

      ftp)
         lpadmin -p$HPFIX$PRINTER -i$HPFIX$PROSHOME/${FILTER_NAME} \
                 -v$HPFIX/dev/null 2>> $instalog
         if test $? != 0; then
            lperror=YES
         else
            lperror=NO
            logg "installed $PRINTER with lpadmin using interface \
                  $PROSHOME/${FILTER_NAME}."
            logg "device used is /dev/null (dummy device)."
         fi
         if test $sys_num -eq $BOS_n; then
            cat <<- FTP_BOS_ENTRY_END | tee -a /etc/config.lp >> $instalog
$PRINTER on $CAT_NAME null - i $PROSHOME/${FILTER_NAME} - - - -
FTP_BOS_ENTRY_END
         fi
         ;;

      lpd)
         if test $sys_num -ne $HPUX_n || test $sys_num -eq $SUN5_n && test $sys_sub_version -lt 6; then
            lpsystem -t bsd $REM_HOST_NAME >> $instalog
            if test $? != 0; then
               lperror=YES
            else
               lperror=NO
               logg "used lpsystem to add(modify) an entry for $REM_HOST_NAME"
               logg "in /etc/lp/Systems"
            fi 
         fi
   	 
         if test $sys_num -eq $HPUX_n; then
            if test ${HP_LPDMODEL} = "rmodel.asx"; then
		if test -d ${DEF_HP_LPDMODELDIR}; then
		    logg "Copying ${DEF_MODELDIR}/${MODEL} to " \
			"${DEF_HP_LPDMODELDIR}/${PRINTER}"
                    # Put model in remote model directory (HP_UX 10.20)
		    cp ${DEF_MODELDIR}/${MODEL} ${DEF_HP_LPDMODELDIR}/${PRINTER}
		    chmod 755 ${DEF_HP_LPDMODELDIR}/${PRINTER} >/dev/null
		    chgrp lp ${DEF_HP_LPDMODELDIR}/${PRINTER} >/dev/null
		    chown lp ${DEF_HP_LPDMODELDIR}/${PRINTER} >/dev/null
		fi
            fi
            lpadmin -p$HPFIX$PRINTER \
                    -v$HPFIX/dev/null \
                    -orm$HPFIX$REM_HOST_NAME \
                    -orp${HPFIX}${REM_HOST_PRINTER} \
                    -m$HPFIX${HP_LPDMODEL} \
                    >> $instalog
         elif test $sys_num -eq $SUN5_n && test $sys_sub_version -ge 6; 
         then
            # Solaris 2.6 and higher uses the netstandard script
            lpadmin -p$HPFIX$PRINTER \
                    -i$HPFIX/usr/lib/lp/model/netstandard \
                    -v${HPFIX}/dev/null \
                    -o${HPFIX}protocol=bsd \
                    -o${HPFIX}dest=$REM_HOST_NAME:${REM_HOST_PRINTER} \
                    >> $instalog
         else
            lpadmin -p$HPFIX$PRINTER \
                    -s$HPFIX$REM_HOST_NAME!$REM_HOST_PRINTER >> $instalog
         fi

         if test $? != 0; then
            lperror=YES
         else
            lperror=NO
            logg "installed $PRINTER with lpadmin using remote system" \
                 "name $REM_HOST_NAME and" 
            logg "  remote printer name $REM_HOST_PRINTER"
         fi
         ;;
   esac
   
   # Add content-type any to printers, except in HP-UX
   if test $sys_num != $HPUX_n && test $sys_type = sysv4; then
      logg "Contents type=${printer_content}, printer type=${printer_ptypes}"
      /usr/lib/lpadmin -p$HPFIX$PRINTER -I${HPFIX}${printer_content} \
                       -T${HPFIX}${printer_ptypes} 2>&1 | tee -a ${instalog}

      # add possibility for user to use -o banner when printing
      logg "Adding nobanner option"
      /usr/lib/lpadmin -p$HPFIX$PRINTER -o${HPFIX}nobanner \
                       2>&1 | tee -a ${instalog}
   fi
   
   # nobanner does not work in Solaris 2.6, fix in config file
   if test -f /etc/lp/printers/$PRINTER/configuration; 
   then
      cat <<EOF > ${TMPDIR}/ed_tmp
         /Banner:/ s/on/off/
         w
         q
EOF
      lecho "Edited /etc/lp/printers/$PRINTER/configuration"
      ed -s /etc/lp/printers/$PRINTER/configuration < ${TMPDIR}/ed_tmp
      rm ${TMPDIR}/ed_tmp
   fi
   
   test $stoplp = YES && lpsched

   # No accept, enable for SunOS 5.6 or higher
   # Yes, if we change to netstandard
   # if test ! \( $print_method = lpd -a \
   #   $sys_num -eq $SUN5_n -a $sys_sub_version -ge 6 \);then
      test $lperror = NO && accept $PRINTER
      test $lperror = NO && enable $PRINTER
   # fi

   test $lperror = YES && error=YES
    
} # --- sysv_install_printerP

# --- aix_install_printerP
aix_install_printerP ()
{
   test $print_method = prosB && print_method=pros
   
   case $print_method in
      pros | ftp)
         ln /dev/null /dev/$PRINTER'd'
         if test $? = 0; then
            logg "linked printer device /dev/"$PRINTER"d to /dev/null"
         else  
           error=YES
         fi
      
         if test $error = NO; then
            mkque -q $PRINTER
            if test $? = 0; then
               logg "used mkque, local queue is "$PRINTER
            else
               logg "mkque did not work."
               error=YES
               rm /dev/$PRINTER'd'
            fi
         fi
     
         if test $error = NO; then
            mkquedev -d $PRINTER'd' -q $PRINTER \
                     -a 'backend = '$PROSHOME'/'$print_method'_'$PRINTER''
            if test $? = 0; then
               logg "used mkquedev, created queue device "$PRINTER"d"
               logg "  backend is "$PROSHOME"/"$print_method"_"$PRINTER 
             else
               logg "mkquedev did not work."
               error=YES
               rm /dev/$PRINTER'd'
               rmque -q $PRINTER
            fi
         fi
    
         if test $error = NO; then
            mkvirprt -d $PRINTER'd' -n $PRINTER'd' -q $PRINTER \
                     -s $aix_stream -t $aix_printer
            if test $? = 0; then
               logg "used mkvirprt, created virtual printer $PRINTER"
               logg "  data stream type $aix_stream, printer type $aix_printer"
            else
               logg "mkvirprt did not work."
               error=YES
               rm /dev/$PRINTER'd'
               rmquedev -q $PRINTER -d $PRINTER'd'
               rmque -q $PRINTER
            fi
         fi
         ;; 
   
      lpd)
         mkque -q$PRINTER -a"up = "'TRUE' -a"host = "$REM_HOST_NAME\
               -a"s_statfilter = "'/usr/lpd/bsdshort'\
               -a"l_statfilter = "'/usr/lpd/bsdlong'\
               -a"rq = "$REM_HOST_PRINTER 
         if test $? = 0; then
            logg "used mkque:, created remote printer queue $PRINTER"
            logg "  destination host is $REM_HOST_NAME"
            logg "  remote queue is "$REM_HOST_PRINTER
            mkquedev -q$PRINTER -d$PRINTER'd'\
                     -a"backend = "'/usr/lpd/rembak'
            if test $? = 0; then
               logg "used mkquedev, created printer queue device "$PRINTER"d" 
            else
               logg "mkquedev did not work."
               rmque -q $PRINTER
               error=YES
            fi
         else
            logg "mkque did not work."
            error=YES
         fi
         ;;
   esac
} # aix_install_printerP

# --- install_prosAP -----------------------------------------------------------
install_prosAP ()
{
   ftp_downloadP $REM_HOST_NAME npipe prosd.c
   test $ftp_download != 0 && error=YES
   
   # --- Compilatilon of prosd differ from BSD to SYSV, and even
   # --- worse, it differ from one SYSV to another.
   
   # If gcc exists, use it
   CC=cc
   gcc -v 1> /dev/null 2>&1
   test $? = 0 && CC=gcc
   
   case $sys_family in
      bsd)
         $CC $ALWAYS_OPEN $LINK_OPT -o prosd prosd.c
         if test $? != 0; then
            error=YES
            start_prosd=NO
         else
            cat <<- BSD_LOGNOTE_END | cat >> $instalog
   Compiled prosd in directory $PROSHOME.
   If you want the PROS daemon to start automatically at system reboot
   then add the following line to /etc/rc.local:

$PROSHOME/prosd $prosd_options 2> $PROSLOG 1>&2 &

BSD_LOGNOTE_END
         fi
         ;;
      sysv)
         compile_error=FirstTime
         altlist=" : -lsocket : -lsocket -lnsl : -linet "
         num_alts=4
         counter=1
   	 
         while test $compile_error != NO -a $counter -le $num_alts;
         do
            # --- Select one alternative of load libraries.
   	 
            LOADLIBES=`echo $altlist | cut -f$counter -d\:`
            echo "Compiling..."
	 
            # --- First make a try with load libraries after source
            # --- and if that fail then try with them before source.
   	 
            $CC -o prosd prosd.c $LOADLIBES 2> /dev/null
            if test $? != 0; then
               $CC -o prosd $LOADLIBES prosd.c 2> /dev/null
               if test $? != 0; then
                  compile_error=YES
               else
                  compile_error=NO
               fi
            else
               compile_error=NO
            fi
	 
            counter=`expr $counter + 1`
         done
         if test $compile_error != NO; then
            error=YES
            start_prosd=NO
         else
            cat <<- SYSV_LOGNOTE_END | cat >> $instalog
   Compiled prosd in directory $PROSHOME.
   If you want the PROS daemon to start automatically at system reboot
   then add the following line to /etc/inittab:

id01::respawn:$PROSHOME/prosd $prosd_options 2> $PROSLOG 1>&2

Note! The ID string 'id01' must be four (4) characters long and individual
      for every entry in /etc/inittab.
      On some systems, the /etc/inittab file is recreated each 
      time the system is booted, erasing the entries you just added. To 
      cure this problem, put the added lines in a separate file in the 
      /etc/conf/init.d directory as well. Any file name will do, such 
      as /etc/conf/init.d/printer-init. 

SYSV_LOGNOTE_END
         fi
         ;;
   esac
} # --- install_prosAP

# --- install_prosBP -----------------------------------------------------------
install_prosBP ()
{
   ftp_downloadP $REM_HOST_NAME $sys_family pros$sys_family.c
   test $ftp_download != 0 && error=YES
   
   # If gcc exists, use it
   CC=cc
   gcc -v 1> /dev/null 2>&1
   test $? = 0 && CC=gcc
   case $sys_family in
      bsd)
         CFLAGS="-DHOSTNAME=\"$HOSTNAME\" \
                 -DBOXNAME=\"$REM_HOST_NAME\" \
                 -DPRINTERNAME=\"$REM_HOST_PRINTER\""
         $CC $CFLAGS -o ${FILTER_NAME} prosbsd.c
         test $? != 0 && error=YES
         ;;
      sysv)
   
         CFLAGS="-DHOSTNAME=\"$HOSTNAME\" \
                 -DBOXNAME=\"$REM_HOST_NAME\" \
                 -DPRINTERNAME=\"$REM_HOST_PRINTER\""
         compile_error=FirstTime
         altlist=" : -lsocket : -lsocket -lnsl : -linet "
         num_alts=4
         counter=1
   	 
         while test $compile_error != NO -a $counter -le $num_alts; do
            # --- Select one alternative of load libraries.
   	 
            LOADLIBES=`echo $altlist | cut -f$counter -d\:`
            echo "Compiling..."
   	 
            # --- First make a try with load libraries after source
            # --- and if that fail then try with them before source.
   	 
            $CC -o ${FILTER_NAME} $CFLAGS prossysv.c $LOADLIBES \
                   2> /dev/null
            if test $? != 0; then
               $CC -o ${FILTER_NAME} $CFLAGS $LOADLIBES prossysv.c \
                      2> /dev/null
               if test $? != 0; then
                  compile_error=YES
               else
                  compile_error=NO
               fi
            else
               compile_error=NO
            fi
   	 
            counter=`expr $counter + 1`
         done
         test $compile_error != NO && error=YES
         ;;
      aix)
         $CC -o prosaix -DTRY_FOREVER prosaix.c
         ftp_downloadP $REM_HOST_NAME $sys_family pros_piobe
   
         PROSPATH=$PROSHOME"/prosaix"
         sedcmd="/^internet_address=.*/s/.*/internet_address=$REM_HOST_NAME/"
         sedcmd="$sedcmd;/^logical_printer=.*/ \
                 s/.*/logical_printer=$REM_HOST_PRINTER/"
         sedcmd="$sedcmd;/^pros_path=.*/s+.*+pros_path=$PROSPATH+"
         sedcmd="$sedcmd;/^hostname=.*/s/.*/hostname=$HOSTNAME/"
         sedcmd="$sedcmd;/^logfile=.*/s+.*+logfile=$PROSLOG+"
   
         sed -e "$sedcmd" pros_piobe > ${FILTER_NAME}
         test $? != 0 && error=YES
   
         rm -f pros_piobe
         chmod 755 ${FILTER_NAME} 
         logg "Placed ${FILTER_NAME} in directory $PROSHOME."
         ;;
   esac
   
   rm -f pros${sys_family}.c
   if test $sys_family = aix; then
     logg "Compiled pros${sys_family} in directory $PROSHOME."
   else
     logg "Compiled ${FILTER_NAME} in directory $PROSHOME."
   fi

} # --- install_prosBP

# --- install_ftpP -------------------------------------------------------------
install_ftpP ()
{
   ftp_downloadP $REM_HOST_NAME $sys_family ftp_$filt_name
   test $ftp_download != 0 && error=YES
   
   # Patch ftp filter with binary
   grep "binary" ftp_$filt_name > /dev/null
   if test $? != 0; then
      ed ftp_$filt_name <<- ED_END > /dev/null
1
/^user/a
binary
.
w
q
ED_END
   fi
   
#   ftp_path=`which ftp`
   ftp_path=`strip ${ftp_path}`

   if test "X${ftp_path}" = "X"; then
      myexit 1 "Could not find ftp program"
   fi

   sedcmd="/^internet_address=.*/ s/.*/internet_address=$REM_HOST_NAME/"
   sedcmd="$sedcmd;/^logical_printer=.*/ \
           s/.*/logical_printer=$REM_HOST_PRINTER/"
   sedcmd="$sedcmd;/^ftp_path=.*/s+.*+ftp_path=${ftp_path}+"
   
   sed -e "$sedcmd" ftp_$filt_name > ${FILTER_NAME}
   test $? != 0 && error=YES
   
   rm -f ftp_$filt_name
   
   chmod 755 ${FILTER_NAME} # rwx r-x r-x
   
   logg "Placed ${FILTER_NAME} in directory $PROSHOME."
} # --- install_ftpP

# --- compile_prosP ------------------------------------------------------------
compile_prosP ()
{
   if test $create_proshome = YES -a ! -f $PROSHOME; then
      mkdir -p $PROSHOME
      test $? != 0 && error=YES
      logg "Created directory $PROSHOME."
   fi
   
   prosd_options="$HOSTNAME $PROSDEV $REM_HOST_NAME $REM_HOST_PRINTER $PROSPWD"
   
   pwd=`pwd`; cd $PROSHOME
   case $print_method in
      prosA) install_prosAP ;;
      prosB) install_prosBP ;;
      ftp) install_ftpP ;;
      *) myexit 1 "compile_prosP: Unknown print method."
   esac
   cd $pwd
} # --- compile_prosP

# --- create_proslogP ---------------------------------------------------------
create_proslogP ()
{
   case $print_method in
      prosA) logsource="pros daemon" ;;
      prosB) logsource="pros filter" ;;
      ftp)   logsource="ftp filter"  ;;
   esac
   
   if test ! -d `dirname $PROSLOG`; then
      mkdir -p `dirname $PROSLOG`
      test $? != 0 && error=YES
      logg "Created $logsource log file directory `dirname $PROSLOG`. " 
   fi
   touch $PROSLOG
   test $? != 0 && error=YES
   logg "Created $logsource log file $PROSLOG."
   
   if test $sys_family = aix; then
      chmod 666 $PROSLOG
   fi
} # --- create_proslogP 

# --- create_prosdevP ----------------------------------------------------------
create_prosdevP ()
{
   if test ! -d `dirname $PROSDEV`; then
      mkdir -p `dirname $PROSDEV`
      test $? != 0 && error=YES
      logg "Created prosd pipe directory `dirname $PROSDEV`. "
   fi
   
   if test $sys_num -ne $FreeBSD_n; then
     $mknod $PROSDEV p
   else
     mkfifo $PROSDEV
   fi
   test $? != 0 && error=YES
   
   case $sys_family in
   bsd)
      chmod 660 $PROSDEV # rw- rw- ---
      ;;
   sysv)
      chmod 600 $PROSDEV # rw- --- ---
      chown lp $PROSDEV
      ;;
   esac
   
   logg "Made prosd pipe $PROSDEV."
} # --- create_prosdevP

# --- start_prosdP ------------------------------------------------------------
start_prosdP ()
{
   prosd_options="$HOSTNAME $PROSDEV $REM_HOST_NAME $REM_HOST_PRINTER $PROSPWD"
   nohup $PROSHOME/prosd $prosd_options 2> $PROSLOG 1>&2 &
   test $? != 0 && error=YES
   
   cat <<- PROSD_LOGNOTE_END | cat >> $instalog
Started prosd in directory $PROSHOME for device $PROSDEV with command:
   
nohup $PROSHOME/prosd $prosd_options 2> $PROSLOG 1>&2 &
   
PROSD_LOGNOTE_END
   if test $sys_family=sysv; then
      cat <<- PROSD_SYSV_STRT_END | cat >> $instalog
If you want the PROS daemon to start automatically at system reboot
then add the following line to /etc/inittab:

id01::respawn:$PROSHOME/prosd $prosd_options 2> $PROSLOG 1>&2

PROSD_SYSV_STRT_END
   fi
   echo "Started prosd for device $PROSDEV."
   echo "See log file for more information."

   # --- Install the prosd startup line in /etc/inittab and place a
   # --- line in /etc/conf/init.d/$CAT_NAME-init.
} # --- start_prosdP

# --- install_printerP ---------------------------------------------------------
# --- Install the specified printer.
install_printerP ()
{         
   logg " ========================= Install $PRINTER ========================"
   echo "Installing printer $PRINTER..."
   
   # compile_pros is YES for prosB, prosA and ftp
   test $compile_pros = YES   && compile_prosP
   test $create_proslog = YES && create_proslogP
   test $create_prosdev = YES && create_prosdevP
   test $start_prosd = YES    && start_prosdP

   case $sys_family in
      bsd)  bsd_install_printerP ;;
      sysv) sysv_install_printerP ;;
      aix)  aix_install_printerP ;;
   esac
   
   logg
   if test $error = NO; then
      if test $sys_num = $Linux_n; then
	 lpq -V 2>&1 | grep LPRng >/dev/null
	 if test $? = 0; then
	    echo In order to start the printer now you need to restart lpd
	    echo You can also restart lpd later
	    yes_or_noP "Do you want to restart lpd now" y
	    if test $yes_or_no = YES; then
	       killall -9 lpd
	       lpd
	    fi
	 fi
      fi
      lecho "Installation of printer $PRINTER done."
      echo
   
      yes_or_noP "Do you want a test printout" y
      if test $yes_or_no = YES; then
         case $sys_family in
            bsd)
               echo | tr '\012' '\014' | cat $instalog - | lpr -P$PRINTER
               ;;
            sysv)
               echo | tr '\012' '\014' | cat $instalog - | lp -d$HPFIX$PRINTER
               ;;
            aix)
               echo | tr '\012' '\014' | cat $instalog - | qprt -P$PRINTER
               ;;
         esac
      fi
   else
      lecho "Installation of printer $PRINTER FAILED."
   fi
   logg " ==================== Finished install $PRINTER ==================="
   logg
   echo # Not to log file

} # --- install_printerP

# --- main_install -----------------------------------------------------------
main_install ()
{
   # Init some system dependent and other variables
   install_initP

   saved_sys_type=$sys_type
   
   more_printers=YES
   while test $more_printers = "YES"; do
      echo
      echo "Select a print method from the list below."
   
      # Make sure that we reset sys_type here, it might be set to
      # something else in some cases below.
      sys_type=$saved_sys_type
   
      case $sys_type in
      sysv4|bsd|aix)
         echo "The recommended basic print method is LPD, for more advanced "
         echo "functionality and status feedback use PROS."
         prompt_print_methodP lpd 
         ;;
   
      sysv3)
         echo "The recommended basic print method is FTP, for more advanced "
         echo "functionality and status feedback use PROS."
         prompt_print_methodP ftp 
         ;;
      *) myexit 1 "Unknown system type when selecting preferred print method"
      esac
      print_method=$prompt_print_method

      # Pretend that it is a BSD system if it is a sysv3 system with bsd spool
      if test $print_method = lpd && test ${sysv3_bsd_lpd} = YES; then
         $sys_type=BSD
      fi
   
      # Set common variables that depends on the printing method
      install_init_methodP
   
      # --- Here starts the installation loop ----
      
      # First time, dont ask for all details, use defaults
      # unless user wants advanved mode from beginning.
      set_ask_configP ${advanced}
      more_changes=YES
      while test $more_changes = "YES"; do
   
         abort_install=NO
   
         barf_setupP
   
         echo "                  ---------------------------------"
         echo "                      1....Install printer"
         echo "                      2....Change configuration"
         echo "                      0....Abort installation"
         echo "                  ---------------------------------"
         echo
         range_answerP "                  Choose action" "[0-2]" 1

         case ${range_answer} in
            1) check_setupP ;;     # Sets more_changes if conflicts found
            2) more_changes=YES ;; # More changes
            0) 
               more_changes=NO
               abort_install=YES
               ;;
         esac 
   
         # If user wants to change something, let him see 
         # all options
         if test $more_changes = "YES"; then
            set_ask_configP YES
         fi
   
      done # while $more_changes

      if test $more_changes = "NO" && test $abort_install = "NO"; then
         error=NO
         install_printerP
      fi # do_install

      yes_or_noP "Do you want to install more printers" n
      more_printers=$yes_or_no
   done # while more_printers

} # --- main_install

# ============================================================================
#                              Uninstaller Code 
# ============================================================================

# --- bsd_create_printer_listP -----------------------------------------------
# --- Create a file named 'printers' in TMPDIR with all printers
# --- found in the /etc/printcap file
bsd_create_printer_listP() 
{
   if test ! -f $PRINTCAP ; then
      myexit 1 "There was no $PRINTCAP file."
   fi

   # --- Remove lines that start with # (comment) or :, whitespaces before
   # --- are accepted. Then match lines like '  printer| comment ' and 
   # --- extract 'printer' and 'comment'
   cat <<END1 > $TMPDIR/sedfile
      /^[	   ]*[:#]/  d
   
      /^[	   ]*[^|]*|[^:]*/  {
         s/^[      ]*\([^|]*\)|\([^:]*\).*$/\1:  	\2/p
      }
END1
   sed -n -f $TMPDIR/sedfile $PRINTCAP > $TMPDIR/printers 
} # --- End of bsd_create_printer_listP


# --- sysv_create_printer_listP -----------------------------------------------
# --- Create a file named 'printers' in TMPDIR with all printers
# --- found in by issue the lpstat command
sysv_create_printer_listP() 
{
    # --- Extract 'printer' and some other info and print in nicer format 
    cat <<END3 > $TMPDIR/sysv_sed_list
      /^printer/  {
        s/^printer \([^ ]*\) .*\(enabled.*\)$/\1:  	\2/p
      }
END3

    if test -n ${LC_MESSAGES} && test "X${LC_MESSAGES}" != "Xen_US" ; then
	logg "Temporary changing locale"
	
	LC_MESSAGES="en_US"
	export LC_MESSAGES
	logg "LC_MESSAGES is $LC_MESSAGES"
    fi

   lpstat -p | sed -n -f $TMPDIR/sysv_sed_list > $TMPDIR/printers
#    cat /etc/printers.conf | grep '^[a-zA-Z0-9]' | sed -e 's/:\\//g' > $TMPDIR/printers    
} # --- End of sysv_create_printer_listP


# --- aix_create_printer_listP -----------------------------------------------
# --- Create a file named 'printers' in TMPDIR with all printers
# --- found in by issue the lpstat command
aix_create_printer_listP() 
{
    # Label over columns in qchk output start with 'Queue' and then there is
    # a separator line, these are remove by the first two lines below.
    # The third is to get the first two colums, queue name and device name,
    # separated by whitespeces. No colon (:) is accepted, this is output from 
    # remote queue. 
    cat <<EOF > $TMPDIR/aix_sed_list
        /^Queue/     d
        /^----/      d

        /^[^: 	]*[ 	]\{1,\}[^: 	]*/ {
	    s/^\([^: 	]*\)[ 	]\{1,\}\([^: 	]*\).*/\1:  	On dev \2/p
        }
EOF

    # list all printers in wide format and do not contact remote hosts
    enq -A -W -s | sed -n -f $TMPDIR/aix_sed_list > $TMPDIR/printers
} # --- End of aix_create_printer_listP


# --- choose_printerP --------------------------------------------------------
# --- Choose a printer from the ones available in the system.
# --- Returns empty string if no printer is choosen.
# --- Uses the *_create_printer_listP functions that should create 
# --- a file 'printers' that looks like below for each printer found.
# --- ^printer_name:__\tcomment$, where    
# --- ^ = beginning of line, _ = space, \t = tab, $ = end of line)
choose_printerP() 
{
    choose_printer=
 
    echo
    echo "Please wait, retreiving list of printers..."

    # --- Create the 'printers' file.
    case $sys_family in
	bsd)  bsd_create_printer_listP ;;
        sysv) sysv_create_printer_listP ;;
        aix)  aix_create_printer_listP ;;
        *) myexit 1 "'choose_printerP' not implemented for $sys_family yet" ;;
    esac
   test ! -f $TMPDIR/printers && myexit 1 "$TMPDIR/printers does not exist!"
   # --- Remove the printers that are receptors for internal bounce queues
   sed -e '/bounced/ d' $TMPDIR/printers > $TMPDIR/printers1

   # --- Add a number before each printer and display choices to user.
   sed = $TMPDIR/printers1 | \
      sed -n -e 'N; s/^/    /; s/ *\(.\{4,\}\)\n/\1: /p' > $TMPDIR/printers2
   clear_init
   cat <<EOF_PLIST
 
                             Available printers:
-------------------------------------------------------------------------------
`cat $TMPDIR/printers2`
-------------------------------------------------------------------------------

EOF_PLIST
   # --- Find last number in results file and ask which printer 
   # --- the user wants to delete. 
   last_num=`tail -1 $TMPDIR/printers2 | cut -f1 -d:`
   last_num=`strip $last_num`
   
   range_answerP "      Number of printer to delete (0 aborts)" \
                 "[0-$last_num]"

   # Return empty string if answer is 0
   if test "X$range_answer" != "X0"; then
      # --- Search for printer name in result file by using given number
      # --- Match line with answer and print printer name
      cat << END2 > $TMPDIR/sedfile2
         /^[^0-9]*$range_answer:/  s/^[^0-9]*$range_answer:\([^:]*\):.*/\1/p
END2
      choose_printer=`sed -n -f $TMPDIR/sedfile2 $TMPDIR/printers2`
#      cat /etc/printers.conf | grep '^[a-zA-Z0-9]' | sed -e 's/:\\//g' > $TMPDIR/printers    
      choose_printer=`strip $choose_printer`
   fi

} # --- End of choose_printerP


# --- bsd_extract_pcapP ------------------------------------------------------
# --- Extract printers printcap entry
# --- $1 = the desired printer
# --- $2 = the desired output file
bsd_extract_pcapP()
{
   # --- Search for printer name from line one in file, set bookmark a
   # --- from a, search for a line that does not end with '\', bookmark b
   # --- delete lines between a and b, save and quit.
   # --- save the output in a file, Default is ${TMPDIR}/bsd_pcap_extracted
   bsd_extract_outfile=${TMPDIR}/bsd_pcap_extracted 
   test $# -ge 2 && bsd_extract_outfile=$2 
   cat << END_PCAP > $TMPDIR/pcap_extract
      1
      /^[ 	]*${1}/ ka
      'a
      /[^\]$/ kb
      'b
      'a,'b w $bsd_extract_outfile
      q
END_PCAP

   ed -s ${PRINTCAP} < ${TMPDIR}/pcap_extract > /dev/null
} # --- End of bsd_extract_pcapP


# --- add_cmdP $1 -----------------------------------------------------------
# --- Add a command that should be executed when uninstalling the printer
# --- Command output will be written to install log
add_cmdP ()
{
   if test ! -f ${TMPDIR}/commands; then
      touch ${TMPDIR}/commands
   fi
   echo "echo '--- $1:' >> $instalog" >> ${TMPDIR}/commands
   echo "$1 2>&1 | tee -a $instalog" >> ${TMPDIR}/commands
} # --- add_cmdP

# --- find_out_filter_typeP $1 ----------------------------------------------
# --- $1 - name of filter.
find_out_filter_typeP ()
{
   printer_type=unknown
   tmp_interface=$1
   if test -f ${tmp_interface}; then
      if grep ftp ${tmp_interface} > /dev/null; then
         printer_type=ftp
         printer_rm=`extract_valueP $tmp_interface internet_address`
         printer_rp=`extract_valueP $tmp_interface logical_printer`
         printer_desc="FTP printer $printer_rp on host $printer_rm"
      elif strings $tmp_interface | grep netprinter > /dev/null;
      then
         # prosb has a string in it that is netprinter
         printer_type=prosb
         printer_desc="PROSB printer"
      fi
   fi

   if test "X${printer_type}" = Xunknown; then
      lecho "Could not find out type of printer by file ${tmp_interface}"
   fi
} # --- find_out_filter_typeP

# --- bsd_present_filesP ----------------------------------------------------
#bsd_present_filesP ()
# --- bsd_get_printer_type $1 ----------------------------------------------
# --- $1 = printer 
# --- affected variables:  bsd_bounce, bsd_get_filter
# --- affected files :     $TMPDIR/bsd_pcap_extracted 
bsd_get_printer_type ()
{
#   printer_type=unknown
   bsd_extract_pcapP $1 $TMPDIR/bsd_pcap_extracted
   printer_device=`extract_valueP $TMPDIR/bsd_pcap_extracted lp`
   if test "X$printer_device" = "X"; then
      printer_type=lpd
      printer_rm=`extract_valueP $TMPDIR/bsd_pcap_extracted rm`
      printer_rp=`extract_valueP $TMPDIR/bsd_pcap_extracted rp`
      printer_desc="LPD printer $printer_rp on host $printer_rm"
   else
      # ftp and prosb has null device, prosa has named pipe
      echo $printer_device | grep null > /dev/null
      if test $? -ne 0; then
	    bsd_bounce=`extract_valueP $TMPDIR/bsd_pcap_extracted bq`
	    if test "X$bsd_bounce" = "X"; then
		myecho "prosa"
	    else
		myecho "bounce"
	    fi
	else
	    bsd_get_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted of`
	    if test "X${bsd_get_filter}" = "X"; then
		bsd_get_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted if`
	    fi
	    if test "X$bsd_get_filter" != "X"; then
		if test -f ${bsd_get_filter}; then
		    if grep ftp ${bsd_get_filter} > /dev/null; then
			myecho "ftp"
		    elif strings ${bsd_get_filter} | grep netprinter > /dev/null;
			then
			myecho "prosb"
		    else
			myecho "unknown"
		    fi
		else
		    myecho "unknown"
		fi
	    else
		myecho "unknown"
	    fi
	fi
    fi
} # --- end of bsd_get_printer_type
# --- bsd_present_files_n_P ------------------------------------------------
bsd_present_files_n_P ()
{
    # get the printer type
    printer_type=`bsd_get_printer_type $PRINTER`
    

    # If the printer has the type bounce it needs special treatment
    bsd_bounce=
    if test "$printer_type" = "bounce" ; then
	bsd_bounce=`extract_valueP $TMPDIR/bsd_pcap_extracted bq = @`
	printer_type=`bsd_get_printer_type ${bsd_bounce}`
	# bsd_pcap_extracted exists an bsd_bounce	
    fi
    
    #collect some general info
    printer_device=`extract_valueP $TMPDIR/bsd_pcap_extracted lp`
    printer_logfile=`extract_valueP $TMPDIR/bsd_pcap_extracted lf`
    printer_spooldir=`extract_valueP $TMPDIR/bsd_pcap_extracted sd`
    logg "Printer device = $printer_device"
    test "$printer_type" = "unknown" && printer_desc="Unknown printer type"
    
    # output of information specific for the type and preparation
    # of removing type specific files
    case $printer_type in
	lpd)
	    printer_rm=`extract_valueP $TMPDIR/bsd_pcap_extracted rm`
	    printer_rp=`extract_valueP $TMPDIR/bsd_pcap_extracted rp`
	    printer_desc="LPD printer $printer_rp on host $printer_rm"
	    printer_ifilter=`extract_valueP $TMPDIR/bsd_pcap_extracted if`
	    printer_model=`extract_valueP $TMPDIR/bsd_pcap_extracted ifhp`
	    echo "Description    : $printer_desc"
	    echo "Log file       : $printer_logfile"
	    add_cmdP "rm $printer_logfile"
	    if test "X$printer_ifilter" != "X"; then
		echo "Input filter   : $printer_ifilter"
	    fi
	    if test "X$printer_model" != "X"; then
		echo "Printer model  : $printer_model"
	    fi
	;;
	prosa)
	    printer_desc="PROSA printer"
	    echo "Description    : $printer_desc"
	    echo "Log file       : $printer_logfile"
	    add_cmdP "rm $printer_logfile"
	    echo "printer device : $printer_device"
	    add_cmdP "rm $printer_device"
	    printer_ifilter=`extract_valueP $TMPDIR/bsd_pcap_extracted if`
	    printer_model=`extract_valueP $TMPDIR/bsd_pcap_extracted ifhp`
	    printer_prosa_log=`eval ${DEF_PROSLOG_EVAL}`
	    test ! -f ${printer_prosa_log} && printer_prosa_log=
#	    if test -n $printer_prosa_log; then
	    if test "X$printer_prosa_log" != "X"; then
		echo "PROSA log file : ${printer_prosa_log}"
		add_cmdP "rm ${printer_prosa_log}"
	    else
		echo "Prosa log file will not be removed"
	    fi
	    if test "X$printer_ifilter" != "X"; then
		echo "Input filter   : $printer_ifilter"
	    fi
	    if test "X$printer_model" != "X"; then
		echo "Printer model  : $printer_model"
	    fi
	;;
	ftp)
	    ftp_ax_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted of`
	    if test "X${ftp_ax_filter}" = "X"; then
		ftp_ax_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted if`
	    fi
	    printer_rm=`extract_valueP $ftp_ax_filter internet_address`
	    printer_rp=`extract_valueP $ftp_ax_filter logical_printer`
	    printer_desc="FTP printer $printer_rp on host $printer_rm"
	    echo "Description    : $printer_desc"
	    echo "Log file       : $printer_logfile"
	    echo "Printer device : $printer_device"
	    echo "Printer filter : $ftp_ax_filter"
	    add_cmdP "rm $ftp_ax_filter"
	    if test "X$bsd_bounce" != "X"; then
		echo "Bounce queue   : $PRINTER --> $bsd_bounce"
		bsd_extract_pcapP $PRINTER $TMPDIR/bounce_queue_extracted
		printer_ifilter=`extract_valueP $TMPDIR/bounce_queue_extracted if`
		printer_model=`extract_valueP $TMPDIR/bounce_queue_extracted ifhp`
		echo "Input filter   : $printer_ifilter"
		echo "Printer model  : $printer_model"
	    fi
	;;
	prosb)
	    prosb_ax_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted of`
	    if test "X${prosb_ax_filter}" = "X"; then
		prosb_ax_filter=`extract_valueP $TMPDIR/bsd_pcap_extracted if`
	    fi
	    printer_desc="PROSB printer"
	    echo "Description    : $printer_desc"
	    echo "Log file       : $printer_logfile"
	    echo "Printer device : $printer_device"
	    echo "Printer filter : $prosb_ax_filter"
	    add_cmdP "rm $prosb_ax_filter"
	    if test "X$bsd_bounce" != "X"; then
		echo "Bounce queue   : $PRINTER --> $bsd_bounce"
		bsd_extract_pcapP $PRINTER $TMPDIR/bounce_queue_extracted
		printer_ifilter=`extract_valueP $TMPDIR/bounce_queue_extracted if`
		printer_model=`extract_valueP $TMPDIR/bounce_queue_extracted ifhp`
		echo "Input filter   : $printer_ifilter"
		echo "Printer model  : $printer_model"
	    fi
	;;
	*)
	    echo "Description    : $printer_desc"
	    echo "Log file       : $printer_logfile"
	    echo "Unknown printer type, all files may not be removed"
	;;
    esac
    echo "Spool dir      : $printer_spooldir"
    add_cmdP "rm -rf $printer_spooldir"
    echo "Printcap file ${PRINTCAP} will be modified."
} # --- end of bsd_present_files_n_P


# --- aix_present_filesP ----------------------------------------------------
aix_present_filesP ()
{
   printer_type=unknown
   lsque -q $PRINTER > $TMPDIR/aix_lsque_extracted
   printer_device=`extract_valueP $TMPDIR/aix_lsque_extracted device`
   printer_rm=`extract_valueP $TMPDIR/aix_lsque_extracted host`

   if test "X$printer_rm" != "X"; then
      printer_type=lpd
      printer_rp=`extract_valueP $TMPDIR/aix_lsque_extracted rq`
      printer_desc="LPD printer $printer_rp on host $printer_rm"
   else
      lsquedev -q $PRINTER -d $printer_device > $TMPDIR/aix_lsquedev_extracted
      printer_filter=`extract_valueP $TMPDIR/aix_lsquedev_extracted backend`

      find_out_filter_typeP ${printer_filter}

      if test $printer_type = prosb; then
         printer_rm=`extract_valueP $printer_filter internet_address`
         printer_rp=`extract_valueP $printer_filter logical_printer`
         printer_logfile=`extract_valueP $printer_filter logfile`
         printer_desc="PROSB printer $printer_rp on host $printer_rm"
      fi
   fi # test "X$printer_rm" != "X"

   echo
   test $printer_type = unknown && printer_desc="Unknown type"
   echo "Desciption     : $printer_desc"

   if test $printer_type != lpd; then
      add_cmdP "rmvirprt -q $PRINTER -d $printer_device"
   fi
   echo "Queue Device   : $printer_device"
   add_cmdP "rmquedev -q $PRINTER -d $printer_device"
   add_cmdP "rm /dev/$printer_device > /dev/null 2>&1"
   add_cmdP "rmque -q  $PRINTER"

   if test "X$printer_logfile" != "X"; then
      echo "Log file       : $printer_logfile"
      add_cmdP "rm $printer_logfile"
   fi

   case $printer_type in
      ftp|prosb) 
         echo "Printer filter : $printer_filter"
         add_cmdP "rm $printer_filter"
         ;;
      unknown)
         lecho "Unknown printer type, all files may not be removed"
         ;;
   esac
} # --- aix_present_filesP

# --- sysv_config_file_printerP $1 -------------------------------------------
# --- Determine printer type from configuration file in system
# --- Only Solaris 2.6 has this, but maybe other systems are compatible?
# --- $1 - configration file.
# --- Output: printer_type, printer_desc for all printers
#             printer_interface for ftp and pros.
sysv_config_file_printerP ()
{
   sysv_config_file=$1
   # Solaris 2.6 has a configuration file for each printer
   printer_interface=`extract_valueP ${sysv_config_file} Interface :`
   if echo ${printer_interface} | grep netstandard > /dev/null ; then
      # solaris 2.6 new lpd method or reverse telnet
      printer_proto=`extract_valueP ${sysv_config_file} protocol = ,`
      printer_dest=`extract_valueP ${sysv_config_file} dest = ,`
      if test "X$printer_proto" = "Xbsd"; then
         printer_type=lpd
         printer_rm=`echo ${printer_dest} | cut -d: -f1`
         printer_rp=`echo ${printer_dest} | cut -d: -f2`
         printer_desc="LPD printer ${printer_rp} on ${printer_rm}"
      else
         lecho "No support for protocol '$printer_proto' in netstandard"
         printer_type=unknown
      fi
   else
      find_out_filter_typeP ${printer_interface}
   fi
} # --- sysv_config_file_printerP

# --- sysv_interface_lookupP ------------------------------------------------
# --- Try to find out printer type by looking up the interface
# --- Output: printer_type, printer_desc for all printers
#             printer_interface for ftp and pros.
sysv_interface_lookupP ()
{
   printer_type=unknown
   printer_home=${DEF_PROSHOME}

   if test ! -d ${printer_home} ; then
      myecho "Cannot find interface or filter directory."
      myecho "The default path is ${printer_home}."
      myecho "Please give path to directory, or just '/'"
      myecho "if the path is unknown."
      while true; do
         path_answerP "Absolute path"
         printer_home=$path_answer
         if -d ${printer_home}; then 
            break
         fi
      done
   fi
   if test "X$printer_home" != "X/"; then
      printer_filter_ftp=`eval ${DEF_FTP_FILE_EVAL}`
      printer_filter_pros=`eval ${DEF_PROSB_FILE_EVAL}`
      if test -f ${printer_home}/${printer_filter_ftp}; then
         printer_interface=${printer_home}/${printer_filter_ftp}
      elif test -f ${printer_home}/${printer_filter_pros}; then
         printer_interface=${printer_home}/${printer_filter_pros}
      else
         echo "Cannot find interface file for $PRINTER in ${printer_home}."
         echo "The deafult name would be ${printer_filter_ftp} if it is a"
         echo "ftp printer and ${printer_filter_pros} if it is a pros printer"
         echo "Please give name of interface, relative to ${printer_home},"
         echo "or as an absolute path, beginning with '/'."
         echo "If the name is unknown, enter '?'"

         while true; do
            answerP "Interface name" "?"
            printer_filter=$answer

            case ${printer_filter} in
               ?)  printer_filter= ; break ;;
               /*) ;; # Absolute path, do nothning
               *)  printer_filter=${printer_home}/${printer_filter} ;;
            esac

            if test -f  ${printer_filter} ; then
               break
            else
               lecho "The file ${printer_filter} doesn't exist, choose another"
            fi
         done

         if test "X${printer_filter}" != "X" && test -f  ${printer_filter}; 
         then
            printer_interface=${printer_filter}
         fi
      fi

      if test "X${printer_interface}" != "X"; then
         find_out_filter_typeP ${printer_interface}
      fi
   fi
   
   if test "X${printer_type}" = Xunknown; then
      lecho "Could not find printer type by interface"
      printer_interface=
   fi   
} # --- sysv_interface_lookupP


# --- sysv_present_filesP ----------------------------------------------------
sysv_present_filesP ()
{
   lpstat -v$PRINTER > $TMPDIR/sysv_lpstat_dev

   printer_type=unknown
   sed_cmd="sed -ne '/^device [^:]*:/ s/^device [^:]*:\(.*\)/\1/p'"
   printer_device=`eval "$sed_cmd $TMPDIR/sysv_lpstat_dev"`
   printer_device=`strip ${printer_device}`
   sysv_config_file="/etc/lp/printers/$PRINTER/configuration"

   if test "X${printer_device}" != "X"; then
      # In here, device should only be PROSA pipe or /dev/null
      if test -p ${printer_device}; then
        printer_type=prosa
        printer_desc="PROSA printer"
        printer_prosa_log=`eval ${DEF_PROSLOG_EVAL}`
        test ! -f ${printer_prosa_log} && printer_prosa_log=
      elif echo ${printer_device} | grep '/dev/null' > /dev/null; then
        # FTP or PROSB or LPD in Solaris 2.6+ using netstandard
        if grep 'remote to:' $TMPDIR/sysv_lpstat_dev > /dev/null 2>&1; then
           # HP-UX: 
           # device for lp9100_lpd5: /dev/null
           #      remote to: pr5 on lp9100 
           printer_type=lpd
           sed_cmd="sed -ne '/remote to:/ s/.*:\(.*\)$/\1/p'"
           printer_remote=`eval "$sed_cmd $TMPDIR/sysv_lpstat_dev"`
           printer_desc="LPD printer $printer_remote"
        elif test -f ${sysv_config_file}; then
           sysv_config_file_printerP ${sysv_config_file}
        else
           sysv_interface_lookupP
        fi

      else
         lecho "Unknown device ${printer_device}"
      fi
   elif grep '^system for' $TMPDIR/sysv_lpstat_dev > /dev/null 2>&1; then
      # solaris 2.x : 
      # system for pelle: nps (as printer pr7)
      # this is old variant, using lpadmin -s, installed by axinstall < 1.9.0
      printer_type=lpd
      printer_remote=`sed -ne '/:/ s/.*:\(.*\)$/\1/p' $TMPDIR/sysv_lpstat_dev`
      printer_desc="LPD printer $printer_remote"
   else
      lecho "Could not determine type of $PRINTER"
   fi


   echo
   test $printer_type = unknown && printer_desc="Unknown type"
   echo "Desciption     : $printer_desc"

   test ${stoplp} = YES && add_cmdP "lpshut"
   add_cmdP "lpadmin -x${HPFIX}${PRINTER}"
   test ${stoplp} = YES && add_cmdP "lpsched"

   case $printer_type in
      prosa)
         echo "Printer device : $printer_device"
         add_cmdP "rm ${printer_device}" 
         if test -n $printer_prosa_log; then 
            echo "PROSA log file: ${printer_prosa_log}"
            add_cmdP "rm ${printer_prosa_log}"
         else
            echo "PROSA log file will not be removed"
         fi
         ;;
      ftp|prosb) 
         echo "Printer interface : $printer_interface"
         add_cmdP "rm $printer_interface"
         ;;
      lpd)
         if test $sys_num -eq $HPUX_n && \
            test -f ${DEF_HP_LPDMODELDIR}/${PRINTER}; 
         then
            echo "Printer model file : ${DEF_HP_LPDMODELDIR}/${PRINTER}"
            add_cmdP "rm ${DEF_HP_LPDMODELDIR}/${PRINTER}"
         fi   
         ;;
      unknown)
         lecho "Unknown printer type, some files may not be removed."
         ;;
   esac 
} # --- sysv_present_filesP

# --- present_filesP ---------------------------------------------------------
# --- Show the user what kind of printer he is about to remove.
present_filesP ()
{
  printer_type=unknown
  print_interface=
  printer_desc=
  printer_filter=
  printer_device=
  printer_home=
  printer_spooldir=
  printer_logfile=
  bsd_bounce=
  # Remove old commands file if it still exists
  rm ${TMPDIR}/commands > /dev/null 2>&1 

  case $sys_family in
      bsd)  bsd_present_files_n_P ;;
      aix)  aix_present_filesP ;;
      sysv) sysv_present_filesP ;;
      *) 
         myexit 1 "present_filesP - does not support system family $sys_family"
         ;;
   esac
} # --- End of present_filesP

# --- bsd_remove_pcap_entryP $1 -----------------------------------------------
# --- Removes the printer entry in the printcap file and makes
# --- a backup of the old printcap in /tmp/printcap.$PRINTER
# --- $1 is the printer name for the entry to be removed - Stefan U Andersson
bsd_remove_pcap_entryP()
{

   logg "Will change ${PRINTCAP}, backup is saved in /tmp/printcap.${1}"
   # --- Make a backup file in tmp
   cp ${PRINTCAP} /tmp/printcap.${1}

   # --- if the printer's not in the file, we may remove the first entry ... 
   grep ${1} ${PRINTCAP} > /dev/null
   if test $? != 0; then
      myexit 1  "Printer ${1} is not present in printcap file!"
   fi

   # FIX to allow deletion of printer that is on first line 
   # in printcap file, simply add an empty line, unless there is
   # one already.
   bytes_on_first_line=`head -1 ${PRINTCAP} | wc -c`
   bytes_on_first_line=`strip $bytes_on_first_line`
   if test "X$bytes_on_first_line" != "X1"; then
      ed ${PRINTCAP} <<- END_PCAPADD > /dev/null
1
i

.
w
q
END_PCAPADD
   fi


   # --- Search for printer name from line one in file, set bookmark a
   # --- from a, search for a line that does not end with '\', bookmark b
   # --- delete lines between a and b, save and quit.
   cat << END_PCAP > $TMPDIR/pcap_edit
      1
      /^[ 	]*${1}/ ka
      'a
      /[^\]$/ kb
      'b
      'a,'b d
      w
      q
END_PCAP

   # --- Now, edit!.
   ed -s ${PRINTCAP} < ${TMPDIR}/pcap_edit  > /dev/null
   if test $? != 0; then
      lecho "${1} could not be removed from ${PRINTCAP}."
   fi
} # --- End of bsd_remove_pcap_entryP

# --- remove_printerP --------------------------------------------------------
# --- This function removes the printer from the system
remove_printerP()
{
    logg
    logg "================== Removing printer ${PRINTER} =================="
    case $sys_family in
       bsd) if test "X$bsd_bounce" != "X"; then
		bsd_remove_pcap_entryP $bsd_bounce
	    fi
	   bsd_remove_pcap_entryP $PRINTER 
	   ;;
    esac

    if test -f ${TMPDIR}/commands; then
       logg "Executing the following commands to remove printer:"
       sh ${TMPDIR}/commands
       rm ${TMPDIR}/commands
    fi
    logg "============== Finished Removing printer ${PRINTER} ==============="
    logg
} # --- End of remove_printerP

# --- main_uninstall --------------------------------------------------------
main_uninstall() 
{

   will_continue=YES
   while test ${will_continue} = YES; do

      choose_printerP
      PRINTER=$choose_printer
      if test "X${PRINTER}" = "X"; then
         break
      fi

      echo
      # Add some fact collecting here, and present what will be deleted
      present_filesP
      echo
      yes_or_noP "Do you want to remove the printer ${PRINTER}"

      if test $yes_or_no = YES; then
         remove_printerP
      fi 

      yes_or_noP "Do you want to remove another printer" y
      will_continue=$yes_or_no    
   done #  ${will_continue} = YES  
} # --- End of main_uninstall

# 
# Get additional resources from ~/.axinstallrc
#
#
get_rc ()
{
    if test "X${axinstallrc}" != "X"; then
	if test -f ${axinstallrc}; then
	    echo "Reading user defined parameters from ${HOME}/.axinstallrc"
	    . ${axinstallrc}
	fi
    fi
}

#
# Put them back if any
#
put_rc ()
{
    if test "X${axinstallrc}" != "X"; then
	if test -f ${axinstallrc}; then
	    echo "found ${axinstallrc}"
	    sed '/ftp_path/d' ${axinstallrc} > /tmp/ax-tmp-rc
	    sed '/source_path/d' /tmp/ax-tmp-rc > ${axinstallrc}
	    rm /tmp/ax-tmp-rc
	else
	    echo "Creates ${HOME}/.axinstallrc for user defined values"
	    cat > ${axinstallrc} <<RC_INIT_EOF
#
# .axinstallrc
#
# This file is used by axinstall to store variables
#
RC_INIT_EOF
	fi
	echo "Saves user defined values in ${HOME}/.axinstallrc"
	cat >> ${axinstallrc} <<RC_EOF
ftp_path=${ftp_path}
source_path=${source_path}
RC_EOF
    fi
}
# ============================================================================
#                         Main Function and Control
# ============================================================================

main_func ()
{
   initilize
   get_rc
   check_programsP
   os_type_initP
   default_ans=1

   while true; do
      clear_init
      cat <<EOF


                   ---------------------------------
                        1.....Install new printer
                        2.....Remove printer
                        3.....View log file
                        4.....Delete log file
                        0.....Exit program
                   ---------------------------------

EOF
      range_answerP "                   Your choice" "[0-4]" 1
      case $range_answer in
         0) break ;;
         1) main_install ;;
         2) main_uninstall ;;
         3) 
            if test -f $instalog; then
               more $instalog
               echo "Press Enter"
               read kanalbat 
            fi
            ;;
         4) test -f $instalog && rm $instalog ;;
      esac
      default_ans=0
   done
}

main_func
put_rc
myexit 0
