#!/bin/ksh
#------------------------------------------------------------------------------
# File Name: saveDT
# Descriptive File Name: Save DirectTalk/6000 data; V1.0
#------------------------------------------------------------------------------
#
# Licensed Materials - Property of IBM
# 5765-B81
# (C) Copyright IBM Corp. 1990, 1997.
# All rights reserved.
# US Government Users Restricted Rights
# Use, duplication or disclosure restricted by GSA ADP Schedule
# Contract with IBM corp.
#
#------------------------------------------------------------------------------
# SCCS File Name: sw/upgutl/saveDT, Migration, Lynx
# File Checked Out From: /home/dt6/vc/0/0/4/3/s.67
# Current Revision: 1.12.1.1 Created 8/13/97 15:37:32
#------------------------------------------------------------------------------
# File Description:
#
# Saves DirectTalk data from the following areas:
#   $SYS_DIR/adm_class          file
#       or if R3, $SYS_DIR/admclass_table and $SYS_DIR/.admclass_table
#   $SYSPARM_DIR/rd.data        file
#   $CUR_DIR/ca                 directory tree; except any filenames that are
#                                               also shipped in the DB installp
#                                               (as denoted by lslpp output)
#   $CUR_DIR/distribution_dir   directory
#   $CUR_DIR/helpdir            directory   (not saved for Rel 3 migration)
#   $CUR_DIR/prompt_dir         directory       (+)
#   $CUR_DIR/scr_db             directory       (+)
#   $CUR_DIR/stbl_dir           directory       (+)
#   $CUR_DIR/upmb_dir           directory
#   $CUR_DIR/vagtxt_dir         dir; all but menu_resrc.001     (+)
#   $CUR_DIR/voicedir           directory       (+)
#   $CUR_DIR/xref_db            directory       (+)
#   $ARC_DIR/oamlog             directory  (prompt)
#   $VAE/sw/environ/.vaeprofile file            rename by appending release
#                                       level before saving; New for R4.1
#
#  + Denotes must be included completely and together to maintain integrity
#
# Options:  -f <complete device specification>  #if not enterd, prompted
#
# Uses tar to save files;
#
# If environment already set (i.e. $VAE is set), runs
# $VAE/sw/environ/.vaeprofile; else if /usr/lpp/dirTalk/tools/vae.setenv
# is executable, runs this; else aborts script because environment cannot be
# set
#
#------------------------------------------------------------------------------
# File Type: script
#          {source,include,makefile,script,ascii data, etc}
# Environment: AIX
# Re-entrant Text: Yes
#------------------------------------------------------------------------------
# Functions Defined:
#  func1()     function to perform <1> and <2> for <3>
#  func2()     function to perform <1> and <2> for <3>
#------------------------------------------------------------------------------
# Change History:
#
# < where modifications to file contents are not described in
#   function prologue change histories>
#
# Date        Description
# ----------  -----------------------------------------------------------------
# 02/06/92    L. Bostick        Created
# 02/24/92    L. Bostick        fixed list of helpdir files
# 02/28/92    L. Bostick        fix to NOT save menu_resrc.001* files
# 07/28/92    L. Bostick        add save of current version for Release 3
#                               creates ./.version file in $CUR_DIR containing
#                               lslpp version output.  writes this file as 1st
#                               file to device; save complete helpdir directory
# 10/27/92    L. Bostick        remove helpdir files from list; not migrated for
#                               Release 3
# 10/28/92    L. Bostick        Don't save DT's $CUR_DIR/cd/include files
# 03Aug93     L. Bostick        Updated for R4; save admclass_table and
#                               .admclass_table files, not adm_class
# 16Aug93     LJB               added check if DT/6000 is running
# 19Oct93     LJB               additional system CA header file CA_access_db.h
# 09Dec93     LJB               update for R4.1; save .vaeprofile
# 30Aug94     LJB               update for R5; create save version file to
#                               indicate saveDT tool version; log output;
# 21Jul95     Sej               update for R6 on v4.1 - use new package name
#                               output from lslpp -hcq has changed format
# 12Sep95     PRC: CMVC defect 13285: Make bi-lingual for AIX 3 and 4
# 13Sep95     PRC: CMVC defect 13315: Use non-specific language "lsdev" command
# 15Nov95     PRC: CMVC defect 14827: Change mechanism for ignoring files in
#                  ca/include from a hard-coded list for header files that are
#                  distributed to an "lslpp" check. Updated SAVEV to 160.
# 24Feb97     SN : CMVC feature 17727e: Migrate saveDT and saveDT.tape
#                  together. Also give extra option to copy database to another
#                  filesystem. Calculate space required for the database and
#                  list the filesytems. Remove the option of specifying the
#                  device name on the command line.
# 27Feb97     SN : CMVC defect 20304: Remove command line options and
#                  redundant variables.
# 11Apr97     SN : CMVC defect 20780. Remove 'rm-rf' statement in option 5 as 
#                  this causes the saveDT.log file to vanish.
# 16Apr97     SN : CMVC defect 20975. Remove '-d' flag from df command as this
#                  fails on AIX 3.25. Tidy up formatting and exit immediately
#                  if user is not root.
# 12Aug97     SN : CMVC defect IX66740. Adding saveDT to the Panther95/Lynx
#                  migration PTFs (U449173 & U449177). No code change done.
#------------------------------------------------------------------------------
#/
LPPname=dirTalk
LPPdir=/usr/lpp/${LPPname}
DEVICE=
integer SAVEBS
SAVEV=210       # Current version of saveDT tools
VERSIONF=./.version
SAVEVERSF=./.save_version

