#!/bin/sh
#*******************************************************************************
#             PACOR II SOFTWARE
#       Property of the U.S. Government
#            NASA/GSFC/Code 560
#*******************************************************************************
#PURPOSE: Installs the PICS database using the Oracle import utility 
#
#INVOCATION METHOD: ins_import user/pw 
#
#
#DEVELOPMENT HISTORY:
#
#       Author          Change-ID       Release Date    Description of Change
#       ------          ---------       ------- ----    ---------------------
#    P. Matthews        B3R2            07/07/94        Initial release 
#    M.Rauschenberger   B4R2.1          08/26/94        Changed script name
#    P. Matthews        B4R2.1          11/16/94        Changed input arguments
#    P. Matthews        B4R2.1          12/14/94        Remove grants from export
#    I. Horowitz        ACE0615     ACE R2.0  06/30/96  Added code to ask if import 
#							 to be performed, and to allow
#							 user to select which tables
#    I. Horowitz        N/A    	  TRACE R1.0  07/08/96  Changed awk to nawk when porting
#							 from HP-UX to IRIX
#    R. Wiechert                                        Relocated temporary files to /tmp
#
#********************************************************************************
if test $# -ne 4
then
   echo '*************************************************************'
   echo 'Error, you must enter an Oracle UserID/Password, Log File,   '
   echo 'Source DB account, and Destination DB account                '
   echo '*************************************************************'
   exit
fi

doimport=Y
importopt=N
filename=pacor2db.dmp

import_log=/tmp/oracle_import_$$.log
rm -f $import_log
touch $import_log

echo " "
echo "Is any part of another database to be imported into this one? (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 DB import " >> $import_log
   echo " " >> $import_log
   exit
fi

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

echo ""
echo "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 $1 $2   # remove user's existing objects
  imp $1 FROMUSER=$3 \
  TOUSER=$4 \
  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 "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=$3 \
  TOUSER=$4 \
  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 database import has completed ..."
echo "********************************************"
echo ""
