#!/bin/sh
validate=0
DD_SORTIN=`ebmsed $DD_SORTIN`
DD_SORTOUT=`ebmsed $DD_SORTOUT`
DD_SYSIN=`ebmsed $DD_SYSIN`
if [ $VALIDATE = 'y' ]
then
	validate=1
	echo PGM=`basename $0` in validation mode is not executed:
	if [ "a$DD_SORTIN" = "a" ]
	then
		echo DD_SORTIN is not set
	else
		echo SORTIN is $DD_SORTIN
	fi
	if [ "a$DD_SORTOUT" = "a" ]
	then
		echo DD_SORTOUT is not set
	else
		echo SORTOUT is $DD_SORTOUT
	fi
	if [ "a$DD_SYSIN" = "a" ]
	then
		echo DD_SYSIN is not set
	else
		echo SYSIN is $DD_SYSIN
	fi
	if [ "a$LRECL_SORTIN" = "a" ]
	then
		echo LRECL_SORTIN is not set
	else
		echo LRECL_SORTIN is $LRECL_SORTIN
	fi
	if [ "a$LRECL_SORTOUT" = "a" ]
	then
		echo LRECL_SORTOUT is not set
	else
		echo LRECL_SORTOUT is $LRECL_SORTOUT
	fi
fi
if [ -z "$SYNCSORT_EXEC" ]
then
  SYNCSORT_EXEC=syncsort
fi
if [ "a$TMPSORT" = "a" ]
then
#if the temporary directory is not specified the /tmp is assumed
	TMPSORT=/tmp
fi
if [ "a$RECFOR_SORTIN" = "a" ]
then
#if the SORTIN record format is not specified the dafault type is record
	RECFOR_SORTIN=record
fi
if [ "a$RECFOR_SORTOUT" = "a" ]
then
#if the SORTOUT record format is not specified the default type is record
	RECFOR_SORTOUT=record
fi
if [ "a$SORT_MODE" = "a" ]
then
#if the SORT_MODE is not specified sortx is assumed as default
	SORT_MODE=sortx
fi
if [ "a$SORTX" = "a" ]
then
#if the SORTX variable is not specified the MTP sortx executable is used
#to access VSAM files, and the batch sortx executable is used to access FS files.
	SORT_VS=$UNIKIX/bin/sortx
	SORT_FS=`takepath pack`/bin/sortx
else
	SORT_FS=$SORTX
	SORT_VS=$SORTX
fi
if [ "a$LRECL_SORTIN" = "a" ]
then
#if LRECL for SORTIN is not found the LRECL for SORTOUT is assumed
	LRECL_SORTIN=$LRECL_SORTOUT
fi
if [ "a$SORTIN_TYPE" = "a" ]
then
#if the SORTIN type is not specified the FS type for SORTIN is assumed
	SORTIN_TYPE=FS;export SORTIN_TYPE
fi
if [ "a$SORTOUT_TYPE" = "a" ]
then
#if the SORTOUT type is not specified the FS type for SORTOUT is assumed
  SORTOUT_TYPE=FS;export SORTOUT_TYPE
fi

#if SORTIN is VS {
if [ "$SORTIN_TYPE" = "VS" ]
then
#if SORTIN is VS type
  if [ ! -f $DD_SYSIN ]
  then
#if SYSIN does not exist
	if [ $validate -ne 0 ]
	then
	  echo $DD_SYSIN does not exist
	  echo 0 > status.${JON}
	  exit 0
	else
	  echo $DD_SYSIN does not exist
	  echo 2 > status.${JON}
	  exit 2
	fi
  fi
  if [ -s $DD_SYSIN ]
  then
#if sysin exists and it is not empty
        execute=`sed -n '1 s/^#!.*/Execute_sh/p' $DD_SYSIN`
        if [ "a$execute" = "aExecute_sh" ]
        then
#if sysin is a shell to be executed
	  if [ $validate -ne 0 ]
	  then
	    echo "sh $DD_SYSIN"
	    echo 0 > status.${JON}
	    exit 0
	  else
	    sh $DD_SYSIN
	    sort_status=$?
	    echo $sort_status > status.${JON}
	  exit $sort_status
	  fi
        else
#if sysin is not a shell to be executed

