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



echo "IDCAMS : start DEFINE"
arguments_0=`echo "$1" | sed -e's/(NAME (/(NAME(/'g`
arguments_1=`echo "$arguments_0" | sed -e's/( NAME(/(NAME(/'g`
arguments_2=`echo "$arguments_1" | sed -e's/( NAME (/(NAME(/'g`
arguments=`echo "$arguments_2" | sed -e's/(NAME( /(NAME(/'g`
if [ $IDEBUG = "yes" ] 
then
cont=0
	echo "$0  starts with :"
	for i in $arguments
	do
	cont=`expr $cont + 1`
	if [ $cont = "2" ] 
	then
  		param=`echo $i | cut -f1 -d')' | cut -f3 -d'('`
		key=`echo $i | cut -f2 -d'('`
	else
  		param=`echo $i | cut -f1 -d')' | cut -f2 -d'('`
		key=`echo $i | cut -f1 -d'('`
	fi
		echo "                     key=$key     param=$param"
	done
fi

file=null
catalg=null
type=null
path=null
ddspath=null
gdg=null

typedef=null
alias=null
relateto=null
catal=null

nvsamm=null
nvsname=null
nvscatal=null
cluste=null
clname=null
clcatal=null

temp=0
nvstype=null
nvspath=null
nvsddspath=null
cltype=null
clpath=null
clddspath=null

