#!/bin/sh
#set -xv
#
#       This shell script copies the Landmark Auxiliary file
#       from the input media and puts them into the DB directory. 
#
#
#==============================================================================
check_run()
{
Clearit;
xxx=
dbloc=$G1GTXDB
G1CDDIR=$G1GTX/data
Clearit;
$G1ECHO "\n\tThe Landmark Auxiliary File load environment is currently set to:\n"
$G1ECHO "\tLandmark input file location:" $G1CDDIR
$G1ECHO "\tOutput database location:" $dbloc"\n"
$G1ECHO "\tEnter c to (c)ontinue"
$G1ECHO "\t   or m to (m)odify database input/output locations"
$G1ECHO "\t   or q to (q)uit\n"
$G1ECHO "\t   ===> \c"
read xxx
case $xxx in
        q|Q)
                $G1ECHO "\n\tUser requested termination.\n"
                exit
        ;;
esac
case $xxx in
        m|M)
                $G1ECHO "\n\tWhich do you want to change?\n"
                $G1ECHO "\tEnter 1 to change the Landmark input file location"
                $G1ECHO "\t   or 2 to change the Landmark output file location"
                $G1ECHO "\t   or 3 to change both\n"
                $G1ECHO "\t  ===> \c"
                read modio
                case $modio in
                        1)      get_pathin
                        ;;
                        2)      get_pathout
                        ;;
                        3)      get_pathin
                                get_pathout
                        ;;
                        *)      check_run
                        ;;
                esac
        ;;
        c|C) :
        ;;
          *) check_run
        ;;
esac

if [ ! -d "$dbloc" ]    # check to see if the install area is there
then
   $G1ECHO "\n\tDatabase final location "$dbloc" does not exist."
   $G1ECHO "\tDo you want to create it now?\n"
   $G1ECHO "\tEnter (y)es to create the new database area."
   $G1ECHO "\t      (n)o to exit.\n"
   $G1ECHO "\t   ===> \c"
   read yyyy
   case $yyyy in
        [yY]*)  curr_dir=$PWD         #  Preserve the current location
                mk_dir                #  Call the make directory mk_dir function
                cd $curr_dir          #  Change to the previous location
        ;;
            *)  exit 1
        ;;
   esac
fi

update_setup

}
#===========================================================================
# Get the full path for the db input area
get_pathin()
{
Clearit;
$G1ECHO "\n\tPlease enter full path where the Landmark files are located"
$G1ECHO "\t ==> \c"
read G1CDDIR
if [ ! -d "$G1CDDIR" ]
then
  $G1ECHO "\n\tPath not found."
  $G1ECHO "\n\tYou must enter a valid full path."
  $G1ECHO "\tPress <Enter> to continue..."
  read pressenter
  get_pathin
else
  $G1ECHO "\n\tThe location of the Landmark files is: " $G1CDDIR
  $G1ECHO "\tIs this correct?\n"
  $G1ECHO "\tEnter (y)es to continue."
  $G1ECHO "\t      (n)o to try again.\n"
  $G1ECHO "\t   ===> \c"
  read yyyy
  case $yyyy in
    [yY]*)  yyyy=
    ;;
        *)  get_pathin
    ;;
  esac

  export G1CDDIR
fi
}

#===========================================================================
# Get the full path for the new db load area
get_pathout()
{
Clearit;
$G1ECHO "\n\tPlease enter full path where you would like to copy"
$G1ECHO "\n\tthe Landmark file ==> \c"
read dbloc
if [ "$dbloc" = "" -o "$dbloc" = " " ]
then
        $G1ECHO "\n\tThe directory (${dbloc}) was not found."
        $G1ECHO "\n\tYou must enter a valid full path."
        $G1ECHO "\tPress <Enter> to continue....."
        $G1ECHO "\t"$G1CDDIR", which are needed for input."
        read pressenter
        get_pathout
fi

$G1ECHO "\n\tThe new Landmark file location will be: " $dbloc
$G1ECHO "\tIs this correct?\n"
$G1ECHO "\tEnter (y)es to continue."
$G1ECHO "\t      (n)o to try again.\n"
$G1ECHO "\t   ===> \c"
read yyyy
case $yyyy in
        [yY]*)  yyyy=
        ;;
            *)  get_pathout
        ;;
