#! /bin/sh
#
#   Copyright 2008 Free Software Foundation, Inc.
#
#  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 3 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, see <http://www.gnu.org/licenses/>.
#
# Turn sqlist output into equivalent of lpq short format output
#

prefix="@prefix@";
exec_prefix="@exec_prefix@";
bindir="@bindir@";
sbindir="@sbindir@";
USERNAME="@SPOOLUSER@";

# Create lp and lpstat

ln -s -f $sbindir/gspl-lpcover $bindir/lp
ln -s -f $sbindir/gspl-lpstatc $bindir/lpstat

# Create cancel

cat <<EOF >$bindir/cancel
#! /bin/sh

exec $bindir/gspl-qdel -y \$*
EOF

# Create lpr

cat <<'EOF' >$bindir/lpr
#! /bin/sh

prefix="@prefix@";
exec_prefix="@exec_prefix@";
bindir="@bindir@";
sbindir="@sbindir@";

remq=-q
copies=1
dest=

set -- `getopt EP:#:CJTlopr "$@"`
while :
do
	arg=$1
	shift
	case "$arg" in
	--)
		break;;
	-[CJTLop])
		:;;
	-#)
		copies=$1
		shift;;
	-P)
		dest=$1
		shift;;
	-r)
		remq=
		;;
	*)
		break;;
	esac
done

if test -n "$dest"
then	dest="-P $dest"
fi

exec $bindir/gspl-pr $dest -c $copies $remq "$@"
EOF

# Create lpq

cat <<'EOF' >$bindir/lpq
#! /bin/sh

prinstat() {
$bindir/gspl-plist -F '%p %t' $dest|awk '{
state=$2
if (state == "idle") state="ready"
else if (state == "printing") state="ready and printing"
else if (state == "error") state="error halted"
printf("%s is %s\n", $1, state)
}'
}

shortlist() {
prinstat
$bindir/gspl-qlist -z $dest -l -F '%u %N %K %h' | awk '{ 
if (count == 0)
	print "Rank   Owner      Job  Files                                 Total Size"
count++
if (int(count/10)%10 == 1) th = "th"
else if (count%10 == 1) th = "st"
else if (count%10 == 2) th = "nd"
else if (count%10 == 3) th = "rd"
else th = "th"
num = sprintf("%d%s", count, th)
printf("%-7s%-11s%-5s%-38s%s bytes\n", num, $1, $2, $4, $3);
}
END { if (count == 0) {
print "no entries"
exit 10 }
}'
if test $? -ne 0
then
	exit 0
fi
}

longlist() {
prinstat
$bindir/gspl-qlist -z -q $qlim -l -F '%u %N %K %c %o %h' | awk '{ 
count++
if (int(count/10)%10 == 1) th = "th"
else if (count%10 == 1) th = "st"
else if (count%10 == 2) th = "nd"
else if (count%10 == 3) th = "rd"
else th = "th"
num = sprintf("%d%s", count, th)
printf("%s: %-35s[job %s%s]\n", $1, num, $2, $5)
if ($4 != 1)
	num = sprintf("%-3d copies of %s", $4, $6)
else
	num = $6
printf("        %-33s%s bytes\n\n", num, $3)
}
END { if (count == 0) {
print "no entries"
exit 10 }
}'
if test $? -ne 0
then
	exit 0
fi
}

prefix="@prefix@";
exec_prefix="@exec_prefix@";
bindir="@bindir@";
sbindir="@sbindir@";

dest=`$bindir/gspl-plist -F %p|sed -e 1q`
long=

set -- `getopt EP:al "$@"`
while :
do
	arg=$1
	shift
	case "$arg" in
	--)
		break;;
	-E)
		:;;
	-P)
		dest="$1"
		shift;;
	-a)
		dest=
		;;
	-l)
		long=y;;
	*)
		break;;
	esac
done

slint=`expr "$1" : '+\([0-9]*\)'`

if test -n "$dest"
then	dest="-q $dest"
fi

if test -z "$slint"
then
	if test -z "$long"
	then
		shortlist
	else
		longlist
	fi
elif test -z "$long"
then
	while :
	do
		shortlist
		sleep $slint
	done
else
	while :
	do
		longlist
		sleep $slint
	done
fi
EOF

# Create lprm

cat <<'EOF' >$bindir/lprm
#! /bin/sh

prefix="@prefix@";
exec_prefix="@exec_prefix@";
bindir="@bindir@";
sbindir="@sbindir@";

me=`who am i|sed -e 's/^\([^ ]*\).*/\1/'`
if test -z "$me"
then	me=`id|sed -e 's/[^(]*(\([^)]*\).*/\1/'`
fi

while :
do
	arg="$1"
	case "$arg" in
	-)
		allj=y;;
	-E)
		:;;
	-P)
		shift
		dest="$1";;
	*)
		break;;
	esac
	shift
done

if test -z "$dest"
then	dest=`$bindir/gspl-plist -F %p|sed -e 1q`
fi

dest="-q $dest"
if test -n "$allj"
then	jobs=`$bindir/gspl-qlist -u $me -F %N`
else	jobs=`$bindir/gspl-qlist -u $me $dest -F %N|sed -e 1q`
fi

if test $# -gt 0
then	jobs="$*"
fi

exec $bindir/gspl-qdel -y $jobs
EOF

chmod 755 $bindir/cancel $bindir/lpr $bindir/lpq $bindir/lprm
