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


echo "IDCAMS : start PRINT"
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_dsn_type=null
output=null
types=null
aa=0
temp=0
FROKEY=null
FRONUM=null
LRECL_I1=null
LRECL_O1=null
TOKEY=null
TONUM=null
TOSKIP=null
TOCOUNT=null
FORMAT=DUMP

for i in $1
do
	param=`echo $i | cut -f1 -d')' | cut -f2 -d'('`
	key=`echo $i | cut -f1 -d'('`
	case $key in
INFILE | IFILE | infile | ifile)
	 		aa=$`echo GDG_$param`
			GDG_I1=`eval echo $aa`
			aa=$`echo DSN_$param`
			DSN_I1=`eval echo $aa`
			aa=$`echo DD_$param`
			DD_I1=`eval echo $aa`
			aa=$`echo ${param}_FILETYPE`
			input_dsn_type=`eval echo $aa`
         if [ "a$input_dsn_type" = aTEMP -o "a$input_dsn_type" = atemp ]
         then
            input_dsn_type=FS
         fi
         if [ "a$DSN_I1" = a ]
         then
            echo " "
            echo "PRINT (S) ERROR INFILE parameter requires ddname"
            exit 255
            echo " "
         fi
			aa=$`echo LRECL_$param`
			LRECL_I1=`eval echo $aa`
			export GDG_I1
			export DSN_I1
			export LRECL_I1;;
INDATASET | IDS | indataset | ids)	
			GDG_I1=`gdg_suffix "$i"`
			DSN_I1=$param 
         if [ "a$DSN_I1" = a ]
         then
            echo " "
            echo "PRINT (S) ERROR INDATASET requires dataset name"
            exit 255
            echo " "
         fi
			LRECL_I1=null
			export GDG_I1
			export DSN_I1
			export LRECL_I1
         ftval $DSN_I1 $JOB_CAT 4 > $DEVNULL 2>&1
         stat=$?
         if [ $stat = 0 ]
         then
            DD_I1=`ftval $DSN_I1 $JOB_CAT 4`
         else
            #------------------------------------------------------------
            # input dataset not found in File_Map, determine here if      
            # it is a temporary file
            #------------------------------------------------------------
            echo $DSN_I1 | grep "^&&" > $DEVNULL 2>&1
            stat=$?
            if [ $stat -eq 0 ]
            then
               #------------------------------------------------------------
               # INDATASET is a temporary file, generate filename and assign
               #------------------------------------------------------------
               tmpdsn=`echo $DSN_I1| cut -f3 -d'&'`
               DD_I1=${EBMTMPDIR}/${JOBNAME}.${tmpdsn}.${EBMSYS}.${JON}
               DSN_I1_orig=$DSN_I1
               DSN_I1=${JOBNAME}_${tmpdsn}
               input_dsn_type=FS
            fi
         fi
      ;;

FROMKEY |FKEY |fromkey |fkey)
			FROKEY=$param
			export FROKEY;;
FROMADDRESS| FADDR |fromaddress |faddr)
				;;
FROMNUMBER |FNUM |fromnumber |fnum)
		 	FRONUM=$param
			export FRONUM;;
TOKEY |tokey)		TOKEY=$param
			export TOKEY;;
TOADDRESS| TADDR |toaddress |taddr)
				;;
TONUMBER |TNUM |tonumber |tnum)
			TONUM=$param
			export TONUM;;
SKIP |skip)		TOSKIP=$param
			export TOSKIP;;
COUNT |count)		TOCOUNT=$param
			export TOCOUNT;;
CHARACTER | CHAR |character |char) 
			FORMAT=CHAR
			export FORMAT;;
DUMP | dump) 		FORMAT=DUMP
			export FORMAT;;
HEX | hex) 		FORMAT=HEX
			export FORMAT;;
OUTFILE|OFILE |outfile |ofile)
		 	aa=$`echo GDG_$param`
			GDG_O1=`eval echo $aa`
			aa=$`echo DSN_$param`
			DSN_O1=`eval echo $aa`
			aa=$`echo DD_$param`
			DD_O1=`eval echo $aa`
         if [ "a$DSN_O1" = a ]
         then
            echo " "
            echo "PRINT (S) ERROR OUTFILE parameter requires ddname"
            exit 255
            echo " "
         fi
			aa=$`echo LRECL_$param`
			LRECL_O1=`eval echo $aa`
			export GDG_O1
			export DSN_O1
			export LRECL_O1;;
	*)
		echo "PRINT (W) WARNING:  $key parameter not supported"	;;
	esac
