
#intented use of these routines:
# if ! preset dir; then
#  while true; do
#   echo "enter a directory"
#   getpar dir
#   if [ -d $dir ]; then break; fi
#   echo "invalid reply"
#  done
# fi
# allpars dir


#check if parameter has a pre_  value. If so, set it.
function preset ()
{
eval pre=\$pre_$1
if [ "$pre" = "" ]; then return 1; fi
export $1=$pre
return 0
}

#ask a value for a parameter.
# arguments: parameter name [default value] [yn]
# a dea-* variable overrides the supplied default value.
# presence the third argument "yn" forces the answer to be 'y' or 'n'.
#the empty string is never accepted as a value.
function getpar ()
{
#process possible checkpre_ parameter
eval checkpre=\$checkpre_$1
if [ "$checkpre" != "" ]; then
 export $1=$checkpre
 echo "-[preset as]-> $checkpre"
 unset checkpre_$1
 return
fi

#batch mode?
if [ $batchmode = y ]; then
 echo "---> "
 echo
 echo "ERROR: Insufficient information in arguments to complete batch run."
 echo "Can't do user input in batch mode."
 echo "No (valid) pre_$1 or checkpre_$1 setting found."
 doabort
fi

eval de=\$dea_$1
if [ "$de" = "" ]; then de=$2; fi

while true; do

 enter -f
 printf "---> "
 if [ "$3" = "yn" ]; then printf "(y/n) "; fi
 if [ "$de" != "" ]; then printf "[%s] " $de; fi
 
 read
 
 if [ "$REPLY" = "" ]; then REPLY=$de; fi
 
 if [ "$REPLY" = "" ]; then continue; fi
 if [ "$3" != "yn" ]; then break; fi
 if [ "$REPLY" = "y" ]; then break; fi
 if [ "$REPLY" = "n" ]; then break; fi
done
export $1=$REPLY
}

# add parameter with it's value to the allpars file.
function allpars()
{
eval pre=\$pre_$1
eval val=\$$1

if [ "$pre" != "" ]; then
 echo "export pre_$1=$val" >>/tmp/tbup/allpars
else
#dirty trick, we need this because getpar unsets checkpre_
 if [ "$checkpre" = "$val" ]; then
  echo "export checkpre_$1=$val" >>/tmp/tbup/allpars
 else
  echo "export dea_$1=$val" >>/tmp/tbup/allpars
 fi
fi
checkpre=""
}

# add parameter as default parameter to the rpars file.
function dea_rpars()
{
eval val=\$$1
echo "export dea_$1=$val" >>/tmp/tbup/rpars
}

# add parameter as preset parameter to the rpars file.
function pre_rpars()
{
eval val=\$$1
echo "export pre_$1=$val" >>/tmp/tbup/rpars
}


# get method name.
#arguments: method letter, method variable, method name.
function getmet ()
{
cd /tmp/tbup

if ! preset $2; then

#get available methods
 echo
 echo "Available $3 methods:"
 cat $bupbin/methods.$1

 while true; do
  printf "Enter method name or number "
  getpar $2

#print chosen method name in bla.
  eval REPLY=\$$2
  gawk '( NR == r ) || ( $2 == r ) { print $2 }' r=$REPLY \
    $bupbin/methods.$1 >bla

  re="`cat bla`"

  if [ "$re" != "" ]; then 
#necessary if chosen by number.
   export $2=$re
   break
  fi

  echo "No such method."
 done

 rm -f mets bla
fi
allpars $2
}


# question <arg>
# ask question <arg>, only accept answers `y' and `n', return 0 if yes,
# 1 if no.
# intended use: if question "move"; then move; fi;
function question ()
{
while true; do
 enter -f
 printf "$1? (y/n) ---> "
 read
 if [ "$REPLY" = "y" ]; then return 0; fi
 if [ "$REPLY" = "n" ]; then return 1; fi
done
}


#get filename
#does pre and dea processing.
#arguments: filename variable, prompt, flag, [nodea], [template]
# flag: exists or new.  files in /dev must always exist.
# does not call `allpars' if the option nodea is given.
# if template given, e.g. /etc/tbackup/%s.arg, reply is filled in in
# template.
function getfilename()
{

if ! preset $1; then
 while true; do
  printf "$2"
  getpar $1
  fn=`eval echo $\`echo $1\``

  if [ "$5" != "" ]; then
   fn=`printf $5 $fn`
  fi

  if [ "`echo $fn | grep "^/"`" = "" ]; then 
    fn=$userwd/$fn
    echo "   [Full pathname is $fn]"
  fi

  if [ ! -d `dirname $fn` ]; then 
   echo "The path to $fn does not exist, try again."
   continue
  fi

  if [ "`echo $fn | grep "^/dev/"`" != "" ]; then 

# is a device
   if [ ! -e $fn ]; then
    echo "The device $fn does not exist."
   else
    break
   fi

  else

# not a device
   if [ -e $fn ]; then
    if [ -d $fn ]; then
     echo "$fn is a directory name."
    else
     if [ $3 = exists ]; then break; fi

     echo "The file $fn already exists!!"
     printf "Overwrite it? "
     getpar owrite n yn
     if [ $owrite = y ]; then break; fi
    fi
   else
# file does not yet exist.
    if [ $3 = new ]; then break; fi
    echo "The file $fn does not exist."
   fi

  fi  

 done
 export $1=$fn
fi

if [ "$4" != "nodea" ]; then
 allpars $1
fi
}

