#!/bin/ksh
#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2001 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#


########################################################################
#                                                                      #
# IDCAMS Utility      :     DELETE                                     #
# Purpose             :     Re-initialize VSAM dataset(s)              #
#                           Remove FS file(s)                          #
#                           Remove an ALIAS entry in File_Map          #
#                                                                      #
########################################################################
echo "IDCAMS : start DELETE"
if [ $IDEBUG = "yes" ] 
then
	echo "$0  starts with :"
	for i in $1
	do
		param=`echo $i | cut -f1 -d')' | cut -f2 -d'('`
		key=`echo $i | cut -f1 -d'('`
		echo "                     key=$key     param=$param"
	done
fi
input=null
multi=null
filflap=0
novsam=null
cluste=null
nvsampath=null
type=null
key=null
count=0
aa=0
temp=0
aliasto=null
gdgroup=null
catalg=null

cat $DEVNULL >$EBMTMPDIR/delete_args.$$
if [ $? -ne 0 ]
then
   echo "IDCAMS (S) DELETE failed, cannot create $EBMTMPDIR/delete_args.$$"
   exit 2
fi
chmod 755 $EBMTMPDIR/delete_args.$$

cat $DEVNULL >$EBMTMPDIR/delete_args_mod.$$
if [ $? -ne 0 ]
then
   echo "IDCAMS (S) DELETE failed, cannot create $EBMTMPDIR/delete_args_mod.$$"
   exit 2
fi
chmod 755 $EBMTMPDIR/delete_args_mod.$$

echo $* > $EBMTMPDIR/delete_args.$$
awk -f $PUBLIC/bin/delete_awk $EBMTMPDIR/delete_args.$$ > $EBMTMPDIR/delete_args_mod.$$

delete_args_mod=`cat $EBMTMPDIR/delete_args_mod.$$`
rm -f $EBMTMPDIR/delete_args.$$ 2>$DEVNULL
rm -f ${EBMTMPDIR}/delete_args_mod.$$ 2>$DEVNULL

for i in $delete_args_mod
do
 if [ "a$i" = "a)" ]
 then 
   continue
 fi
 if [ "a$i" = "a(" ]
 then 
   continue
 fi
	count=`expr $count + 1`
	if [ $key = multi ]
	then
		testa=`echo ${i}_ | cut -f2 -d')'`
		if [ $testa = "_" ]
		then
			key=multied
			param=`echo $i | cut -f1 -d')'`
			multi="$multi $param"
		else
			multi="$multi $i"
		fi
	else
		param=`echo $i | cut -f1 -d')' | cut -f2 -d'('`
 		key=`echo $i | cut -f1 -d'('`_
		if [ $key = "FILE_" ]
		then 
			filflap=1
		fi
		if [ $key  = "_" ]
		then
			if [ $filflap -eq 0 ]
			then
				key=multi
				multi=`echo $i | cut -f2 -d'('`
				testa=`echo ${multi}_ | cut -f2 -d')'`
				if [ $testa = "_" ]
				then
					key=multied
					multi="$param"
				fi
			fi
			filflap=0
		else
			key=`echo $i | cut -f1 -d'('`
		fi
	fi

		case $key in
	ALIAS|alias) 					   aliasto=$key;;
	GENERATIONDATAGROUP|GDG|generationdatagroup|gdg)   gdgroup=$key;;
	CATALOG|CAT|catalog|cat)			   catalg=$param;;
	NONVSAM|NVSAM|nonvsam|nvsam)                       novsam=$param;;
	CLUSTER|CL|cluster|cl)				   cluste=$param;;
	ALTERNATEINDEX|AIX|alternateindex|aix)		   
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	PURGE|PRG|purge|prg|NOPURGE|NPRG|nopurge|nprg)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	PAGESPACE|PGSPC|pagespace|pgspc)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	PATH|path)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	SPACE|SPC|space|spc)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	USERCATALOG|UCAT|usercatalog|ucat)
			   ;;
	ERASE|ERAS|erase|eras)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	NOERASE|NERAS|noerase|neras)
			   ;;
	FILE|file)
			   ;;
	FORCE|FRC|force|frc)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	NOFORCE|NFRC|noforce|nfrc)
			   ;;
				_) ;;
	SCRATCH|SRC|scratch|src)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	NOSCRATCH|NSRC|noscratch|nsrc)
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS (W) DELETE $key command ignored"
			fi ;;
	multi)				input=present		;;
	multied)			input=present		;;
	*)
		if [ $count -eq 1 ]
		then
			multi=$param
			input=present
		fi ;;
	esac

done

if [ $IDEBUG = "yes" ]
then
   set -x
fi

if [ $catalg = null ] 
then
	catalg=`ebmsed $JOB_CAT`
fi


if [ $input = null ]
then
	echo "DELETE (S) - entryname is required"
	exit 16
fi

if [ $IDEBUG = "yes" ] 
then
	RDMDL="echo rdmdl"
	RDMCLR="echo rdmclr"
	RM="echo rm"
	COBDELETE="echo unikixbld"