# { Begin Bug 3637
       case $SORT_MODE in
       syncsort | SYNCSORT)
       #
       # SYNCSORT:
          if [ $validate -ne 0 ]
          then
             aa=`cat $DD_SYSIN`
             echo $SYNCSORT_EXEC `eval echo $aa`
             echo 0 > status.${JON}
             exit 0
          else
             aa=`cat $DD_SYSIN`
             $SYNCSORT_EXEC `eval echo $aa`
             sort_status=$?
             echo $sort_status > status.${JON}
             exit $sort_status
          fi
       ;;

       cosort | COSORT)
        #
        # COSORT:
        #
          if [ $validate -ne 0 ]
          then
              echo "Warning: Input file VSAM type not supported in $SORT_MODE"
              echo 0 > status.${JON}
              exit 0
          else
              echo "ERROR: Input file VSAM type not supported in $SORT_MODE"
              echo 255 > status.${JON}
              exit 255
          fi
        ;;

        sortx | SORTX)
            if [ "$SORTOUT_TYPE" = "VS" ]
            then
              if [ $validate -ne 0 ]
              then
                echo "Warning: Output file VSAM type not supported in $SORT_MODE
"
                echo 0 > status.${JON}
                exit 0
              else
                echo "ERROR: Output file VSAM type not supported in $SORT_MODE"
                echo 255 > status.${JON}
                exit 255
              fi
            fi
            if [ $validate -ne 0 ]
            then
              echo $SORT_VS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t VS $SORT_
WK
              echo 0 > status.${JON}
              exit 0
            else
              $SORT_VS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t VS $SORT_WK
              sort_status=$?
              echo $sort_status > status.${JON}
              exit $sort_status
            fi
         ;;

         *)
            if [ $validate -ne 0 ]
            then
               echo invalid SORT_MODE option $SORT_MODE
               echo 0 > status.${JON}
               exit 0
            else
               echo invalid SORT_MODE option $SORT_MODE
               echo 2 > status.${JON}
               exit 2
            fi
            ;;
         esac

        fi

# } End Bug 3637

  else
#if sysin exists and it is empty do a copy
	if [ $validate -ne 0 ]
	then
	  echo $UNIKIX/bin/unikixbld -t s -d $DD_SORTIN -s $DD_SORTOUT -r $RECFOR_SORTOUT
	  echo 0 > status.${JON}
	  exit 0
	else
	  $UNIKIX/bin/unikixbld -t s -d $DD_SORTIN -s $DD_SORTOUT -r $RECFOR_SORTOUT
	  sort_status=$?
	  echo $sort_status > status.${JON}
	  exit $sort_status
	fi
  fi
fi
#end of SORTIN VS type }

#if SORTOUT is VS type {
if [ "$SORTOUT_TYPE" = "VS" ]
then
  if [ ! -f $DD_SYSIN ]
  then
#if SYSIN does not exist
	if [ $validate -ne 0 ]
	then
	  echo $DD_SYSIN does not exist
	  echo 0 > status.${JON}
	  exit 0
	else
	  echo $DD_SYSIN does not exist
	  echo 2 > status.${JON}
	  exit 2
	fi
  fi
  if [ -s $DD_SYSIN ]
  then
#if sysin exists and it is not empty
        execute=`sed -n '1 s/^#!.*/Execute_sh/p' $DD_SYSIN`
        if [ "a$execute" = "aExecute_sh" ]
        then
#if sysin is a shell to be executed
	  if [ $validate -ne 0 ]
	  then
	    echo "sh $DD_SYSIN"
	    echo 0 > status.${JON}
	    exit 0
	  else
	    sh $DD_SYSIN
	    sort_status=$?
	    echo $sort_status > status.${JON}
	  exit $sort_status
	  fi
        else
	TMP_OUT=$TMPSORT/sortwk.$$;export TMP_OUT
#if sysin is not a shell to be executed
	  case $SORT_MODE in
	  syncsort | SYNCSORT)
	  #
	  # SYNCSORT:
	  # The SORT sysin file must be translated using the syncsort facility,
	  # and the infile and outfile must specified in the following way:
	  # /INFILE  \$DD_SORTIN FIXED \$LRECL_SORTIN
	  # /OUTFILE \$TMP_OUT OVERWRITE
	  #
	    gdginst=`echo $DD_SORTIN | grep ":" > $DEVNULL`
	    temp1=$?
# if the file is a base name gdg
# set the multiple DD_SORTIN variables
	    if [ $temp1 = "0" ]
            then
              num=0
              for file in `echo $DD_SORTIN | sed -e 's/:/ /g'`
              do
               if [ $validate -ne 0 ]
	       then
                ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
	        if [ -f $fil ]
	        then
                 echo  DD_SORTIN$ind=$fil
	        else
	         echo DD_SORTIN$ind=$DEVNULL
	        fi
	        echo export DD_SORTIN$ind
                num=`expr 1 + $num`
               else
                ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
	        if [ -f $fil ]
	        then
                 eval DD_SORTIN$ind=$fil
	        else
	         eval DD_SORTIN$ind=$DEVNULL
	        fi
	        export DD_SORTIN$ind
                num=`expr 1 + $num`
	       fi
              done
            fi
