#!/bin/sh -
PATH=/l/palm/bin:$PATH; export PATH

xfer="${PILOTXFER-pilot-xfer}"
malsync="${MALSYNC-malsync}"
bakdir="${PALMBAK-$HOME/l/palm/bak}"
user="${PALMUSER-${USER-$LOGNAME}}"
instdir="${PALMINST-$HOME/l/palm/install}"

#dflttarg=biat
# malsync 2.1.1 observed to disrupt AvantGo config and save to Palm mem vs MS
malsync=malsync-DISABLED
#dflttarg=bitp
dflttarg=b

# PILOTPORT can be specified in $bakdir/$user-port
# file basenames to exclude from backup can be listed in $bakdir/$user-exclude

# palm - backup sync, install pending files and sync time on a Palm PDA
# Steve Kinzler, steve@kinzler.com, Jan 99/Apr 00/Jun 03/Jun 05/Jul 08
# https://kinzler.com/me/home.html#palm

# NOTE: compatible with pilot-link 0.11.{5,7}
#   not compatible with pilot-link 0.09.0

target=; bad=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-[biatpP]*)	target="$target$1";;

		--)		shift; break;;
		-h)		bad=t; break;;
		-*)		bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)		break;;
		esac
		shift;;
	esac
done

case "$#,$bad" in
0,)	;;
1,)	user="$1";;
*)	cat << EOF 1>&2
usage: $0 [ -biatpP ] [ user ]
	-b	backup to $bakdir/<user>, default "$user"
	-i	install pending files from $instdir
	-a	synchronize AvantGo with '$malsync'
	-t	synchronize time
	-p	prompt between hotsyncs
	-P	do not prompt between hotsyncs
	default -$dflttarg
EOF
	exit 1;;
esac

case "$target" in
'')	target="$dflttarg";;
esac

echo "#### $0 -$target $user ####" `date`

if test -r "$bakdir/$user-port"
then
	PILOTPORT=`cat "$bakdir/$user-port"`; export PILOTPORT
fi

hotsync=
case "$target" in
*b*)	excl=
	if test -r "$bakdir/$user-exclude"
	then
		excl=" --exclude $bakdir/$user-exclude"
	fi
	case "$user" in
	?*)	echo "RUNNING: $xfer$excl --sync $bakdir/$user"
		"$xfer"$excl --sync "$bakdir/$user" || exit 2
		echo ''; hotsync=t;;
	*)	echo "$0: no user specified" 1>&2; exit 3;;
	esac;;
esac

time=; _time=
case "$target" in
*t*)	case `"$xfer" --help | grep -Ei '.-t.*(imesync|sync.*time)'` in
	?*)	time=-t; _time=" $time";;
	*)	case `("$xfer"+time --help) 2> /dev/null |
		      grep -Ei '.-t.*(imesync|sync.*time)'` in
		?*)	xfer="$xfer"+time; time=-t; _time=" $time";;
		*)	case `(pilot-justintime --help) 2> /dev/null` in
			?*)	target=`echo "$target" | sed 's/t/j/g'`;;
			*)	echo 'Time synchronization not supported.'
				target=`echo "$target" | sed 's/t//g'`;;
			esac;;
		esac;;
	esac;;
esac

case "$target" in
*i*t*|*t*i*)	case `echo "$instdir"/*` in
		''|*/\*)	echo 'No files to install.'
				target=`echo "$target" | sed 's/[ij]//g'`;;
		*)		target=`echo "$target" | sed 's/[tj]//g'`;;
		esac;;
esac

pmsg='Press return to continue'
case "$target" in
*P*)	target=`echo "$target" | sed 's/p//g'`;;
esac

case "$target" in
*i*)	case `echo "$instdir"/*` in
	''|*/\*)	echo 'No files to install.';;
	*)		case "$hotsync,$target" in
			?,*p*)  echo "$pmsg" | tr -d '\012'; read x;;
			esac
			echo "RUNNING: $xfer$_time --install" $instdir/*
			"$xfer" $time --install "$instdir"/*
			case "$?" in
			0)	rm "$instdir"/*;;
			*)	echo 'Not removing install files.';;
			esac
			echo ''; hotsync=t;;
	esac;;
esac

case "$target" in
*a*)	case "$hotsync,$target" in
	?,*p*)  echo "$pmsg" | tr -d '\012'; read x;;
	esac
	echo "RUNNING: $malsync"
	$malsync
	echo ''; hotsync=t;;
esac

case "$target" in
*t*)	case "$hotsync,$target" in
	?,*p*)  echo "$pmsg" | tr -d '\012'; read x;;
	esac
	echo "RUNNING: $xfer$_time"
	"$xfer" $time
	echo '';;
*j*)	case "$hotsync,$target" in
	?,*p*)  echo "$pmsg" | tr -d '\012'; read x;;
	esac
	echo "RUNNING: pilot-justintime"
	pilot-justintime
	echo '';;
esac