else
	RDMDL=rdmdl
	RDMCLR=rdmclr
	RM=rm
	COBDELETE="unikixbld"
fi

#--------------------------------------------------------------
# following line handles:
#
# DELETE 'A.B.C'  PURGE
#
# dataset in apostrophes
#--------------------------------------------------------------
multi=`eval echo $multi`

if [ "a$FILEMAP" = "a" ]
then
   echo "DELETE (S) The FILEMAP environment variable is not set"
   exit 16		
fi
if [ $aliasto != null ]
then
   # The ALIAS parameter was specified
	for i in $multi
	do
      grep "^${i};$catalg;" $FILEMAP >$DEVNULL 2>&1
      if [ $? -ne 0 ]
      then
         echo "DELETE (W) ALIAS: $i   CATALOG: $catalg   not found" 
         cat $DEVNULL > ${JON}_RET
         echo "_${STEPNAME}_ 8" > ${JON}_RET
         cat ${JON}_RET >> ${JON}_RETCOD
         continue
      fi
		path=`ftval $i $catalg 4`\;
		temp=$?
		path=`eval echo $path`
		if [ $temp != "0" ]
		then
         echo "DELETE (S) Command failed: ftval $i $catalg 4"
         exit 255
		fi
      path=`ebmsed $path`
		cat $FILEMAP|grep $path > $EBMTMPDIR/del.$$
      if [ $? -ne 0 ]
      then
         echo " "
         echo "DELETE (S)  Unable to update file $EBMTMPDIR/del.$$"
         echo " "
			exit 255
      fi
		while read alias_name
		do
			aliasdel=`echo $alias_name | cut -f1 -d';'`
			if [ $aliasdel = $i ]
			then
				if [ $IDEBUG = "yes" ] 
				then
				     echo " --- remove $aliasdel --- "
            else
ed -s $FILEMAP <<!
/$aliasdel;$catalg;/d
w
q
!
					temp=$?
					if [ $temp != "0" ]
					then 
                  echo "DELETE (S) Error removing alias entry: $alias_name"
                  echo "           from File_Map: $FILEMAP"
						exit 255		
               else
                  echo "DELETE (I) ALIAS: $aliasdel CATALOG: $catalg "
					fi
				fi
			fi
		done < $EBMTMPDIR/del.$$
		rm $EBMTMPDIR/del.$$
	done