done

#------------------------------------------------------------------------------
# Evaluate input  
#------------------------------------------------------------------------------
# Determine input file type:
# If INFILE was specified, file type should already be resolved
# If INDATASET, file type may not have yet been determined
# If INDATASET is a temporary file, it has already been set
# to FS.
#------------------------------------------------------------------------------
if [ "a$input_dsn_type" = a -o "a$input_dsn_type" = anull ]
then
   input_dsn_type=`ftval $DSN_I1 $JOB_CAT 3` 
   stat=$?
   if [ $stat != 0 ]
   then
     echo " "
     echo "PRINT (S) ERROR:  dataset not found in File_Map"
     echo "Dataset:   $DSN_I1"
     echo " "
	  exit 1
   fi 
fi

if [ "a$input_dsn_type" = aVS ]
then
   echo " "
   echo "PRINT (W) VSAM input dataset is not supported in the current version"
   echo " "
   exit 0
else
   if [ "a$input_dsn_type" != aFS ]
   then
      echo "PRINT (S) ERROR: Invalid input file type:   $input_dsn_type"
   fi
fi
if [ "a$DD_I1" = a ]
then
   #---------------------------------------------------------------------------
   # DD_<ddname> not resolved, must retrieve file name from File_Map
   #---------------------------------------------------------------------------
	DD_I1=`ftval $DSN_I1 $JOB_CAT 4` 
   stat=$?
   if [ $stat != 0 ]
   then
     echo " "
     echo "PRINT (S) ERROR:  dataset not found in File_Map"
     echo "Dataset:   $DSN_I1"
     echo " "
	  exit 1
   fi 
fi
			
if [ "a$GDG_I1" != a ]
then
   if [ $GDG_I1 = null ] 
   then
      if [ $input_dsn_type = "FS" ]
      then
   	   CMD_I1="-inf $DD_I1"
      else
		   CMD_I1="-ind $DD_I1"
      fi
   else
	   if [ $types = "FS" ] 
	   then
	   	CMD_I1="-inf $DD_I1"
	   else
         rootgdg=$DD_I1
	   	DD_I1=${rootgdg}$GDG_I1
	   	CMD_I1="-ind $DD_I1"
	   fi
   fi
fi

if [ "a$LRECL_I1" != a ]
then
   if [ $LRECL_I1 != null ]
   then
	   IRS_I1="-irs $LRECL_I1"
   else
	   IRS_I1=""
   fi
fi
#------------------------------------------------------------------------------
# Evaluate output 
#------------------------------------------------------------------------------
#O1
output=FS

if test $DSN_O1
then
	temp=$?
else
   DSN_O1=`eval echo $DD_SYSPRINT`
   DD_O1=`eval echo $DD_SYSPRINT`
fi

if [ "a$DD_O1" = a ]
then
   # check if there is an entry in the File_Map
   ftval $DSN_O1 $JOB_CAT 3 > $DEVNULL 2>&1
   if [ $? -ne 0 ]
   then 
      #  if outfile is a sysout file or a temporary file, 
      #  then an entry in File_Map is not required
      echo $DSN_O1 | grep ^$SYSOUTDIR 
      if [ $stat != 0 ]
      then
         echo $DSN_O1 | grep ^$EBMTMPDIR 
         if [ $stat != 0 ]
         then
            DD_O1=`ftval $DSN_O1 $JOB_CAT 4` 
            temp=$?
            if [ $temp != "0" ]
            then
               echo "PRINT (S) ERROR:  dataset not found in File_Map"
               echo "Dataset:   $DSN_O1"
	            exit 1
            fi
         fi
      fi 
   fi
fi

CMD_O1="-ouf $DD_O1"


if [ "a$LRECL_O1" != a ]
then
   if [ $LRECL_O1 != null ]
   then
	   ORS_O1="-ors $LRECL_O1"
   else
	   ORS_O1=""
   fi
fi


if [ $IDEBUG = "yes" ] 
then
	RDMCP="echo rdmcp"
	CP="echo cp"
else
	RDMCP=rdmcp
	CP=cp
fi

