#!/bin/sh
#*******************************************************************************
#             PACOR II SOFTWARE
#       Property of the U.S. Government
#            NASA/GSFC/Code 560
#*******************************************************************************
#PURPOSE: Installs the QAWS tables using the Oracle import utility 
#
#INVOCATION METHOD: ins_import_QAWS user/pw 
#
#
#DEVELOPMENT HISTORY:
#
#       Author          Change-ID       Release Date    Description of Change
#       ------          ---------       ------- ----    ---------------------
#    I. Horowitz        ACE0615     ACE R2.0  06/30/96  Added to merge QAWS tables into
#							 DPS DB
#    I. Horowitz        N/A       TRACE R1.0  07/08/96  Changed awk to nawk, port from
#							 HP-UX to IRIX
#
#********************************************************************************
if test $# -ne 3
then
   echo '*************************************************************'
   echo 'Error, you must enter an Oracle UserID/Password, Log File,   '
   echo 'and Destination DB account                '
   echo '*************************************************************'
   exit
fi

doimport=Y
importopt=N
filename=expdat.dmp
import_log=tmp_qaw_import.log

rm -f $import_log
touch $import_log

echo " "
echo -n "Are QAWS tables to be imported into this database? (y/n) [$doimport] "
read in_doimport
if [ $in_doimport ]
then
   doimport=`echo $in_doimport | tr '[a-z]' '[A-Z]'`
fi

if [ $doimport = N ]
then
   echo " " >> $import_log
   echo "User elected to skip QAWS DB import " >> $import_log
   echo " " >> $import_log
   exit
fi

echo " "
echo -n "Enter the name of the owner account for the tables being imported: "
read source_name
if [ -z $source_name ]
then
   echo " " >> $import_log
   echo "User did not supply import file owner account name " >> $import_log
   echo " " >> $import_log
   exit
fi

echo ""
echo -n "Enter the name of the file to be imported [$filename]: "
read infilename
if [ $infilename ]
then
  filename=$infilename
fi

echo ""
echo -n "Import entire database (Y/N)? [$importopt]: "
read inimportopt
if [ $inimportopt ]
then
  importopt=`echo $inimportopt | tr '[a-z]' '[A-Z]'`
fi

if [ "$importopt" = Y ]
then
  clear
  ins_remobjects_QAWS $1 $2   # remove user's existing objects
  imp $1 FROMUSER=$source_name \
  TOUSER=$3 \
  FILE= $filename \
  IGNORE=Y \
  COMMIT=Y \
  GRANTS=N \
  LOG=$import_log
elif [ "$importopt" = N ]
then
  echo "List of available tables: "
  grep '^TABLE' $filename | sort -u | \
  nawk '{ gsub("\"", " ");\
          if ( NR%2 == 0 ) printf("\n");\
          printf("%-40s   ",$2) } \
         END { printf("\n\n") }'

  echo "Enter the name of the table(s) to import separated by commas"
  echo -n "Tablename(s): "
  read tablenames

  if [ -z "$tablenames" ] 
  then
     echo " " >> $import_log
     echo "User did not supply list of tables to be imported" >> $import_log
     echo " " >> $import_log
     exit
  fi

  echo " "
  imp $1 FROMUSER=$source_name \
  TOUSER=$3 \
  FILE= $filename \
  IGNORE=Y \
  COMMIT=Y \
  GRANTS=N \
  TABLES="$tablenames" \
  LOG=$import_log
else 
  echo '**************'
  echo 'Invalid option'
  echo '**************'
exit
fi
cat $import_log >> $2
rm -f $import_log

echo ""
echo ""
echo "********************************************"
echo "The QAWS tables have been imported..."
echo "********************************************"
echo ""