# invoke the syncsort
	    if [ $validate -ne 0 ]
	    then
    	      aa=`cat $DD_SYSIN`
    	      echo $SYNCSORT_EXEC `eval echo $aa`
	      echo $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      echo 0 > status.${JON}
	      exit 0
	    else
    	      aa=`cat $DD_SYSIN`
    	      $SYNCSORT_EXEC `eval echo $aa`
	      sort_status=$?
	      if [ $sort_status -ne 0 ]
	      then
	        rm -f $TMP_OUT
    	        echo $SYNCSORT_EXEC `eval echo $aa` aborted
	        echo $sort_status > status.${JON}
	        exit $sort_status
	      fi
	      $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      sort_status=$?
	      rm $TMP_OUT
 echo 	      echo $sort_status > status.${JON}
	      exit $sort_status
	    fi
	    ;;
	  cosort | COSORT)
	  #
	  # COSORT:
	  # The SORT sysin file must be translated to the appropiate sorctl 
	  # syntax. 
	  # You can also specify the $LRECL_SORTIN environment variable in 
	  # the /LENGTH parameter.
	  # You can set the SORTOPTS environment variable in the .btshrc file 
	  # to the appropiate options you wish for the sorctl command.
	  #    	
	    if [ $validate -ne 0 ]
	    then
	      if [ "a$LRECL_SORTOUT" = "a" ]
	      then
    	        echo sortcl /INFILE=$DD_SORTIN /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$TMP_OUT
	      else
    	        echo sortcl /INFILE=$DD_SORTIN /LENGTH=$LRECL_SORTOUT /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$TMP_OUT
	      fi
	      echo $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      echo 0 > status.${JON}
	      exit 0
	    else
	      if [ "a$LRECL_SORTOUT" = "a" ]
	      then
    	        sortcl /INFILE=$DD_SORTIN /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$TMP_OUT
	        sort_status=$?
	      else
    	        sortcl /INFILE=$DD_SORTIN /LENGTH=$LRECL_SORTOUT /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$TMP_OUT
	        sort_status=$?
	      fi
	      if [ $sort_status -ne 0 ]
	      then
	        rm -f $TMP_OUT
    	        echo sortcl /INFILE=$DD_SORTIN /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$TMP_OUT aborted
	        echo $sort_status > status.${JON}
	        exit $sort_status
	      fi
	      $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      sort_status=$?
	      rm $TMP_OUT
	      echo $sort_status > status.${JON}
	      exit $sort_status
	    fi
	    ;;
	  sortx | SORTX)
	    if [ $validate -ne 0 ]
	    then
	      echo $SORT_FS -i DD_SORTIN -o $TMP_OUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
	      echo $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      echo 0 > status.${JON}
	      exit 0
	    else
	      $SORT_FS -i DD_SORTIN -o $TMP_OUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
	      sort_status=$?
	      if [ $sort_status -ne 0 ]
	      then
	        rm -f $TMP_OUT
	        echo $SORT_FS -i DD_SORTIN -o $TMP_OUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK aborted
	        echo $sort_status > status.${JON}
	        exit $sort_status
	      fi
	      $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
	      sort_status=$?
	      rm $TMP_OUT
	      echo $sort_status > status.${JON}
	      exit $sort_status
	    fi
	    ;;
	  *)
	    if [ $validate -ne 0 ]
	    then
	      echo invalid SORT_MODE option $SORT_MODE
	      echo 0 > status.${JON}
	      exit 0
	    else
	      echo invalid SORT_MODE option $SORT_MODE
	      echo 2 > status.${JON}
	      exit 2
	    fi
	    ;;
	  esac
        fi
  else
#if sysin exists and it is empty
	if [ $validate -ne 0 ]
	then
	  echo $UNIKIX/bin/unikixbld -t v -s $DD_SORTIN -d $DD_SORTOUT -r $RECFOR_SORTIN
	  echo 0 > status.${JON}
	  exit 0
	else
	  $UNIKIX/bin/unikixbld -t v -s $DD_SORTIN -d $DD_SORTOUT -r $RECFOR_SORTIN
	  sort_status=$?
	  echo $sort_status > status.${JON}
	  exit $sort_status
	fi
  fi