aliasto=null
cont=0
define_exit_status=0
temp=0
swich=0
for i in $arguments
do
	cont=`expr $cont + 1`
	if [ $cont = "2" ] 
	then
  		param=`echo $i | cut -f1 -d')' | cut -f3 -d'('`
		key=`echo $i | cut -f2 -d'('`
	else
		param=`echo $i | cut -f1 -d')' | cut -f2 -d'('`
		key=`echo $i | cut -f1 -d'('`
	fi

   if [ $key ]
   then
      if [ $key = "ALIAS"  -o  $key = "alias" ]
      then
	      swich=1
      else
         if [ $key = "NONVSAM" -o  $key = "nonvsam" -o $key = "NVSAM" -o $key = "nvsam" ]
         then
	         swich=2
         else
            if [ $key = "CLUSTER" -o  $key = "cluster" -o $key = "CL" -o $key = "cl" ]
            then
	            swich=3
            fi
         fi
      fi
   fi


	if [ $swich = "1" ]
	then
		case $key in

		ALIAS|alias)               aliasto=$key;;
		NAME|name)                 alias=$param;;
		RELATE|REL|relate|rel)		relateto=$param;;
		CATALOG|CAT|catalog|cat)	catal=$param;;
      *)
         if [ $key ]
         then
            if [ $key = "NONVSAM" -o  $key = "nonvsam" -o $key = "NVSAM" -o $key = "nvsam" ]
           then
              swich=2
           else
              if [ $key = "CLUSTER" -o  $key = "cluster" -o $key = "CL" -o $key = "cl" ]
              then
                 swich=3
              else
                 swich=0
              fi
           fi
        fi
     ;;
     esac
	fi

	if [ $swich = "2" ]
	then
      case $key in

		NONVSAM|nonvsam|NVSAM|nvsam)		nvsamm=$key;;
		NAME|name)			            	nvsname=$param;;
		CATALOG|CAT|catalog|cat)	   	nvscatal=$param;;
		DEVICETYPES|devicetypes|DEVT|devt|\
		VOLUMES|volumes|VOL|vol|\
		FILESEQUENCENUMBERS|filesequencenumbers|FSEQN|fseqn|\
		OWNER|owner|TO|to|FOR|for)
		if [ $IDEBUG = "yes" ] 
		then
			echo "IDCAMS : DEFINE NONVSAM $key command ignored"
		fi
		;;

      *)

         if [ $key ]
         then
            if [ $key = "ALIAS"  -o  $key = "alias" ]
            then
               swich=1
            else
               if [ $key = "CLUSTER" -o  $key = "cluster" -o $key = "CL" -o $key = "cl" ]
               then
                  swich=3
               else
                  swich=0
               fi
            fi
         fi
         ;;
			esac
	fi #swich =2

	if [ $swich -eq 3 ]
	then
      case $key in
         CLUSTER|cluster|CL|cl)				cluste=$key ;;
         NAME|name)					clname=$param;;
         CATALOG|CAT|catalog|cat)			clcatal=$param;;
         CYLINDERS|cylinders|CYL|cyl|RECORDS|records|REC|rec|\
         TRACKS|tracks|TRK|trk|VOLUMES|volumes|VOL|vol|\
         ATTEMPTS|attempts|ATT|att|AUTHORIZATION|authirization|AUTH|auth|\
         BUFFERSPACE|bufferspace|BUFSP|bufsp|BUFSPC|bufspc|\
         CODE|code|CONTROLINTERVALSIZE|controlintervalsize|CISZ|\
         cisz|CNVSZ|cnvsz|CONTROLPW|controlpw|CTLPW|ctlpw|\
         DESTAGEWAIT|destagewait|DSTGW|dstgw|\
         NODESTAGEWAIT|nodestagewait|NDSTGW|ndstgw|ERASE|erase|\
         ERAS|eras|NOERASE|noerase|neras|NERAS|EXCEPTIONEXIT|exceptionexit|\
         EEXT|eext|FILE|file|FREESPACE|freespace|FSPC|fspc|IMBED|imbed|\
         IMBD|imbd|NOIMBED|noimbed|NIMBD|nimbd|INDEXED|indexed|IXD|ixd|\
         NONINDEXED|nonindexed|NIXD|nixd|NUMBERED|numbered|NUMD|numd|\
         KEYRANGES|keyranges|KRNG|krng|KEYS|keys|MASTERPW|masterpw|MRPW|\
         mrpw|MODEL|model|ORDERED|ordered|ORD|ord|UNORDERED|unordered|\
         UNORD|unord|OWNER|owner|READPW|readpw|RDPW|rdpw|RECORDSIZE|\
         recorsize|RECSZ|recsz|REPLICATE|replicate|REPL|repl|NOREPLICATE|\
         noreplicate|NREPL|nrepl|REUSE|reuse|RUS|rus|NOREUSE|noreuse|NRUS|\
         nrus|SHAREOPTIONS|shareoptions|SHR|shr|SPANNED|spanned|SPND|spnd|\
         NONSPANNED|nonspanned|NSPND|nspnd|SPEED|speed|RECOVERY|recovery|\
         RCVY|rcvy|STAGE|stage|BIND|bind|CYLINDERFAULT|cylinderfault|CYLF|\
         cylf|TO|to|FOR|for|UNIQUE|unique|UNQ|unq|SUBALLOCATION|suballocation|\
         SUBAL|subal|UPDATEPW|updatepw|UPDPW|updpw|WRITECHECK|writecheck|WCK|\
         wck|NOWRITECHECK|nowritecheck|NWCK|nwck|DATA|INDEX|data|index)
         if [ $IDEBUG = "yes" ] 
         then
            echo "IDCAMS : DEFINE CLUSTER $key command ignored"
         fi
		;;

      *)
         if [ $key ]
         then

            if [ $key = "ALIAS"  -o  $key = "alias" ]
            then
	            swich=1
            else
               if [ $key = "NONVSAM" -o  $key = "nonvsam" -o $key = "NVSAM" -o $key = "nvsam" ]
               then
	               swich=2
               else
	               swich=0
               fi
            fi
         fi
		;;
		esac
   fi

	if [ $swich = "0" ]
	then
      case $key in

		ALTERNATEINDEX|alternateindex|AIX|aix)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		GENERATIONDATAGROUP|generationdatagroup|GDG|gdg)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		PAGESPACE|pagespace|PGSPC|pgspc)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		PATH|path)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		SPACE|space|SPC|spc)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		USERCATALOG|usercatalog|UCAT|ucat)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
		MASTERCATALOG|mastercatalog|MCAT|mcat)
			cont=1
			if [ $IDEBUG = "yes" ] 
			then
				echo "IDCAMS : DEFINE $key command ignored"
			fi ;;
			*)
		   ;;
		esac
	fi
done

if [ $aliasto != null ]
then
	if [ $alias = null -o $relateto = null ] 
	then
		echo "IDCAMS : DEFINE ($alias - $relateto) is redundant"
		exit 2
	fi
	if [ $catal = null ] 
	then
		catal=`ebmsed $JOB_CAT`
	fi
else
   if [ $nvsamm != null ]
   then
	   if [ $nvsname = null ]
	   then
		   echo " IDCAMS : DEFINE NONVSAM name missing "
		   exit 2
	   fi
	   if [ $nvscatal = null ] 
	   then
		   nvscatal=`ebmsed $JOB_CAT`
	   fi
   else
      if [ $cluste != null ]
      then
	      if [ $clname = null ]
	      then
		      echo " IDCAMS : DEFINE CLUSTER name missing "
		      exit 2
	      fi
	      if [ $clcatal = null ] 
	      then
		      clcatal=`ebmsed $JOB_CAT`
   	   fi
      fi
   fi
fi


