#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_printer,v 1.14 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, Germany
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Fabian Herschel
#
##############################################################################
#
# make_printer
# configures the printer subsystem
#
# Sections: printer
# Tags:     PRINTER_QUEUES, ALLOWED_PRINT_CLIENTS, START_LPD
#
##############################################################################
#
# NOTES:
#     make_printer does not build the directoty structures at this time
#     this will be implemented later, maybe by using a suse std interface
# TODO: Make it possible to configure CUPS
# TODO: Update for SLES8 and above
#
usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn"
}

test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir


while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) export fqhn=$2; shift
            ;;

   esac
   shift
done

. $alice_dir/lib/alicerc
WELCOME

#
############################################################################
#
# And now we begin - get the configuration
#
GET_CONFIGURATION printer  $CFG_DEBUG
if [ -z "$PRINTER_QUEUES" ]
then
   GOOD_BYE
fi
#
##########################################################################
#
#
# first let us build the access list for lp clients
#
BACKUP /etc/hosts.lpd
(
    echo "# /etc/hosts.lpd"
    echo "# generated by alice $(date)"
    if [ -n "$ALLOWED_PRINT_CLIENTS" ]
    then
       for cl in $ALLOWED_PRINT_CLIENTS
       do
          echo $cl
       done
    else
       echo "# list of allowed clients is blank!!"
    fi
) > /etc/hosts.lpd
#
##########################################################################
#
#
# let's have a look if /etc/rc.config sets START_LPD correctly
#
RC_CONFIG_SET START_LPD ${START_LPD}
#
##########################################################################
#
# now we can set the /etc/printcap file
#   NOTE that the spool directories are not created yet
#        this will be implemented later by using a
#        std suse linux interface
#
BACKUP /etc/printcap
(
    echo "# /etc/printcap"
    echo "# generated by alice $(date)"
    if [ -n "$PRINTER_QUEUES" ]
    then
       echo "$PRINTER_QUEUES"
    else
       echo "# list of print queues is blank!!"
    fi       
) > /etc/printcap
#
##########################################################################
#
# get the printer names
#
the_printer_names=$(echo "$PRINTER_QUEUES" | awk -F: '{
	    for (i=1; i<=NF; i++ ) {
		if ( $i ~ /^sd=/ ) {
		   a=$i
		   no_fields=split($i,mypaths,"/");
		   printer_dir=mypaths[no_fields];
		   no_fields=split(printer_dir,mypaths,"-");
		   printer_name=mypaths[1];
		   printer_names[printer_name]="x"
		}
	    }
	} 
	END {
	    for ( printer_name in printer_names ) {
	       print printer_name
	    }
	}'
    )
#echo "The printer names $the_printer_names"
for a_printer in $the_printer_names
do
   cp /var/lib/apsfilter/template/apsfilterrc.gs_device_name /etc/apsfilterrc.${a_printer}
   chmod go=r /etc/apsfilterrc.${a_printer}
done
cp /var/lib/apsfilter/template/apsfilterrc /etc/apsfilterrc
#
# get all directories to be generated
#
the_directories=$(echo "$PRINTER_QUEUES" | awk -F: '{
	for (i=1; i<=NF; i++ ) {
	    if ( $i ~ /^sd=/ ) {
	       a=$i
	       no_fields=split($i,mypaths,"=");
	       the_dir=mypaths[2];
	       print the_dir
	    }
	}
     }'
  ) 
#echo "The directories: $the_directories"
for a_dir in $the_directories
do
     mkdir -p $a_dir
     chown lp:lp $a_dir
     chmod a=rx $a_dir
     chmod u=rwx $a_dir
done
#
# make links for the aps filter programs
#
the_filters=$(echo "$PRINTER_QUEUES" | awk -F: '{
                                    for (i=1; i<=NF; i++ ) {
                                        if ( $i ~ /^if=/ ) {
                                           a=$i
                                           no_fields=split($i,mypaths,"=");
                                           the_filter=mypaths[2];
                                           print the_filter
                                        }
                                    }
                                 }'
                  )
for a_filter in $the_filters
do
    ln -s ../apsfilter $a_filter
done
#
############################################################################
#
# restart the lpd if it is already up and if START_LPD is yes
#


#
############################################################################
# Thats all
GOOD_BYE