if [ $input_dsn_type = "FS" ]
then
   if [ $FRONUM != null -a $TONUM != null ]
   then
		FROTONUM=\'\'\($FRONUM-$TONUM\)\'\'
      $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
		temp=$?
		if [ $temp != "0" ]
		then
			rm /tmp/temp.$$  2>$DEVNULL 
			exit 1
		fi
		if [ $FORMAT = "CHAR" ]  
		then
			if [ $IDEBUG = "yes" ] 
			then
				echo " od -c /tmp/temp.$$ > $DD_O1 "
			else
				od -c /tmp/temp.$$ > $DD_O1
				temp=$?
			fi
		else
         if [ $FORMAT = "HEX" ]  
         then
            if [ $IDEBUG = "yes" ] 
			   then
               echo " od -cx /tmp/temp.$$ > $DD_O1 "
			   else
               od -cx /tmp/temp.$$ > $DD_O1
				   temp=$?
            fi
         else
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -cx /tmp/temp.$$ > $DD_O1 "
            else
               od -cx /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         fi
      fi
   else
    	if [ $TONUM != null -a $TOSKIP != null ]
 	   then
         FROTONUM=\'\'\($FRONUM-$TONUM\)\'\'
 	      $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
         temp=$?
         if [ $temp != "0" ]
         then
            rm /tmp/temp.$$  2>$DEVNULL
            exit 1
         fi
         if [ $FORMAT = "CHAR" ]  
         then
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -c /tmp/temp.$$ > $DD_O1 "
            else
               od -c /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         else
            if [ $FORMAT = "HEX" ]  
            then
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -cx /tmp/temp.$$ > $DD_O1 "
               else
                  od -cx /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            else
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -cx /tmp/temp.$$ > $DD_O1 "
               else
                  od -cx /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            fi
         fi
      else
         if [ $TONUM != null ]
         then
            FROTONUM=\'\'\(1-$TONUM\)\'\'
            $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
            temp=$?
            if [ $temp != "0" ]
            then
               rm /tmp/temp.$$  2>$DEVNULL
               exit 1
            fi
            if [ $FORMAT = "CHAR" ]  
            then
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -c /tmp/temp.$$ > $DD_O1 "
               else
                  od -c /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            else
               if [ $FORMAT = "HEX" ]  
               then
                  if [ $IDEBUG = "yes" ] 
                  then
                     echo " od -cx /tmp/temp.$$ > $DD_O1 "
                  else
                     od -cx /tmp/temp.$$ > $DD_O1
                     temp=$?
                  fi
               else
                  if [ $IDEBUG = "yes" ] 
                  then
                     echo " od -cx /tmp/temp.$$ > $DD_O1 "
                  else
                     od -cx /tmp/temp.$$ > $DD_O1
                     temp=$?
                  fi
               fi
            fi
         else
            if [ $FRONUM != null -a $TOCOUNT != null ]
            then
               AUX=`expr $FRONUM + $TOCOUNT`
               FROTONUM=\'\'\($FRONUM-$AUX\)\'\'
               $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
               temp=$?
               if [ $temp != "0" ]
               then
                  rm /tmp/temp.$$  2>$DEVNULL
                  exit 1
               fi
               if [ $FORMAT = "CHAR" ]  
               then
                  if [ $IDEBUG = "yes" ] 
                  then
                     echo " od -c /tmp/temp.$$ > $DD_O1 "
                  else
                     od -c /tmp/temp.$$ > $DD_O1
                     temp=$?
                  fi
               else
                  if [ $FORMAT = "HEX" ]  
                  then
                     if [ $IDEBUG = "yes" ] 
                     then
                        echo " od -cx /tmp/temp.$$ > $DD_O1 "
                     else
                        od -cx /tmp/temp.$$ > $DD_O1
                        temp=$?
                     fi
                  else
                     if [ $IDEBUG = "yes" ] 
                     then
                        echo " od -cx /tmp/temp.$$ > $DD_O1 "
                     else
                        od -cx /tmp/temp.$$ > $DD_O1
                        temp=$?
                     fi
                  fi
               fi
            else
               if [ $FRONUM != null ]
               then
                  FROTONUM=\'\'\($FRONUM-end\)\'\'
                  $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
                  temp=$?
                  if [ $temp != "0" ]
                  then
                     rm /tmp/temp.$$  2>$DEVNULL
                     exit 1
                  fi
                  if [ $FORMAT = "CHAR" ]  
                  then
                     if [ $IDEBUG = "yes" ] 
                     then
                        echo " od -c /tmp/temp.$$ > $DD_O1 "
                     else
                        od -c /tmp/temp.$$ > $DD_O1
                        temp=$?
                     fi
                  else
                     if [ $FORMAT = "HEX" ]  
                     then
                        if [ $IDEBUG = "yes" ] 
                        then
                           echo " od -cx /tmp/temp.$$ > $DD_O1 "
                        else
                           od -cx /tmp/temp.$$ > $DD_O1
                           temp=$?
                        fi
                     else
                        if [ $IDEBUG = "yes" ] 
                        then
                           echo " od -cx /tmp/temp.$$ > $DD_O1 "
                        else
                           od -cx /tmp/temp.$$ > $DD_O1
                           temp=$?
                        fi
                     fi
                  fi
               else
                  if [ $TOCOUNT != null -a $TOSKIP != null ]
                  then
                     AUX=`expr $TOSKIP + $TOCOUNT`
                     FROTONUM=\'\'\($TOSKIP-$AUX\)\'\'
                     $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
                     temp=$?
                     if [ $temp != "0" ]
                     then
                        rm /tmp/temp.$$  2>$DEVNULL
				            exit 1
			            fi
                     if [ $FORMAT = "CHAR" ]  
                     then
                        if [ $IDEBUG = "yes" ] 
                        then
                           echo " od -c /tmp/temp.$$ > $DD_O1 "
                        else
                           od -c /tmp/temp.$$ > $DD_O1
                           temp=$?
                        fi
                     else
                        if [ $FORMAT = "HEX" ]  
                        then
                           if [ $IDEBUG = "yes" ] 
                           then
                              echo " od -cx /tmp/temp.$$ > $DD_O1 "
                           else
                              od -cx /tmp/temp.$$ > $DD_O1
                              temp=$?
                           fi
                        else
                           if [ $IDEBUG = "yes" ] 
                           then
                              echo " od -cx /tmp/temp.$$ > $DD_O1 "
                           else
                              od -cx /tmp/temp.$$ > $DD_O1
                              temp=$?
                           fi
                        fi
                     fi
                  else
                     if [ $TOCOUNT != null ]
                     then
                        FROTONUM=\'\'\(1-$TOCOUNT\)\'\'
                        $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
                        temp=$?
                        if [ $temp != "0" ]
                        then
                           rm /tmp/temp.$$  2>$DEVNULL
                           exit 1
                        fi
                        if [ $FORMAT = "CHAR" ]  
                        then
                           if [ $IDEBUG = "yes" ] 
                           then
                              echo " od -c /tmp/temp.$$ > $DD_O1 "
                           else
                              od -c /tmp/temp.$$ > $DD_O1
                              temp=$?
                           fi
                        else
                           if [ $FORMAT = "HEX" ]  
                           then
                              if [ $IDEBUG = "yes" ] 
                              then
                                 echo " od -cx /tmp/temp.$$ > $DD_O1 "
                              else
                                 od -cx /tmp/temp.$$ > $DD_O1
                                 temp=$?
                              fi
                           else
                              if [ $IDEBUG = "yes" ] 
                              then
                                 echo " od -cx /tmp/temp.$$ > $DD_O1 "
                              else
                                 od -cx /tmp/temp.$$ > $DD_O1
                                 temp=$?
                              fi
                           fi
                        fi
                     else
                        if [ $TOSKIP != null ]
                        then
                           FROTONUM=\'\'\($TOSKIP-end\)\'\'
                           $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -rg $FROTONUM $IRS_I1 $ORS_O1
                           temp=$?
                           if [ $temp != "0" ]
                           then
                              rm /tmp/temp.$$  2>$DEVNULL
                              exit 1
                           fi
                           if [ $FORMAT = "CHAR" ]  
                           then
                              od -c /tmp/temp.$$ > $DD_O1
                              temp=$?
                           else
                              if [ $FORMAT = "HEX" ]  
                              then
                                 if [ $IDEBUG = "yes" ] 
                                 then
                                    echo " od -x /tmp/temp.$$ > $DD_O1 "
                                 else
                                    od -cx /tmp/temp.$$ > $DD_O1
                                    temp=$?
                                 fi
                              else
                                 if [ $IDEBUG = "yes" ] 
                                 then
                                    echo " od -cx /tmp/temp.$$ > $DD_O1 "
                                 else
                                    od -cx /tmp/temp.$$ > $DD_O1
                                    temp=$?
                                 fi
                              fi
                           fi
                        else
                           $CP $DD_I1 /tmp/temp.$$
                           temp=$?
                           if [ $temp != "0" ]
                           then
                              rm /tmp/temp.$$  2>$DEVNULL
                              exit 1
                           fi
                           if [ $FORMAT = "CHAR" ]  
                           then
                              if [ $IDEBUG = "yes" ] 
                              then
                                 echo " od -c /tmp/temp.$$ > $DD_O1 "
                              else
                                 od -c /tmp/temp.$$ > $DD_O1
                                 temp=$?
                              fi
                           else
                              if [ $FORMAT = "HEX" ]  
                              then
                                 if [ $IDEBUG = "yes" ] 
                                 then
                                    echo " od -cx /tmp/temp.$$ > $DD_O1 "
                                 else
                                    od -cx /tmp/temp.$$ > $DD_O1
                                    temp=$?
                                 fi
                              else
                                 if [ $IDEBUG = "yes" ] 
                                 then
                                    echo " od -cx /tmp/temp.$$ > $DD_O1 "
                                 else
                                    od -cx /tmp/temp.$$ > $DD_O1
                                    temp=$?
                                 fi
                              fi
                           fi
                        fi
                     fi
                  fi
               fi
            fi
         fi
      fi
   fi
