#-------------------------------
# Sub Function to execute sqlldr
#-------------------------------
run_sqlldr()
{
 sqlldr parfile=$1 control=$2 log=$PGM_DATA/$DLOG bad=$PGM_DATA/$DBAD data=$3 >> $PGM_DATA/$SQLLOG 2>&1
}

#----------------------------------------------------------
# Load Found Data files into respective tables using sqlldr:
#-----------------------------------------------------------
for i in $DATA_FILES
do
  #---------------------------------
  # Checking file type (zip/txt/csv)
  #---------------------------------
  cd $PGM_DATA
  case $i in
         *zip) BNAME=`basename $i .zip`;
               DATAF=`basename $i .zip`.txt;
               unzip -p $i | cat -vet | sed -e s/\\^M\\$//g > $DATAF;;
         *txt) DATAF=$i;
               BNAME=`basename $i .txt`;;
         *csv) DATAF=$i;
               BNAME=`basename $i .csv`;;
  esac
  #----------------------------------------------------------
  # Now call run_sqlldr function with respective control file
  #----------------------------------------------------------
  case $BNAME in

              *ELIGI*)ELIG_CNT=`expr $ELIG_CNT - 1`;
                     cd $MEM_DIR;
                     run_sqlldr $PARFILE member_elig_glhp.ctl $PGM_DATA/$DATAF;;

              *REASON*)REA_CNT=`expr $REA_CNT - 1`;
                       cd $REA_DIR;
                       run_sqlldr $PARFILE reason_code_glhp.ctl $PGM_DATA/$DATAF;;

              *VENDOR*)VEN_CNT=`expr $VEN_CNT - 1`;
                       cd $VEN_DIR;
                       run_sqlldr $PARFILE glhp_vendor.ctl $PGM_DATA/$DATAF;;

              *DEMO*)MEM_CNT=`expr $MEM_CNT - 1`;
                       cd $MEM_DIR;
                       run_sqlldr $PARFILE glhp_memeber.ctl $PGM_DATA/$DATAF;;

              *PROVIDER_EX*)PRO_CNT=`expr $PRO_CNT - 1`;
                         cd $PRO_DIR;
                         run_sqlldr $PARFILE stg_provider_glhp.ctl $PGM_DATA/$DATAF;;

              *SPECIALTY*)SPEC_CNT=`expr $SPEC_CNT - 1`;
                         cd $PRO_DIR;
                         run_sqlldr $PARFILE stg_provider_spec_glhp.ctl $PGM_DATA/$DATAF;;

              *CONTRACT*)CONC_CNT=`expr $CONC_CNT - 1`;
                         cd $PRO_DIR;
                         run_sqlldr $PARFILE stg_prov_contract_glhp.ctl $PGM_DATA/$DATAF;;

              *INSTITUTIONAL*)INST_CNT=`expr $INST_CNT - 1`;
                                       cd $CLM_DIR;
                                       run_sqlldr $PARFILE inst_claim_glhp.ctl $PGM_DATA/$DATAF;;

              *PROFESSIONAL*)PROF_CNT=`expr $PROF_CNT - 1`;
                                       cd $CLM_DIR;
                                       run_sqlldr $PARFILE prof_claim_glhp.ctl $PGM_DATA/$DATAF;;

              *RX*)RX_CNT=`expr $RX_CNT - 1`;
                                     cd $CLM_DIR;
                                     run_sqlldr $PARFILE rx_claim_glhp.ctl $PGM_DATA/$DATAF;;

 esac

 #----------------------
 #Remove temporary files
 #----------------------
 case $i in
     *zip)rm $PGM_DATA/$DATAF;;
 esac

 if [ -f $PGM_DATA/$DBAD ]
 then
   cp $PGM_DATA/$DBAD $PGM_DATA/$BNAME.bad
   rm $PGM_DATA/$DBAD
 fi;

done