#!/bin/sh

# "piload"	K. J. Turner <kjt@cs.stir.ac.uk>	26/06/99

# This program loads the Pilot from files in the directory given by
# environment variable "PILOTDIR". The options are:
# 
#   -a		load all files (i.e. restore)
#   -d		load database (PDB) files only (default option)
#   -h		print usage help
#   -p		load program (PRC) files only
#   file ...	load named files only (use quotes for names with spaces,
#		omit suffix such as ".prc" or ".pdb")
#
# Note that only files already existing in "PILOTDIR" are handled! Some
# system files cannot be loaded back into the Pilot. Filenames can be
# shortened as long as they do not become ambiguous (e.g "AddressDB.pdb" can
# be shortened as "Address").
# 
# It is assumed that environment variables "PILOTPORT" and "PILOTRATE" have
# already been set up or that the defaults are satisfactory.

usage () {				# report use of program
  echo "usage: $prog [ -a(ll) | -d(ata) | -h(elp) | -p(rogram) | file ... ]"
  exit 1
}

prog="`basename $0`"			# pilot command
omit='"Graffiti ShortCuts.prc" "Update 1.0.2.prc"'

echo "$prog: Unix -> Pilot"

if [ -d "$PILOTDIR" -a -r "$PILOTDIR" ]	# Pilot directory OK?
  then					# yes - change to it
    cd "$PILOTDIR"
  else					# no - report error
    echo "$prog: Pilot directory \"$PILOTDIR\" not readable"
    exit 1
fi

while [ $# -gt 0 ]			# analyse parameters
  do
    case $1 in
      -a) opt="all"; shift;;		# all files
      -d) opt="data"; shift;;		# data files
      -h) usage;;			# help
      -p) opt="prog"; shift;;		# program files
      -*) echo "$prog: unknown option \"$1\""; usage;;
      *)
        if [ "$opt" = "" ]		# option unset?
	  then opt="name"		# named files
	  else
	    echo "$prog: files not allowed for chosen option"
	    usage
	fi
	break;;
    esac
  done

if [ "$opt" = "" ]			# option unset?
  then opt="data"			# data files by default
fi

case $opt in				# analyse options
  all)					# restore from directory
    pars="-r $PILOTDIR";;
  data)					# data files?
    for file in *.pdb			# process all database files
      do
        if [ "$file" != "*.pdb" ]	# a data file?
	  then
	    par="$file"			# leave suffix
	    pars="$pars -i '$par'"	# install file
	fi
      done;;
  prog)					# program files?
    for file in *.prc			# process all program files
      do
        if [ "$file" != "*.prc" ]	# a program file?
	  then
	    if [ `expr "$omit" : ".*\"$file\""` = "0" ]
	      then			# not file to be omitted
	        par="$file"		# leave suffix
	        pars="$pars -i '$par'"	# install file
	    fi
	fi
      done;;
  name)					# command-line files?
    while [ $# -gt 0 ]			# process all command-line files
      do
        if [ ! -f "$1" ]		# file name as given doesn't exist?
	  then
	    par=""			# initialise parameter
	    for file in "$1"*		# look for all suffixes
	      do
	        if [ "$file" != "$1*" ]	# file name expanded?
		  then
		    if [ -f "$file" -a -r "$file" ] # is a readable file?
		      then		# file is OK, set as parameter
			if [ "$par" = "" ] # parameter unset?
			  then
			    par="$file"	# set file name
			  else		# parameter set - ambiguous name
			    echo "$prog: \"$1\" is an ambiguous prefix"
			    exit 1
			fi
		      else		# file is not OK
		        echo "$prog: cannot read \"$1\""
		        exit 1
		    fi
		  else			# no files beginning with name
		    echo "$prog: cannot read \"$1\""
		    exit 1

		fi
	      done
	  else				# file name exists as given
	    par="$1"			# set file name
	fi
	pars="$pars -i '$par'"		# install file
	shift
      done;;
esac

eval pilot-xfer $pars		# do transfer (eval for quotes)
