#! /bin/sh
#get machine names, tape device, level
#
#grep fstab's for machine name > put in temp file for dkread
#
#call dkbackup with information
#
#
#
#
DK=/usr/misc/dk
PATH="$DK/bin:.:$PATH"
export PATH

HOST=`hostname`
LEVEL=""
MACHINES=""
USAGE="Usage: dkselect -t (tape device) [-l level] machine1 machine2..."
cat /dev/null > ${DK}/etc/fstab.select
cp ${DK}/bin/mskel ${DK}/etc/mailselect

#
# Get the command line arguments.
#
while [ $# -gt 0 ] ; do
  found=0
  case "$1" in
	-l)	LEVEL="$2"
		shift
		shift;;
	-t)	TAPE="$2"
		shift
		shift;;
	-*)	shift;;
	$HOST)	for i in `ls -1 ${DK}/etc/fstab.*`
		do
		  grep ':rw:' $i >> ${DK}/etc/fstab.select
		  case $? in
			0) found=1
			   break
		  esac
		done
		case $found in
		  0)  echo "Machine name $1 not found in fstab files"
		      echo -n "Is it okay to use /etc/fstab? [y/n]"
		      read X
    		      case "$X" in
      			[yY]*) echo "/etc/fstab:@$1:rw:1:1" >> ${DK}/etc/fstab.select
			       ;;
      			[nN]*) exit 1 ;;
		      esac
		esac
		for i in `ls -1 ${DK}/etc/mail*`
		do
		  grep -i $1 $i >> ${DK}/etc/mailselect
		  case $? in
			0) break
		  esac
		done
		MACHINES="${MACHINES} $1"
		shift;;
	*)	for i in `ls -1 ${DK}/etc/fstab.*`
		do
		  grep -i "@$1:" $i >> ${DK}/etc/fstab.select
		  case $? in
			0) found=1
			   break
		  esac
		done
		case $found in
		  0)  echo "Machine name $1 not found in fstab files"
		      while :
		      do
		        echo -n "Use /etc/fstab on $1 ? (y/n): "
		        read X
		        case "$X" in
		        [yY]*) echo "/etc/fstab:@$1:dw:1:1" >> ${DK}/etc/fstab.select
			       break ;;
			[nN]*) break ;;
			esac
		      done
		esac
		for i in `ls -1 ${DK}/etc/mail*`
		do
		  grep -i \"$1\" $i >> ${DK}/etc/mailselect
		  case $? in
			0) break
		  esac
		done
		MACHINES="${MACHINES} $1"
		shift;;
  esac
done

if [ -z "$TAPE" ] ;
then
	echo $USAGE
	rm -f ${DK}/etc/fstab.select ${DK}/etc/mailselect
        exit 0 ;
fi

if [ -z "$MACHINES" ] ;
then
	echo $USAGE
	rm -f ${DK}/etc/fstab.select ${DK}/etc/mailselect
	exit 0 ;
fi


case $LEVEL in
	"") $DK/bin/dkbackup -t $TAPE -f select ;;
	 *) $DK/bin/dkbackup -t $TAPE -f select -l ${LEVEL};;
esac

rm ${DK}/etc/fstab.select ${DK}/etc/mailselect

exit 0