else
   # input_dsn_type not FS
   if [ $FROKEY != null -a $TOKEY != null ]
   then
      FROTOKEY=\'\'\(\"$FROKEY\"\,\"$TOKEY\"\)\'\'
      $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -kr $FROTOKEY $ORS_O1
      temp=$?
      if [ $temp != "0" ]
      then
         rm /tmp/temp.$$  2>$DEVNULL
         exit 1
      fi
      if [ $FORMAT = "CHAR" ]  
      then
         if [ $IDEBUG = "yes" ] 
         then
            echo " od -c /tmp/temp.$$ > $DD_O1 "
         else
            od -c /tmp/temp.$$ > $DD_O1
            temp=$?
         fi
      else
         if [ $FORMAT = "HEX" ]  
         then
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -x /tmp/temp.$$ > $DD_O1 "
            else
               od -x /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         else
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -cx /tmp/temp.$$ > $DD_O1 "
            else
               od -cx /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         fi
      fi
   else
      if [ $FROKEY != null ]
      then
         FROTOKEY=\'\'\(\"$FROKEY\"\,\)\'\'
         $RDMCP $CMD_I1 -ouf /tmp/temp.$$ -kr $FROTOKEY $ORS_O1
         temp=$?
         if [ $temp != "0" ]
         then
            rm /tmp/temp.$$  2>$DEVNULL
            exit 1
         fi
         if [ $FORMAT = "CHAR" ]  
         then
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -c /tmp/temp.$$ > $DD_O1 "
            else
               od -c /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         else
            if [ $FORMAT = "HEX" ]  
            then
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -x /tmp/temp.$$ > $DD_O1 "
               else
                  od -x /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            else
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -cx /tmp/temp.$$ > $DD_O1 "
               else
                  od -cx /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            fi
         fi
      else
         $RDMCP $CMD_I1 -ouf /tmp/temp.$$ $ORS_O1
         temp=$?
         if [ $temp != "0" ]
         then
          rm /tmp/temp.$$  2>$DEVNULL
          exit 1
         fi
         if [ $FORMAT = "CHAR" ]  
         then
            if [ $IDEBUG = "yes" ] 
            then
               echo " od -c /tmp/temp.$$ > $DD_O1 "
            else
               od -c /tmp/temp.$$ > $DD_O1
               temp=$?
            fi
         else
            if [ $FORMAT = "HEX" ]  
            then
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -x /tmp/temp.$$ > $DD_O1 "
               else
                  od -x /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            else
               if [ $IDEBUG = "yes" ] 
               then
                  echo " od -cx /tmp/temp.$$ > $DD_O1 "
               else
                  od -cx /tmp/temp.$$ > $DD_O1
                  temp=$?
               fi
            fi
         fi
      fi
   fi
fi
if [ $IDEBUG != "yes" ] 
then
   rm /tmp/temp.$$  2>$DEVNULL
fi
exit $temp