esac
if [ ! -d "$dbloc" ]    # check to see if the install area is there
then
        $G1ECHO "\n\tThe Landmark file output location "$dbloc" does not exist."
        $G1ECHO "\tDo you want to create it now?\n"
        $G1ECHO "\tEnter (y)es to create the new Landmark file area."
        $G1ECHO "\t      (n)o to exit.\n"
        $G1ECHO "\t   ===> \c"
        read yyyy
        case $yyyy in
                [yY]*)  curr_dir=$PWD         #  Preserve the current location
                        mk_dir                #  Call the make directory mk_dir function
                        cd $curr_dir          #  Change to the previous location
                ;;
                    *)  $G1ECHO "\n\tYou must create the new Landmark file install area."
                        exit 1
                ;;
        esac
fi
export dbloc
}

#===========================================================================
get_name()
{
  filefound="0" ; export filefound

  while [ $filefound = "0" ]
  do 
    get_input_filename

    lmnamecd="$lmname"
    lmnamecd2=`$G1ECHO $lmname | tr "[:lower:]" "[:upper:]"` 
    if [ -f $G1CDDIR/$lmnamecd ]
    then
      filefound="1"
      export lmnamecd
    elif [ -f $G1CDDIR/$lmnamecd2 ] 
    then
      filefound="1"
      lmnamecd=$lmnamecd2
      export lmnamecd
    else
      $G1ECHO "\n\tLandmark file ($lmname) not found in $G1CDDIR.\n"
    fi
    if [ "$filefound" = "1" ]
    then
      $G1ECHO "\n\tLoading Landmark file..."
      $G1ECHO "\tSource: $G1CDDIR/$lmnamecd"
      $G1ECHO "\tTarget: $dbloc/$lmname\n"
    else
      $G1ECHO "\tSource file, $G1CDDIR/$lmnamecd not found."
      $G1ECHO "\tPress <Enter> to continue..."
      read xxx
    fi
  done
}
#===========================================================================
#
# Function to clear the screen. Try the clear shell command
# first, then try the tput command with a clear parameter and then
# try to clear each position (line) of the screen the hard way.
#
Clearit()
{
{ clear; } 2>/dev/null  ||
        { tput clear; } 2>/dev/null  ||
                { for i in \
                p01 p02 p03 p04 p05 p06 p07 p08 p09 p10   \
                p11 p12 p13 p14 p15 p16 p17 p18 p19 p20   \
                p21 p22 p23 p24 p25 p26 p27 p28 p29 p30   \
                p31 p32 p33 p34 p35 p36 p37 p38 p39 p40
                do
                        echo ""
                done
                }
}

#===========================================================================
get_input_filename()
{
  Clearit;
  $G1ECHO "\n\tPlease enter the file name of your Landmark Auiliary file,"
  $G1ECHO "\tthen press <Enter> to continue..."
  $G1ECHO "\n\t=====> \c"
  read lmname
  export lmname
}
#===========================================================================
#  create new db location based upon user's new entered path
mk_dir()
{
  cd /
  for n in `$G1ECHO $dbloc | tr '/' ' '`
  do
    if [ ! -d "$n" ]
    then
         mkdir $n
    fi
  cd $n
  done
}

#===========================================================================
Clearit;
check_run;
Clearit;
get_name;
if [ "$filefound" = "1" ] 
then
  cp $G1CDDIR/$lmnamecd $dbloc/$lmname 2> /dev/null
  status=$?
  if [ $status -eq 0 ]
  then
    $G1ECHO "*******************************************************************************" 
    $G1ECHO "Landmark File ($lmname) successfully loaded on `date`."
    $G1ECHO "*******************************************************************************\n"
  else
    $G1ECHO "*********************************************" 
    $G1ECHO "Landmark File ($lmname) not loaded." 
    $G1ECHO "*********************************************\n"
  fi
fi
