#!/bin/sh
# -------------------------------------------------------------------------
# merge 2 "sandbox" printcap files, and the spool directories into 
# /tmp/printcap and one working spool directory
# -------------------------------------------------------------------------

ln -sf /tmp/profile/printcap /tmp/printcap

file1=userprintcap
file2=machineprintcap

echo "#" > /etc/printcap 
echo "# printcap - Printer definition file." >> /etc/printcap 
echo "# Generated by ibmprintcapmerge." >> /etc/printcap 
echo "# Do not edit by hand unless you know what you" >> /etc/printcap 
echo "# are doing.  The file format is very important" >> /etc/printcap 
echo "# and needs to be strictly maintained.  See the" >> /etc/printcap 
echo "# manpage for printcap(5) for more information" >> /etc/printcap 
echo "# on the format of this file." >> /etc/printcap 
echo "#" >> /etc/printcap 
echo "" >> /etc/printcap 

if [ -f /tmp/profile/user/printcap ]; then
file1num=`grep -n "##" /tmp/profile/user/printcap | awk -F: '{print $1}'`
cat -n /tmp/profile/user/printcap > /tmp/$file1.numbered
for i in $file1num
do
   curline=`grep "    $i" /tmp/$file1.numbered | awk -F"    $i" '{print $2}'`
   echo $curline >> /etc/printcap  # echo out the "##" line

   let COUNTER=$i+1
   curline=`grep "    $COUNTER" /tmp/$file1.numbered | grep : | awk -F"   $COUNTER" '{print $2}'`
   if [ ! "$curline" ]; then
        let COUNTER=$COUNTER+1
        curline=`grep "    $COUNTER" /tmp/$file1.numbered | grep : | awk -F"   $COUNTER" '{print $2}'`
        printername=`grep "    $COUNTER" /tmp/$file1.numbered | awk -F$COUNTER '{print $2}' |awk -F: '{print $1}'`
        printername=`echo -n $printername`   # get rid of tab...
        printernamelist=`echo -n $printernamelist $printername`
        echo $curline >> /etc/printcap
   else
        echo $curline >> /etc/printcap
        printername=`grep "    $COUNTER" /tmp/$file1.numbered | awk -F$COUNTER '{print $2}' |awk -F: '{print $1}'`
        printername=`echo -n $printername`   # get rid of tab...
        printernamelist=`echo -n $printernamelist $printername`
   fi
   while [ "0$curline" != "0" ]
   do
         let COUNTER=$COUNTER+1
         curline=`grep "    $COUNTER" /tmp/$file1.numbered | grep -v "##" `
         if [ "$curline" ]; then
            echo -ne "\t" >> /etc/printcap
            echo -e $curline | awk -F"$COUNTER " '{print $2}' >> /etc/printcap
         fi
   done
done
rm /tmp/$file1.numbered
fi

if [ -f /tmp/profile/machine/printcap ]; then
file2num=`grep -n "##" /tmp/profile/machine/printcap | awk -F: '{print $1}'`
cat -n /tmp/profile/machine/printcap > /tmp/$file2.numbered
for i in $file2num
do
   firstline=`grep "    $i" /tmp/$file2.numbered | awk -F"    $i" '{print $2}'`

   let COUNTER=$i+1
   curline=`grep "    $COUNTER" /tmp/$file2.numbered | grep : | awk -F"   $COUNTER" '{print $2}'`
   if [ ! "$curline" ]; then
        let COUNTER=$COUNTER+1
        curline=`grep "    $COUNTER" /tmp/$file2.numbered | grep : | awk -F"   $COUNTER" '{print $2}'`
        printername=`grep "    $COUNTER" /tmp/$file2.numbered | awk -F$COUNTER '{print $2}' |awk -F: '{print $1}'`
        printername=`echo -n $printername`   # get rid of tab...
   else
        printername=`grep "    $COUNTER" /tmp/$file2.numbered | awk -F$COUNTER '{print $2}' |awk -F: '{print $1}'`
        printername=`echo -n $printername`   # get rid of tab...
   fi
   printerfound=0
   echo $printername
   echo $printernamelist
   for z in $printernamelist;
   do
      if [ "$z" = "$printername" ]; then
         printerfound=1
	echo printerfound
      fi
   done

   if [ "$printerfound" = "0" ]; then
   echo $firstline >> /etc/printcap
   echo $curline >> /etc/printcap 
   while [ "0$curline" != "0" ]
   do
         let COUNTER=$COUNTER+1
         curline=`grep "    $COUNTER" /tmp/$file2.numbered | grep -v "##" `
         if [ "$curline" ]; then
            echo -ne "\t" >> /etc/printcap
            echo -e $curline | awk -F"$COUNTER " '{print $2}' >> /etc/printcap
         fi
   done
   fi
done
rm /tmp/$file2.numbered
fi

mkdir -p /var/spool/lpd

if [ -d /tmp/profile/machine/spool/lpd/ ]; then 
   for a in `ls /tmp/profile/machine/spool/lpd/` 
   do 
	cp -Rf /tmp/profile/machine/spool/lpd/$a /var/spool/lpd
   done;
fi

if [ -d /tmp/profile/user/spool/lpd/ ]; then
   for b in `ls /tmp/profile/user/spool/lpd/` 
   do 
	cp -Rf /tmp/profile/user/spool/lpd/$b /var/spool/lpd
   done;
fi

chown -R lp:lp /var/spool/lpd
chown -R lp:lp /var/spool/lpd.current
chown lp:lp /dev/parallel
chown lp:lp /dev/lp0
chown lp:lp /dev/printer

# gdm does not like restart ... restart, i have no idea why. 
if [ -f /etc/rc.d/init.d/lpd ]; then
   /etc/rc.d/init.d/lpd stop 1>/dev/null
   /etc/rc.d/init.d/lpd start 1>/dev/null
fi

# set exit value to 0 for gdm presession script...
exit 0