TMPBKF=/tmp/bklist$$
OLDTAPEBS=512
TAPEBS=32768
TAPE_FLAG=""

#  ca/include and vagtxt_dir is handled later

curdir_list="ca/*_dir ca/bin distribution_dir prompt_dir stbl_dir scr_db upmb_dir \
 voicedir xref_db"
arcdir_list="oamlog"


###############################################################################
# list file systems

list_file_systems()
{

typeset -L15 fs_name
typeset -L20 mount_pt     # define field widths
typeset -L15 size
typeset -L15 free
typeset -L15 k_size
typeset -L15 k_free
typeset -L5 percent_used
typeset -L15 filesystem

print
print "Available File Systems"
print "----------------------"
print

sleep 0

lsfs | while read fs_name node mount_pt rest1
do
   if [[ $fs_name != "Name           " ]]
   then

     if [[ $mount_pt != "Mount               " ]] then

        df $mount_pt | while read filesystem size free percent_used iused percent_iused dir_name rest2
        do

          if [[ $filesystem != "Filesystem     " ]]
          then

             ((k_size = size / 2))
             ((k_free = free / 2))

             print "$filesystem $mount_pt $k_size $k_free $percent_used"
          fi
        done

     fi

   else
     fs_name="Filesystem"
     mount_pt="Mount Pt"
     size="Size (Kbytes)"
     free="Free (KBytes)"
     percent_used="%Used"
     print "$fs_name $mount_pt $size $free $percent_used"
   fi
done


}

###############################################################################
# calculate how much K of space is needed to save current data

calculate_space()
{

du -sk $CUR_DIR/ca | read ca_K path
du -sk $CUR_DIR/distribution_dir | read distribution_dir_K path
du -sk $CUR_DIR/prompt_dir | read prompt_dir_K path
du -sk $CUR_DIR/scr_db | read scr_db_K path
du -sk $CUR_DIR/stbl_dir | read stbl_dir_K path
du -sk $CUR_DIR/upmb_dir | read upmb_dir_K path
du -sk $CUR_DIR/vagtxt_dir | read vagtxt_dir_K path
du -sk $CUR_DIR/voicedir | read voicedir_K path
du -sk $CUR_DIR/xref_db | read xref_db_K path

du -sk $ARC_DIR/oamlog | read oamlog_K path

du -ak $SYSPARM_DIR/rd.data | read rd_K path

du -ak $SYS_DIR/admclass_table | read admclass_K path
du -ak $SYS_DIR/.admclass_table | read dot_admclass_K path

du -ak $VAE/sw/environ/.vaeprofile | read vaeprofile_K path

du -ak $CUR_DIR/vagtxt_dir/menu_resrc.*[0-9] | read menu_resrc_K path

((total_K = ca_K +               \
            distribution_dir_K + \
            prompt_dir_K +       \
            scr_db_K +           \
            stbl_dir_K +         \
            upmb_dir_K +         \
            vagtxt_dir_K +       \
            voicedir_K +         \
            xref_db_K +          \
            oamlog_K +           \
            rd_K +               \
            admclass_K +         \
            dot_admclass_K +     \
            vaeprofile_K +       \
            menu_resrc_K))

echo "Total amount of space needed to save DirectTalk : $total_K Kbytes"

}