if [ $aliasto != null ]
then
   # DEFINE an ALIAS
	file=`ebmsed $alias`
	catalg=`ftval $relateto $catal 2` 
	temp=$?
	if [ $temp != "0" ]
	then
		exit 1
	fi
   catalg=`ebmsed $catalg`
	type=`ftval $relateto $catal 3` 
	temp=$?
	if [ $temp != "0" ]
	then
		exit 1
	fi
	path=`ftval $relateto $catal 4` 
	temp=$?
	if [ $temp != "0" ]
	then
		exit 1
	fi
   path=`ebmsed $path`

	if [ $type = "DB" ]
	then
	   ddspath=`ftval $relateto $catal 5` 
		temp=$?
		if [ $temp != "0" ]
		then
			exit 1
		fi
	fi
	gdg=`ftval $relateto $catal 6` 
	temp=$?
	if [ $temp != "0" ]
	then
		exit 1
   fi
   if [ $IDEBUG = "yes" ] 
   then
      echo "new alias is: $file $catalg $type $path $ddspath $gdg"
      echo "new alias is: $file $catalg $type $path $ddspath $gdg"
   else
      if [ $type = "DB" ]
      then
         echo "$file;$catalg;$type;$path;$ddspath;$gdg;" > /tmp/define.$$
         cat /tmp/define.$$ >> $FILEMAP
         define_exit_status=$? 
         rm -f /tmp/define.$$
      else
         if [ "$ddspath" = "null" ]
         then
            ddspath='' 
         fi
         entry="$file;$catalg;$type;$path;$ddspath;$gdg"
         # check if the alias entry already exists, do not add a duplicate
         grep "$entry" $FILEMAP >$DEVNULL 2>&1
         if [ $? -ne 0 ]
         then
            echo "$file;$catalg;$type;$path;;$gdg;" > /tmp/define.$$
            cat /tmp/define.$$ >> $FILEMAP
            define_exit_status=$?
            if [ $define_exit_status -eq 0 ]
            then
               echo "DEFINE (I) ALIAS: $file   CATALOG: $catalg"
               echo "           $entry  Entry added"  
            fi
         else
            echo "DEFINE (I) Entry exists for ALIAS: $file: CATALOG: $catalg"
            echo " "
            echo "           $file;$catalg;$type;$path;$ddspath;$gdg"
            echo " "
         fi
         rm -f /tmp/define.$$
      fi
   fi
else # not an ALIAS
   if [ $nvsamm != null ]
   then
	   nvstype=`ftval $nvsname $nvscatal 3`
	   temp=$?
	   if [ $temp -eq 1 ]
	   then
		   exit 1
	   fi
	   if [ $nvstype = "VS" ]
     	then
         echo "DEFINE (W) $nvsname is a VSAM dataset: command ignored "
      else
         if [ $nvstype = "FS" ]
         then
            nvspath=`ftval $nvsname $nvscatal 4`
            temp=$?
            if [ $temp -eq 1 ]
            then
               exit 1
            fi
            touch `eval ebmsed $nvspath`
         else
            if [ $nvstype = "DB" ]
            then
               nvspath=`ftval $nvsname $nvscatal 4`
               temp=$?
               if [ $temp -eq 1 ]
               then
                  exit 1
               fi
               nvsddspath=`ftval $nvsname $nvscatal 5`
               temp=$?
               if [ $temp -eq 1 ]
               then
                  exit 1
               fi
               if [ $IDEBUG = "yes" ] 
               then
                  echo "rdmcr -f $nvspath  -s $nvsddspath "
               else
                  rdmcr -f $nvspath  -s $nvsddspath
                  define_exit_status=$?
               fi
            fi
         fi
      fi
   else # not an alias AND nvsamm is 'null'
      if [ $cluste != null ]
      then
         cltype=`ftval $clname $clcatal 3`
         temp=$?
         if [ $temp -eq 1 ]
         then
            exit 1
         fi
         if [ $cltype = "VS" ]
         then
            echo "DEFINE (I) $clname is a VSAM dataset: command ignored "
         else
            if [ $cltype = "FS" ]
            then
               clpath=`ftval $clname $clcatal 4`
               temp=$?
               if [ $temp -eq 1 ]
               then
                  exit 1
               fi
               touch `eval ebmsed $clpath`  
            else
               if [ $cltype = "DB" ]
               then
                  clpath=`ftval $clname $clcatal 4`
                  temp=$?
                  if [ $temp -eq 1 ]
                  then
                     exit 1
                  fi
                  clddspath=`ftval $clname $clcatal 5`
                  temp=$?
                  if [ $temp -eq 1 ]
                  then
                     exit 1
                  fi
                  if [ $IDEBUG = "yes" ] 
                  then
                     echo "rdmcr -f $clpath  -s $clddspath "
                  else
                     rdmcr -f $clpath  -s $clddspath
                     define_exit_status=$?
                  fi

               fi
            fi
         fi
      fi
   fi
fi
exit $define_exit_status
