#!/bin/bash
#
# print_labels
# (c) Buchan Milne <bgmilne@linux-mandrake.com> 2003 
# License: GPLv2
# 
# print_labels is a simple script to allow users print CD labels for Mandrake
# Linux installed on their system (/usr/share/mandrake-cards).
#
# Changelog
# v0.0.1 - 20030330 Initial script
# v0.0.2 - 20030402 Add detection of dialog and printing programs

RELEASE=/etc/mandrake-release
LABELS=/usr/share/mandrake-media
ARCH=`awk '{print $7}' $RELEASE`

#Find a printing program
PRINT=""
# Use desktop print programs if available
[ -x `which kprinter` -a "$DESKTOP" == "kde" ] && PRINT="kprinter"
[ -x `which gtklp` -a -n "$GNOME_DESKTOP_SESSION_ID" ] && PRINT="gtklp"
# Fall back to kprinter, gtklp or xpp if they exist
[ -z "$PRINT" -a -x "`which kprinter 2>/dev/null`" ] && PRINT="kprinter"
[ -z "$PRINT" -a -x "`which gtklp 2>/dev/null`" ] && PRINT="gtklp"
[ -z "$PRINT" -a -x "`which xpp 2>/dev/null`" ] && PRINT="xpp"
# If we don't have a display variable, hope the user has the right printer by default
[ -z "$DISPLAY" -a -x "`which lpr 2>/dev/null`" ] && PRINT="lpr"

# Find a dialog program we can use, either kdialog or gdialog 
# if we are running in console, gdialog or dialog
if [ -n "$DISPLAY" ]
then

	# Use desktop dialog programs if available
	[ -x `which kdialog` -a "$DESKTOP" == "kde" ] && DIALOG="kdialog"
	[ -x `which gdialog` -a -n "$GNOME_DESKTOP_SESSION_ID" ] && DIALOG="gdialog"
	# Fall back to gdialog, kdialog or gmessage if they exist
	[ -z "$DIALOG" -a -x "`which gdialog 2>/dev/null`" ] && DIALOG="gdialog"
	[ -z "$DIALOG" -a -x "`which kdialog 2>/dev/null`" ] && DIALOG="kdialog"
	#[ -z "$DIALOG" -a -x "`which gmessage 2>/dev/null`" ] && DIALOG="gmessage"
else
	# If we don't have a display variable, use gdialog or dialog
	[ -z "$DIALOG" -a -x "`which dialog 2>/dev/null`" ] && DIALOG="dialog"
	[ -z "$DIALOG" -a -x "`which gdialog 2>/dev/null`" ] && DIALOG="gdialog"
fi

# Exit if we don't have a dialog program
if [ -z "$DIALOG" ]
then 
	echo "No dialog program found, exiting"
	exit 1
fi

c=( `ls $LABELS/$ARCH*.ps 2>/dev/null|cut -f5 -d'/'|cut -f2- -d'-'|cut -f1 -d'.'`)

BUTTONS=`for i in $(seq 0 "$[${#c[*]}-2]");do echo -n "${c[i]}:$[$i+1],";done``echo -n "${c[$[${#c[*]}-1]]}:$[${#c[*]}]"`
LIST=`for i in $(seq 0 "$[${#c[*]}-2]");do echo -n "${c[i]} ";done``echo -n "${c[$[${#c[*]}-1]]}"`
CHECKLIST=`for i in $(seq 0 "$[${#c[*]}-1]");do echo -n "$[$i+1]" "${c[i]}" "off" " ";done` 
MESSAGE="Please choose which labels to print."

if [ "$DIALOG" == "kdialog" ]
then
	CHOICES=`kdialog --separate-output --checklist "$MESSAGE" $CHECKLIST 2>&1`
#elif [ "$DIALOG" == "gmessage" ]
#then
#	gmessage -center -buttons $BUTTONS $MESSAGE
#CHOICES=${c[$[$?-1]]}
else
	CHOICES=`$DIALOG --separate-output --checklist "$MESSAGE" 15 24 8 $CHECKLIST 2>&1`
fi
#dialog --checklist "$MESSAGE" 12 24 5 $CHECKLIST
#gdialog --checklist "$MESSAGE" 12 24 5 $CHECKLIST

if [ $? -eq 0 ]
then
	FILES=""
	for i in $CHOICES;do FILES=`echo "$FILES $LABELS/$ARCH-${c[$[$i-1]]}.ps"`;done
	[ -n "$FILES" ] && $PRINT $FILES
fi