###############################################################################
# function to reset tape drive before exit
clean_exit()
{
   if [ -n "$DEVICE" ]; then
      echo "Reset tape device..."
      DEVICEbase=`basename ${DEVICE%.*}`

#trace#      echo "*** trace:clean_exit: DEVICEbase=$DEVICEbase SAVEBS=$SAVEBS ***"

      chdev -l ${DEVICEbase} -a block_size=$SAVEBS
   fi

   cd $CUR_DIR

   if [ -f $VERSIONF ]; then
     rm -f $VERSIONF
   fi
   if [ -f $TMPBKF ]; then
     rm -f $TMPBKF
   fi
   echo "Exiting saveDT..."
   exit
}

###############################################################################
# Report Tape Error
tape_error()
{
   echo "ERROR:  Can't access device $DEVICE...Aborting"
   clean_exit
}

###############################################################################
# Write directly to tape
write_to_tape()
{

DEVICEbase=`basename ${DEVICE%.*}`

#trace#     print "*** DEVICE = $DEVICE, DEVICEbase = $DEVICEbase ***"

######################################
# Save Tape Drive Block Size setting #
######################################

SAVEBS=`lsattr -EO -l ${DEVICEbase} -a block_size | grep -v block_size`

#trace#     echo "*** Initial Tape Drive Block Size setting : $SAVEBS ***"

#############################################
# Change Tape Drive Block Size to OLDTAPEBS #
#############################################

chdev -l ${DEVICEbase} -a block_size=$OLDTAPEBS
rc=$?

#trace#     echo "*** Changed Block Size. New Block Size Seting : $OLDTAPEBS ***"

if [ $? -ne 0 ]; then
   tape_error
fi
trap clean_exit 2
echo "\n--------------------------------------------"
echo "\nDEVICE:  $DEVICE"
lsattr -E -l `basename ${DEVICE%.*}`
rc=$?

echo "\nDirectTalk Environment:  $VAE\n"
echo "Press ENTER to continue  \c"
read go_on

###############################################################################
# Rewind tape to start
echo "Rewind tape..."
tctl -f ${DEVICE%.*} rewind
if [ $? -ne 0 ]; then
   tape_error
fi

###############################################################################
# Save version information and block size used for saving data
echo -------------------------------
echo "Saving DirectTalk version..."
cd $CUR_DIR

# Handle different packaging formats between AIX 3 and 4
if [ $AIXVER -eq 4 ]; then
 lslpp -hcq "dirTalk.DT.rte" | awk -F: '{print $3}' | tail -1 >$VERSIONF
else
 lslpp -hcq "dirTalk.rte.obj" | awk -F: '{print $4}' | tail -1 >$VERSIONF
fi

echo "$SAVEV  $SAVEBS" > $SAVEVERSF

#trace#      print "*** Writing data in $VERSIONF and $SAVEVERSF using Block Size of $TAPEBS ***"

#trace# print "Data in $VERSIONF :"
#trace# cat $VERSIONF
#trace# print

#trace# print "Data in $SAVEVERSF :"
#trace# cat $SAVEVERSF
#trace# print


echo "ls $VERSIONF $SAVEVERSF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "

ls $VERSIONF $SAVEVERSF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS

if [ $? -ne 0 ]; then
   tape_error
fi

###############################################################################
# Reset tape device to original blocksize for the rest of the data

#trace#    print "*** About to reset tape device to original blocksize for the rest of the data ***"

CURRENTBS=`lsattr -EO -l ${DEVICEbase} -a block_size | grep -v block_size`

#trace#    print "*** Current Block Size setting = $CURRENTBS ***"

if [ $CURRENTBS -ne $SAVEBS ]; then

#trace#    print "*** CURRENTBS ($CURRENTBS) not equal to SAVEBS ($SAVEBS) so changing it back ***"

#trace#    echo "*** Setting tape device to original block size ($SAVEBS) for data write ***"

   chdev -l ${DEVICEbase} -a block_size=$SAVEBS
   if [ $? -ne 0 ]; then
      tape_error
   fi
   trap clean_exit 2

   # Must now fast forward past fist file
   tctl -f $DEVICE rewind
   sleep 3
   tctl -f $DEVICE fsf 1
   sleep 3
fi

###############################################################################
# Save .vaeprofile file
echo -------------------------------
echo "Saving DirectTalk profile..."
cd ${VAE}/sw/environ
VAEPROFILE=".vaeprofile_`cat $CUR_DIR/$VERSIONF`"

print "Copying .vaeprofile to $VAEPROFILE in ${VAE}/sw/environ"

cp .vaeprofile $VAEPROFILE
echo "ls $VAEPROFILE | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "
ls $VAEPROFILE | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS
if [ $? -ne 0 ]; then
   tape_error
fi


###############################################################################
# Save Administrator Profile files
echo -------------------------------
echo "Saving administrator profiles..."
cd ${SYS_DIR}
ADMCLASSF=`ls adm_class admclass_table .admclass_table 2>/dev/null`
echo "ls $ADMCLASSF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "
ls $ADMCLASSF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS
if [ $? -ne 0 ]; then
   tape_error
fi

###############################################################################
# Save Sysparm file
echo -------------------------------
echo "Saving system parameters..."
cd $SYSPARM_DIR
echo "ls rd.data | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "
ls rd.data | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS
if [ $? -ne 0 ]; then
   tape_error
fi

###############################################################################
# Save rest of database in $CUR_DIR
echo -------------------------------
echo "Saving database..."
cd $CUR_DIR
rm -f $TMPBKF 2>/dev/null
find ca/*_dir ca/bin distribution_dir prompt_dir stbl_dir scr_db \
        upmb_dir voicedir xref_db -print >>$TMPBKF 2>/dev/null
find vagtxt_dir ! -name "menu_resrc.001*" -print >>$TMPBKF

# Get a list of all files in the custom server include directory
ca_include_list_tmp=`find ca/include/* -print`

# For each of those files, check if it is a file that is distributed with
# DirectTalk via the "lslpp -f" command. We do not want to save these
# since on restore we will overwrite the version supplied with the new
# release.
for file in $ca_include_list_tmp
do
   if [ $AIXVER -eq 4 ]; then
      lslpp -f "dirTalk.DT.db" | grep $file | num=`wc -l`
   else
      lslpp -f "dirTalk.db.obj" | grep $file | num=`wc -l`
   fi
   if [ $num -eq 0 ]; then
      echo $file >> $TMPBKF
   fi
done

#trace# print "Following files are being saved : "
#trace# cat $TMPBKF
#trace# print

echo "cat $TMPBKF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "
cat $TMPBKF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS
if [ $? -ne 0 ]; then
   tape_error
fi

###############################################################################
# Save Archived Statistics in $ARC_DIR
echo -------------------------------
echo "Saving archived statistics..."
cd $ARC_DIR
find oamlog -print >$TMPBKF

#trace#  print "Following files are being saved : "
#trace#  cat $TMPBKF
#trace#  print

echo "cat $TMPBKF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS "
cat $TMPBKF | /usr/sbin/backup -iqf- | dd of=$DEVICE bs=$TAPEBS
if [ $? -ne 0 ]; then
   tape_error
fi

echo -------------------------------
echo "Finished saving data to tape"

if [ -n "$DEVICE" ]; then
    echo "Reset tape device..."
    DEVICEbase=`basename ${DEVICE%.*}`
    chdev -l ${DEVICEbase} -a block_size=$SAVEBS
#trace#     print "*** Changing Block Size setting back to $SAVEBS ***"
fi

echo "Exiting saveDT..."

} # end of write_to_tape()


###############################################################################
# Check write permission of directory where database will be copied to.
check_dir_perms()
{

   if [[ -f $DEVICE ]]   # User specified a file instead of a directory
   then
      print "\nYou must specify a directory where the database will be copied to.\n"
      return -1
#   elif [[ -d `dirname $DEVICE` ]] && [[ -w `dirname $DEVICE` ]] && [[ -a `dirname $DEVICE` ]]  #  Can write to directory okay
   elif [[ -d $DEVICE ]] && [[ -w $DEVICE ]] && [[ -a $DEVICE ]]  #  Can write to directory okay
   then
      return 0
      break
   else
      print "\nCannot write to $DEVICE. Check the permissions for $DEVICE\n"
      return -1
   fi


}


###############################################################################
# Check write permission of the specified tar file
check_file_perms()
{

   if [ -f $DEVICE -a -w $DEVICE ]; then           # Can write to file ok
     return 0
     break
   else
     if [ -w `dirname $DEVICE` -a ! -f $DEVICE ]; then   # can write to file ok
       return 0
       break
     else
       echo "\nCan't write to $DEVICE\n"
       return -1
     fi
   fi

}

###############################################################################
# List Available tape drives
list_tape_drives()
{

   total=`lsdev -C -c tape -S a | awk '{print NR}' | tail -1`
   if [ $total -gt 1 ]; then  # must choose a taped device
      while true
      do
         echo "Available Tape Devices:"
         lsdev -C -c tape -S a
         echo "\nEnter tape device name (e.g. rmt0) > \c"
         read taped
         if [ -z "$taped" ]; then
            echo "No device specified"
         else
            chk=`echo $tape_dev | grep ${taped%.*}`
            if [ -n "$chk" ]; then    # entry ok
              if [[ $TAPE_FLAG = "WRITE" ]] then
                 DEVICE=/dev/${taped%.*}.1
              else
                 DEVICE=/dev/${taped}
              fi
              break
            else
              echo "\nCan't find $taped"
              exit
            fi
         fi
      done
      break
   else      #only one tape device
      if [[ $TAPE_FLAG = "WRITE" ]] then
        DEVICE=/dev/`echo $tape_dev | awk '{print $1}'`.1
      else
        DEVICE=/dev/`echo $tape_dev | awk '{print $1}'`
      fi
      break
   fi

}

tar_up_files()
{

echo "tar -c -f $DEVICE $VERSIONF $SAVEVERSF -C $SYS_DIR $ADMCLASSF \\"
echo "-C $SYSPARM_DIR rd.data \\"
echo "-C $VAE/sw/environ $NEWVAEPROFILE \\"
echo "-C $CUR_DIR $curdir_list $vagtxt_dir_list ${ca_include_list}\\ "
echo "-C $ARC_DIR $arcdir_list\n"

tar -c -f $DEVICE $VERSIONF $SAVEVERSF -C $SYS_DIR $ADMCLASSF \
 -C $SYSPARM_DIR rd.data \
 -C $VAE/sw/environ $NEWVAEPROFILE \
 -C $CUR_DIR $curdir_list $vagtxt_dir_list ${ca_include_list}\
 -C $ARC_DIR $arcdir_list

}

copy_database()
{

# Create Directories

  cd $DEVICE
  mkdir sw
  mkdir db

  cd $DEVICE/sw
  mkdir environ

  cd $DEVICE/db
  mkdir sys_dir
  mkdir sysparm_dir
  mkdir archive_dir
  mkdir current_dir

  cd $DEVICE/db/current_dir
  mkdir vagtxt_dir
  mkdir ca

  cd $DEVICE/db/current_dir/ca
  mkdir include

# Copy Directories

  cd $CUR_DIR

  print "Copying $VERSIONF"
  cp -p $VERSIONF $DEVICE/$VERSIONF

  print "Copying $SAVEVERSF"
  cp -p $SAVEVERSF $DEVICE/$SAVEVERSF

  print "Copying Custom Server Directories"
  cp -pr $CUR_DIR/ca/*_dir $DEVICE/db/current_dir/ca

  print "Copying $CUR_DIR/ca/bin"
  cp -pr $CUR_DIR/ca/bin $DEVICE/db/current_dir/ca

  print "Copying $CUR_DIR/distribution_dir"
  cp -pr $CUR_DIR/distribution_dir $DEVICE/db/current_dir

  print "Copying $CUR_DIR/prompt_dir"
  cp -pr $CUR_DIR/prompt_dir $DEVICE/db/current_dir

  print "Copying $CUR_DIR/stbl_dir"
  cp -pr $CUR_DIR/stbl_dir $DEVICE/db/current_dir

  print "Copying $CUR_DIR/scr_db"
  cp -pr $CUR_DIR/scr_db $DEVICE/db/current_dir

  print "Copying $CUR_DIR/upmb_dir"
  cp -pr $CUR_DIR/upmb_dir $DEVICE/db/current_dir

  print "Copying $CUR_DIR/voicedir"
  cp -pr $CUR_DIR/voicedir $DEVICE/db/current_dir

  print "Copying $CUR_DIR/xref_db"
  cp -pr $CUR_DIR/xref_db $DEVICE/db/current_dir

  print "Copying $ARC_DIR/oamlog"
  cp -pr $ARC_DIR/oamlog  $DEVICE/db/archive_dir

  print "Copying $VAE/sw/environ/$NEWVAEPROFILE"
  cp -p $VAE/sw/environ/$NEWVAEPROFILE $DEVICE/sw/environ/$NEWVAEPROFILE

  print "Copying SYSPARM_DIR/rd.data"
  cp -p $SYSPARM_DIR/rd.data $DEVICE/db/sysparm_dir/rd.data


  ls ${SYS_DIR}adm_class ${SYS_DIR}admclass_table ${SYS_DIR}.admclass_table 2>/dev/null | awk -F/ '{print $NF}' | while read admclass_file
do
  print "Copying $SYS_DIR/$admclass_file"
  cp -p $SYS_DIR/$admclass_file $DEVICE/db/sys_dir/$admclass_file
done


  find vagtxt_dir ! -name "menu_resrc.001*" ! -name "vagtxt_dir" -print | while read vagtxt_file
  do
    print "Copying $CUR_DIR/$vagtxt_file"
    cp -p $CUR_DIR/$vagtxt_file $DEVICE/db/current_dir/$vagtxt_file
  done

  IFS=" "
  set ${ca_include_list}
  for ca_include_file in ${ca_include_list}
  do
    print "Copying $CUR_DIR/$ca_include_file"
    cp -p $CUR_DIR/$ca_include_file $DEVICE/db/current_dir/$ca_include_file
  done

#  set ${curdir_list}
#  for curdir_dir in ${curdir_list}
#  do
#    print "Copying $CUR_DIR/$curdir_dir"
#    cp -pr $CUR_DIR/$curdir_dir $DEVICE/db/current_dir
#  done

  print "Finished copying database files"

}

create_file_lists()
{

cd $CUR_DIR

###############################################################################
# Handle different packaging formats between AIX 3 and 4
if [ $AIXVER -eq 4 ]; then
 lslpp -hcq "dirTalk.DT.rte" | awk -F: '{print $3}' | tail -1 >$VERSIONF
else
 lslpp -hcq "dirTalk.rte.obj" | awk -F: '{print $4}' | tail -1 >$VERSIONF
fi

echo $SAVEV > $SAVEVERSF
vagtxt_dir_list=`find vagtxt_dir ! -name "menu_resrc.001*" ! -name "vagtxt_dir" -print`

###############################################################################
# Get a list of all files in the custom server include directory
ca_include_list_tmp=`find ca/include/* -print`

###############################################################################
# For each of those files, check if it is a file that is distributed with
# DirectTalk via the "lslpp -f" command. We do not want to save these
# since on restore we will overwrite the version supplied with the new
# release.
###############################################################################
for file in $ca_include_list_tmp
do
   if [ $AIXVER -eq 4 ]; then
      lslpp -f "dirTalk.DT.db" | grep $file | num=`wc -l`
   else
      lslpp -f "dirTalk.db.obj" | grep $file | num=`wc -l`
   fi
   if [ $num -eq 0 ]; then
      ca_include_list=$ca_include_list' '$file
   fi
done

###############################################################################
# Copy .vaeprofile to file with LPP version appended
NEWVAEPROFILE=".vaeprofile_`cat $VERSIONF`"
cp $VAE/sw/environ/.vaeprofile $VAE/sw/environ/${NEWVAEPROFILE}
ADMCLASSF=`ls ${SYS_DIR}adm_class ${SYS_DIR}admclass_table ${SYS_DIR}.admclass_table 2>/dev/null | awk -F/ '{print $NF}'`

}

tar_option()
{

create_file_lists
tar_up_files

}

copy_option()
{

create_file_lists
copy_database

}

main_part()
# Main script
{

# Save AIX version
AIXVER=`uname -v`

###############################################################################
# Set Environment

if [ -n "$VAE" ]; then  # Set DirectTalk environment using this
   . $VAE/sw/environ/.vaeprofile
else                    # Look for vae.setenv file
   if [ -x ${LPPdir}/tools/vae.setenv ]; then
     . ${LPPdir}/tools/vae.setenv   # Set the DirectTalk environment
   else
     echo "\nCan't set DirectTalk environment -- Aborting"
     exit 1
   fi
fi

################################################################################
# Check to see if DirectTalk is currently running
dtuser=""
dtgroup=""
if [ -f $VAE/sw/environ/dTalk.user ]; then
   cat $VAE/sw/environ/dTalk.user | read dtuser dtgrp
fi
if [ -n "$dtuser" ]; then     # user has been defined;
   chkNODEM=`ps -u $dtuser | awk -F" " '$5 == "NODEM" { print $2 ; }'`
else
   chkNODEM=`ps -de | awk -F" " '$4 == "NODEM" { print $2 ; }'`
fi
if [ "-n" "$chkNODEM" ]; then
   echo "\nDirectTalk is currently running."
   echo "Shutdown DirectTalk before attempting migration\n"

   exit

fi


###############################################################################
# No command line options                                                     #
###############################################################################

# Set Device

if [ -z "$DEVICE" ]; then  # Prompt for device
   tape_dev=`lsdev -C -c tape -S a`
   if [ -n "$tape_dev" ]; then   # Tape Drive installed and Available
      TAPE_TAR="Tape (using tar)"
      TAPE_WR="Tape (using direct write)"
   else
      TAPE_TAR="Tape (using tar) <--- TAPE DRIVE NOT AVAILABLE"
      TAPE_WR="Tape (using direct write) <--- TAPE DRIVE NOT AVAILABLE"
   fi
   echo "\nSave DirectTalk Data Script--Backup Device\n\n"
   print
   calculate_space
   print
   PS3='Select device type: (type ENTER to list choices) >'
   select one in "Diskette (Recommended only for Small Amount of Data)" "Hard Disk file (using tar)" "Hard Disk directory (using copy)" "$TAPE_TAR" "$TAPE_WR" "Quit"
   do
     case $one in
     "Diskette (Recommended only for Small Amount of Data)" )
        DEVICE=/dev/fd0
        echo "Using Device $DEVICE"
        break
        ;;
     "Hard Disk file (using tar)" )
        list_file_systems
        echo "\nEnter complete path for tar file on Hard Disk > \c"
        read DEVICE
        check_file_perms
        if (($? != 0))
        then
           echo "error returned from check_file_perms!"
           exit
        fi
        break
        ;;
     "Hard Disk directory (using copy)" )
        list_file_systems
        echo "\nEnter complete path for destination directory on Hard Disk > \c"
        read DEVICE
        check_dir_perms
        if (($? != 0))
        then
           echo "error returned from check_dir_perms!"
           exit
        else
           copy_option
        fi
        exit
        ;;
     "Tape (using tar)" )
        echo "\nUsing Tar to save data to tape"
        TAPE_FLAG="TAR"
        list_tape_drives
        break
        ;;
     "Tape (using direct write)" )
        echo "\nWriting data directly to tape"
        TAPE_FLAG="WRITE"
        list_tape_drives
        write_to_tape
        exit
        ;;
     "Tape (using tar) <--- TAPE DRIVE NOT AVAILABLE" )
        echo "Sorry, there are no tape drives available !"
        exit
        ;;
     "Tape (using direct write) <--- TAPE DRIVE NOT AVAILABLE" )
        echo "Sorry, there are no tape drives available !"
        exit
        ;;
     "Quit" )
        exit
        ;;
     esac
   done
fi                      # Device specified on command line


echo "--------------------------------------------"
echo "\nDEVICE:  $DEVICE"
echo "DirectTalk Environment:  $VAE\n"
echo "Press ENTER to continue  \c"
read go_on

tar_option

}

#
# Main
#

# Check if user is root
if [ `id -u -nr` != "root" ]; then
  echo "ERROR: saveDT must be executed from the \"root\" login; exiting..."
  exit 1
fi

rm -f `basename $0`.log 2>/dev/null
main_part 2>&1 | tee -a `basename $0`.log

rm -f $VERSIONF $SAVEVERSF

exit