else
   # The ALIAS parameter was not specified
	if [ $gdgroup != null ]
	then
      # The GDG parameter was specified
		for i in $multi
		do
			gdgpath=`ftval $i $catalg 4`
			temp=$?
			gdgpath=`eval echo $gdgpath`
			if [ $temp -ne "0" ]
			then
            echo "DELETE (S) Command failed: ftval $i $catalg 4"
				exit 255
			fi
         gdgpath=`ebmsed $gdgpath`
			type=`ftval $i $catalg 3`  
         if [ $? -ne 0 ]
         then 
            echo "DELETE (S) Command failed: ftval $i $catalg 3"
            exit 255
         fi
			if [ $type  = "FS" ] 
			then
				if [ -f ${gdgpath}.* ]
				then
               echo "         Dataset:  $i   Catalog:  $catalg "
               gdglist=`ls -1 ${gdgpath}.*`
               if [ -f ${gdgpath}_* ]
               then
                  gdglist="${gdglist} `ls -1 ${gdgpath}_*`"
               fi
               for gdginstance in $gdglist
               do
					   $RM  -f $gdginstance
					   temp=$?
					   if [ $temp -eq "2" ]
					   then
					    temp=0
					   fi
                  echo "         $gdginstance                 DELETED"
               done
            else
               echo "         GDG:  $i  Catalog:  $catalg"
               echo "         DELETE (I) GDG - no generations are cataloged"
				fi
			else
            echo "DELETE (S) Invalid file type: $type" 
            echo "           Dataset:  $i   Catalog:  $catalg "
			fi

			if [ $temp != "0" ]
			then
				aa=$temp
			fi
		done
	else
      # Not an ALIAS and not a GDG                                  
	   if [ $novsam != null ]
	   then
         # The NONVSAM parameter was specified
		   for i in $multi
         do
		      ftval3=`ftval $i $catalg 3`
            if [ $? -ne 0 ]
            then
               echo "IDCAMS DELETE: (S) Dataset  <$i>" 

               echo "                   Entry was not found in CATALOG\n"
               exit 255
            fi
		      if [ "a${ftval3}" != "aVS" ] 
		      then
               vsampath=`ftval $i $catalg 4`
			      temp=$?
			      vsampath=`eval echo $vsampath`
			      if [ $temp = "1" ]
			      then
				      exit 1
			      fi
               vsampath=`ebmsed $vsampath`
   
			      if [ -f $vsampath ]
			      then
				      if [ -f ${vsampath}.idx ]
				      then
					      $RM ${vsampath}.idx
					      temp=$?
					      if [ $temp = "2" ]
					      then
						      temp=0
					      fi
                     echo "        Dataset:  $vsampath   Catalog:  $catalg "
                     echo "        ${vsampath}.idx  DELETED"
				      fi
				      $RM $vsampath
				      temp=$?
				      if [ $temp = "2" ]
				      then
					      temp=0
				      fi
                  echo "        Dataset:  $vsampath   Catalog:  $catalg "
                  echo "        $vsampath  DELETED"
			      fi
		       else
		     	   echo "DELETE (E) NONVSAM was specified, $vsampath is VSAM, dataset not deleted"
		       fi

			    if [ $temp != "0" ]
			    then
				    aa=$temp
			    fi
         done
      else
      # The ALIAS and GDG and NONVSAM parameters were not specified
	      if [ $cluste != null ]
         then
            # The CLUSTER parameter was specified
            for i in $multi
            do
               if [ `ftval $i $catalg 3` != "VS" ] 
			      then
                  # Not VSAM file 
					   nvsampath0=`ftval $i $catalg 4`
					   nvsampath=`eval echo $nvsampath0`
                  if [ -f $nvsampath ]
                  then
                     $RM $nvsampath
                     echo "        Dataset:  $i   Catalog:  $catalg "
                     echo "        $nvsampath DELETED"
                  fi
                  if [ -f $nvsampath.idx ]
                  then
                     $RM $nvsampath.idx
                     echo "        Dataset:  $i   Catalog:  $catalg "
                     echo "        $nvsampath.idx DELETED"
                  fi
               else
                  # Dataset is VSAM
                  # echo "1 VSAM dataset *** MAKE ACTION ***"
				      for i in $multi
				      do
					      nvsampath=`ftval $i $catalg 4`
					      temp=$?
					      nvsampath=`eval echo $nvsampath`
					      if [ $temp = "1" ]
					      then
                        echo "IDCAMS (S) DELETE command failed: ftval $i $catalg 4"
						      exit 255
					      fi
					      nvsampath=`ebmsed $nvsampath`
 					      $COBDELETE -i -d$nvsampath
					      temp=$?
					      if [ $temp -ne 0 ]
					      then
						      echo "DELETE (S) Command failed: $COBDELETE -i -d$nvsampath"
						      exit 1
                     else 
                        echo "         Dataset: $nvsampath  Catalog: $catalg   Reinitialized"
					      fi
				      done
			      fi
            done
         else
            # The CLUSTER parameter was not specified
            for i in $multi
            do
               nvsampath=`ftval $i $catalg 4`
               temp=$?
               nvsampath=`eval echo $nvsampath`
               if [ $temp = "1" ]
               then
                  echo "DELETE (S) Command failed:   ftval $i $catalg 4"
                  exit 255
               fi
               nvsampath=`ebmsed $nvsampath`
               type=`ftval $i $catalg 3`
               type=`eval echo $type`
               if [ $type = "VS" ] 
               then
                  $COBDELETE -i -d$nvsampath
                  temp=$?
                  if [ $temp -ne 0 ]
                  then
                     echo "DELETE (S) Command failed:  $COBDELETE -i -d$nvsampath"
                     exit 255
                  else 
                     if [ -f ${JON}_RET ]
                     then
                        cat ${JON}_RET | grep "_${STEPNAME}_ 0$"  > $DEVNULL 2>&1
                        if [ $? = 0 ]
                        then 
                           echo "         Dataset: $nvsampath  Catalog: $catalg   Reinitialized"
                        #else
                           # unikixbld has zero exit status but has set a
                           # non-zero return code and has issued its own
                           # warning message.
                        fi
                     else
                           echo "         Dataset: $nvsampath  Catalog: $catalg   Reinitialized"
                     fi
                  fi
               else
                  if [ $type = "FS" ] 
                  then
                     if [ -f $nvsampath ]
                     then
                        if [ -f ${nvsampath}.idx ]
                        then
                           $RM ${nvsampath}.idx
                           temp=$?
                           if [ $temp = "2" ]
                           then
                              temp=0
                           fi
                           echo "        ${nvsampath}.idx  DELETED"
                        fi
                        $RM $nvsampath
                        temp=$?
                        if [ $temp = "2" ]
                        then
                           temp=0
                        fi
                        echo "        Dataset:  $i   Catalog:  $catalg "
                        echo "        ${nvsampath}  DELETED"
                     else
                        echo "        Dataset : ${i}  Catalog: $catalg" 
                        echo "        File    : ${nvsampath}" 
                        echo " "
                        echo "        -------- File not found --------" 
                        echo " "
                        temp=0
                     fi
                  else
                     if [ $type = "DB" ] 
                     then
                        $RDMDL -f $nvsampath
                        temp=$?
                     else
                        echo "DELETE (S) Invalid file type: $type" 
                        echo "           Dataset:  $i   Catalog:  $catalg "
                        exit 255
                     fi
                  fi
               fi
               if [ $temp != "0" ]
               then
                  aa=$temp
               fi
            done
         fi
      fi
   fi
fi
exit $aa