fi
#end of SORTOUT VS type }

#if SORTIN and SORTOUT are FS type
  if [ -s $DD_SYSIN ]
  then
#if sysin exists and it is not empty
        execute=`sed -n '1 s/^#!.*/Execute_sh/p' $DD_SYSIN`
        if [ "a$execute" = "aExecute_sh" ]
        then
#if sysin is a shell to be executed
          if [ $validate -ne 0 ]
          then
            echo "sh $DD_SYSIN"
            echo 0 > status.${JON}
            exit 0
          else
            sh $DD_SYSIN
            sort_status=$?
            echo $sort_status > status.${JON}
          exit $sort_status
          fi
        fi
fi
case $SORT_MODE in
syncsort | SYNCSORT)
#
# SYNCSORT:
# The SORT sysin file must be translated using the syncsort facility,
# and the infile and outfile must specified in the following way:
# /INFILE  \$DD_SORTIN FIXED \$LRECL_SORTIN
# /OUTFILE \$DD_SORTOUT OVERWRITE
#
# if the file is a base name gdg
# set the multiple DD_SORTIN variables
	gdginst=`echo $DD_SORTIN | grep ":" > $DEVNULL`
	temp1=$?
	if [ $temp1 = "0" ]
	then
	  num=0
	  for fil in `echo $DD_SORTIN | sed -e 's/:/ /g'`
	  do
	    if [ $validate -ne 0 ]
	    then
	      ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
	      if [ -f $fil ]
	      then
                echo DD_SORTIN$ind=$fil
	      else
	        echo DD_SORTIN$ind=$DEVNULL
	      fi
	      echo export DD_SORTIN$ind
	      num=`expr 1 + $num`
	    else
	      ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
	      if [ -f $fil ]
	      then
	       eval DD_SORTIN$ind=$fil
	      else
	       eval DD_SORTIN$ind=$DEVNULL
	      fi
	      export DD_SORTIN$ind
	      num=`expr 1 + $num`
	    fi
	  done
	fi
# invoke the syncsort
	if [ $validate -ne 0 ]
	then
	  echo  
    	  aa=`cat $DD_SYSIN`
    	  echo $SYNCSORT_EXEC `eval echo $aa`
	  echo 0 > status.${JON}
	  exit 0
	else
    	  aa=`cat $DD_SYSIN`
    	  $SYNCSORT_EXEC `eval echo $aa`
	  sort_status=$?
	  echo $sort_status > status.${JON}
	  exit $sort_status
	fi
	;;
	
cosort | COSORT)
#
# COSORT:
# The SORT sysin file must be translated to the appropiate sorctl syntax. 
# You can also specify the $LRECL_SORTIN environment variable in the /LENGTH
# parameter.
# You can set the SORTOPTS environment variable in the .btshrc file to the
# appropiate options you wish for the sorctl command.
#
#    	
	if [ $validate -ne 0 ]
	then
	  if [ "a$LRECL_SORTOUT" = "a" ]
	  then
    	    echo sortcl /INFILE=$DD_SORTIN /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$DD_SORTOUT 
	  else
    	    echo sortcl /INFILE=$DD_SORTIN /LENGTH=$LRECL_SORTOUT /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$DD_SORTOUT 
	  fi
	  echo 0 > status.${JON}
	  exit 0
	else
	  if [ "a$LRECL_SORTOUT" = "a" ]
	  then
    	    sortcl /INFILE=$DD_SORTIN /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$DD_SORTOUT 
	    sort_status=$?
	  else
    	    sortcl /INFILE=$DD_SORTIN /LENGTH=$LRECL_SORTOUT /SPEC=$DD_SYSIN $SORTOPTS /OUTFILE=$DD_SORTOUT 
	    sort_status=$?
	  fi
	  echo $sort_status > status.${JON}
	  exit $sort_status
	fi
	;;
sortx | SORTX)
	if [ $validate -ne 0 ]
	then
	  echo $SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
	  echo 0 > status.${JON}
	  exit 0
	else
	  $SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
	  sort_status=$?
	  echo $sort_status > status.${JON}
	  exit $sort_status
	fi
	;;
*)
	if [ $validate -ne 0 ]
	then
	  echo invalid SORT_MODE option $SORT_MODE
	  echo 0 > status.${JON}
	  exit 0
	else
	  echo invalid SORT_MODE option $SORT_MODE
	  echo 2 > status.${JON}
	  exit 2
	fi
	;;
esac
