#!/bin/sh
cosort_vse(){
#
# the following is specific to VSE mode AND COSORT sort
#
#
# COSORT should handle ALL file modes internally, freeing us from having
# to determine between FS and VS, SORTIN or SORTOUT.  COSORT expects
# everything to be in the SYSIN stream, so we can just pass that through
# regardless of file file types.
#
# the CoSort control cards will all be processed as SYSIN.  So, we only need
# to pass the entire, fully expanded, SYSIN value to sortcl and check the
# return.  For any VSAM, input or output, CoSort will make the appropriate
# CISAM calls to do I/O and gather any file attributes they may need
# internally to their sort process.
#
aa=`cat $DD_SYSIN`
if [ $validate -ne 0 ]
then
   echo $COSORT_EXEC $aa
   echo 0 > status.${JON}
   exit 0
else
   cmd=`eval echo $COSORT_EXEC $aa`
   $cmd
   sort_status=$?
   echo $sort_status > status.${JON}
   exit $sort_status
fi
# end VSE and COSORT option
}

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 "$COSORT_EXEC" ]
then
   COSORT_EXEC="sortcl"
   export COSORT_EXEC
fi
if [ -z "$SYNCSORT_EXEC" ]
then
   case "$EBM_OS_NAME" in
     NT)
        SYNCSORT_EXEC="syncsort.exe"
     ;;
     *)
        SYNCSORT_EXEC="syncsort"
     ;;
   esac
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, 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 SYSIN does not exist
   if [ ! -f $DD_SYSIN ]
   then
      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 sysin exists and it is not empty
  if [ -s $DD_SYSIN ]
  then

    execute=`sed -n '1 s/^#!.*/Execute_sh/p' $DD_SYSIN`
    #if sysin is a shell to be executed
    if [ "a$execute" = "aExecute_sh" ]
    then
      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:
       #     don't support VS - VS in syncsort
       if [ "$SORTOUT_TYPE" != "VS" ]  
       then
          #
          # set the optional syncsort format for for any
          # of the DSN_xxx files.  The user may have
          # $SYNCFMT_{dsname} set in the syncsort sysin
          # stream.  i.e.  /infile \$DD_SORTOUT \$SYNCFMT_SORTOUT
          # were $SYNCFMT_SORTOUT={fixed|variable} based on the
          # $SORTOUT_RECFMT.  Default is fixed
          #
          # some OS's can NOT export a var outside the scopt
          # of a while/done, so put to a file and source
          # after the while/done
          #
          rm -rf syncfmt.${EBMSYS}.${JON}
          touch syncfmt.${EBMSYS}.${JON}
          env | grep "^DSN_" | cut -f1 -d'=' | cut -f2 -d'_' | while read dsname
          do
             eval SYNCFMT_${dsname}="fixed"
             export `eval echo SYNCFMT_${dsname}`
             name=$`eval echo ${dsname}_RECFMT`
             value=`eval echo $name`
             if [ "$value" = "recordv" ]
             then
                eval SYNCFMT_${dsname}="variable"
                export `eval echo SYNCFMT_${dsname}`
             else 
                if [ "$value" = "mfrcdv" ]
                then
                   eval SYNCFMT_${dsname}="mfvariable"
                   export `eval echo SYNCFMT_${dsname}`
                fi
             fi
             tmpvarnm=`eval echo SYNCFMT_${dsname}`
             tmpx=$`eval echo $tmpvarnm`
             tmpvarval=`eval echo $tmpx`
             echo "${tmpvarnm}=${tmpvarval}" >> syncfmt.${EBMSYS}.${JON}
             echo "export ${tmpvarnm}" >> syncfmt.${EBMSYS}.${JON}
          done
          . syncfmt.${EBMSYS}.${JON}
          rm -rf syncfmt.${EBMSYS}.${JON}
          #
# if the file is a base name gdg or concat
# set the multiple DD_SORTIN variables
#
          gdgconc="no"
#
# on NT we must go through the process of 
# "normalizing" the path name becuase with
# NutCracker it can be all fouled up with
# /C=xxx formats.
          case "$EBM_OS_NAME" in
             NT)
                # splitconcpaths will create, space separated, common
                # NT path names in the form C:<filepath> [C:<filepath>]
                tmp_dd_sortin=`ebmsed $DD_SORTIN splitconcpaths`
                gdginst=`echo $tmp_dd_sortin | grep " " > $DEVNULL`
                temp1=$?
             ;;
  
             *)
                tmp_dd_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
                gdginst=`echo $DD_SORTIN | grep ":" > $DEVNULL`
                temp1=$?
             ;;
          esac

          if [ $temp1 = "0" ]
          then
            gdgconc="yes"
            num=0
            for file in $tmp_dd_sortin
            do
#
# on NT we have a problem, again with NutCracker, getting the
# NT equivalent of /dev/null, NUL, to work.  NutCracker fouls 
# it up.  So, for NT, do NOT generate any DD_SORTINxx for null
# files.  For UNIX, continue as before.
#
              case "$EBM_OS_NAME" in
                 NT)
                    if [ $validate -ne 0 ]
                    then
                       ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                       if [ -f $file ]
                       then
                          eval DD_SORTIN${ind}=$file
                          echo DD_SORTIN${ind}=$file
                          export `eval echo DD_SORTIN${ind}`
                          echo export DD_SORTIN${ind}
                          num=`expr 1 + $num`
                       fi
                    else
                       ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                       if [ -f $file ]
                       then
                          eval DD_SORTIN${ind}=$file
                          export DD_SORTIN${ind}
                          num=`expr 1 + $num`
                       fi
                    fi
                 ;;
  
                 *)
                    if [ $validate -ne 0 ]
                    then
                       ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                       if [ -f $file ]
                       then
                          eval DD_SORTIN${ind}=$file
                          echo DD_SORTIN${ind}=$file
                       else
                          eval DD_SORTIN${ind}=$DEVNULL
                          echo DD_SORTIN${ind}=$DEVNULL
                       fi
                       export `eval echo DD_SORTIN${ind}`
                       echo export DD_SORTIN${ind}
                       num=`expr 1 + $num`
                    else
                       ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                       if [ -f $file ]
                       then
                          eval DD_SORTIN${ind}=$file
                       else
                          eval DD_SORTIN${ind}=$DEVNULL
                       fi
                       export DD_SORTIN${ind}
                       num=`expr 1 + $num`
                    fi
                 ;;
              esac

            done
          fi
# invoke the syncsort
        # if we have a gdg or concatenate, then if we have
        # an \infile $DD_SORTIN xxxxxx
        # we must expand the \infile $DD_SORTIN xxxxx into N
        # number of \infile $DD_SORTINnn xxxxx lines.  One for
        # each number of DD_SORTINnn files that we created
        # earlier when we broke out the DD_SORTIN
#
        grep " \$DD_SORTIN" $DD_SYSIN
        if [ $? = 0 ]
        then
           # make sure its not DD_SORTIN0n
           grep " \$DD_SORTIN0" $DD_SYSIN
           if [ $? != 0 ]
           then
              # we have now determined we have a $DD_SORTIN in our sysin
              if [ "$gdgconc" = "yes" ]
              then
                 # we know we have a concatenate from earlier setting of gdgconc
                 # call ebm_syncsort_expand of ebmsed to create the
                 # multiple /infile lines
                 #
                 aa=`ebmsed EBM_SYNCSORT_EXPAND`
              else
                 aa=`cat $DD_SYSIN`
              fi
           else
              aa=`cat $DD_SYSIN`
           fi
        else
           aa=`cat $DD_SYSIN`
        fi
        if [ $validate -ne 0 ]
        then
          echo $SYNCSORT_EXEC `eval echo $aa`
          echo 0 > status.${JON}
          exit 0
        else
#
# if a DSN_SYSOUT exists, syncsort on the mainframe would put
# the statistics to that file.  we will get the filename from
# the DD_SYSOUT variable and 'tee' the results there.
#
           if [ "a$DSN_SYSOUT" != "a" ]
           then
              resultsfile=`eval echo $DD_SYSOUT`
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$ > $resultsfile 2>&1
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
                    errno=$?
                    if [ "$errno" != 0 ]
                    then
                       echo "Invalid UNIX data in Syncsort sysin"
                       exit $errno
                    fi
                    $SYNCSORT_EXEC `eval echo $aa` > $resultsfile 2>&1
                 ;;
              esac
              sort_status=$?
              cat $resultsfile
           else
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
          errno=$?
          if [ "$errno" != 0 ]
          then
             echo "Invalid UNIX data in Syncsort sysin"
             exit $errno
          fi
                    $SYNCSORT_EXEC `eval echo $aa`
                 ;;
              esac
              sort_status=$?
           fi
           echo $sort_status > status.${JON}
           exit $sort_status
        fi
       else
          echo "ERROR: VSAM to VSAM is not supported for SYNCSORT"
       fi
       ;;
          
       cosort | COSORT)
   #
   # COSORT:
   #       
          case "$BATCH_MODE" in
             vse | VSE)
                cosort_vse
             ;;
             *)
                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
             ;;
          esac
       ;;

   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 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
   touch $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
          # the output of the syncsort, will then become the input to
          # unikixbld to build the VSAM file
          #
          # set the optional syncsort format for any
          # of the DSN_xxx files.  The user may have
          # $SYNCFMT_{dsname} set in the syncsort sysin
          # stream.  i.e.  /infile \$DD_SORTOUT \$SYNCFMT_SORTOUT
          # where $SYNCFMT_SORTOUT={fixed|variable} based on the
          # $SORTOUT_RECFMT.  Default is fixed
          #
          # some OS's can NOT export a var outside the scopt
          # of a while/done, so put to a file and source
          # after the while/done
          #
          rm -rf syncfmt.${EBMSYS}.${JON}
          touch syncfmt.${EBMSYS}.${JON}
          env | grep "^DSN_" | cut -f1 -d'=' | cut -f2 -d'_' | while read dsname
          do
             eval SYNCFMT_${dsname}="fixed"
             export `eval echo SYNCFMT_${dsname}`
             name=$`eval echo ${dsname}_RECFMT`
             value=`eval echo $name`
             if [ "$value" = "recordv" ]
             then
                eval SYNCFMT_${dsname}="variable"
                export `eval echo SYNCFMT_${dsname}`
        else
                if [ "$value" = "mfrcdv" ]
                then
                   eval SYNCFMT_${dsname}="mfvariable"
                   export `eval echo SYNCFMT_${dsname}`
                fi
             fi
             tmpvarnm=`eval echo SYNCFMT_${dsname}`
             tmpx=$`eval echo $tmpvarnm`
             tmpvarval=`eval echo $tmpx`
             echo "${tmpvarnm}=${tmpvarval}" >> syncfmt.${EBMSYS}.${JON}
             echo "export ${tmpvarnm}" >> syncfmt.${EBMSYS}.${JON}
          done
          . syncfmt.${EBMSYS}.${JON}
          rm -rf syncfmt.${EBMSYS}.${JON}
     #

# if the file is a base name gdg or concat
# set the multiple DD_SORTIN variables
#
   gdgconc="no"
#
# on NT we must go through the process of 
# "normalizing" the path name becuase with
# NutCracker it can be all fouled up with
# /C=xxx formats.
        case "$EBM_OS_NAME" in
           NT)
              # splitconcpaths will create, space separated, common
              # NT path names in the form C:<filepath> [C:<filepath>]
              tmp_dd_sortin=`ebmsed $DD_SORTIN splitconcpaths`
              gdginst=`echo $tmp_dd_sortin | grep " " > $DEVNULL`
              temp1=$?
           ;;

           *)
              tmp_dd_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
              gdginst=`echo $DD_SORTIN | grep ":" > $DEVNULL`
              temp1=$?
           ;;
        esac

   if [ $temp1 = "0" ]
   then
     gdgconc="yes"
     num=0
     for file in $tmp_dd_sortin
     do
#
# now create the individual DD_SORTINnn files from the original DD_SORTIN
#
# on NT we have a problem, again with NutCracker, getting the
# NT equivalent of /dev/null, NUL, to work.  NutCracker fouls 
# it up.  So, for NT, do NOT generate any DD_SORTINxx for null
# files.  For UNIX, continue as before.
#
            case "$EBM_OS_NAME" in
               NT)
                  if [ $validate -ne 0 ]
                  then
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        echo DD_SORTIN${ind}=$file
                        export `eval echo DD_SORTIN${ind}`
                        echo export DD_SORTIN${ind}
                        num=`expr 1 + $num`
                     fi
                  else
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        export DD_SORTIN${ind}
                        num=`expr 1 + $num`
                     fi
                  fi
               ;;

               *)
                  if [ $validate -ne 0 ]
                  then
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        echo DD_SORTIN${ind}=$file
                     else
                        eval DD_SORTIN${ind}=$DEVNULL
                        echo DD_SORTIN${ind}=$DEVNULL
                     fi
                     export `eval echo DD_SORTIN${ind}`
                     echo export DD_SORTIN${ind}
                     num=`expr 1 + $num`
                  else
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                     else
                        eval DD_SORTIN${ind}=$DEVNULL
                     fi
                     export DD_SORTIN${ind}
                     num=`expr 1 + $num`
                  fi
               ;;
            esac
     done
        fi  # if [ $temp1 = "0" ]
#
# invoke the syncsort
#
        # if we have a gdg or concatenate, then if we have
        # an \infile $DD_SORTIN xxxxxx
        # we must expand the \infile $DD_SORTIN xxxxx into N
        # number of \infile $DD_SORTINnn xxxxx lines.  One for
        # each number of DD_SORTINnn files that we created
        # earlier when we broke out the DD_SORTIN
        grep " \$DD_SORTIN" $DD_SYSIN
        if [ $? = 0 ]
        then
           # make sure its not DD_SORTIN0n
           grep " \$DD_SORTIN0" $DD_SYSIN
           if [ $? != 0 ]
           then
              # we have now determined we have a $DD_SORTIN in our sysin
              if [ "$gdgconc" = "yes" ]
              then
                 # we know we have a concatenate from earlier setting of gdgconc
                 # call ebm_syncsort_expand of ebmsed to create the
                 # multiple /infile lines
                 #
                 aa=`ebmsed EBM_SYNCSORT_EXPAND`
              else
                 aa=`cat $DD_SYSIN`
              fi
           else
              aa=`cat $DD_SYSIN`
           fi
        else
           aa=`cat $DD_SYSIN`
        fi
        if [ $validate -ne 0 ]
        then
           DD_SORTOUT_SAVE=$DD_SORTOUT
           DD_SORTOUT=$TMP_OUT; export DD_SORTOUT
           echo $SYNCSORT_EXEC `eval echo $aa`
           DD_SORTOUT=$DD_SORTOUT_SAVE; export DD_SORTOUT
           echo $UNIKIX/bin/unikixbld -t v -s $TMP_OUT -d $DD_SORTOUT -r $RECFOR_SORTIN
           echo 0 > status.${JON}
           exit 0
        else
           DD_SORTOUT_SAVE=$DD_SORTOUT
           DD_SORTOUT=$TMP_OUT; export DD_SORTOUT
#
# if a DSN_SYSOUT exists, syncsort on the mainframe would put
# the statistics to that file.  we will get the filename from
# the DD_SYSOUT variable and 'tee' the results there.
#
           if [ "a$DSN_SYSOUT" != "a" ]
           then
              resultsfile=`eval echo $DD_SYSOUT`
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$ > $resultsfile 2>&1
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
          errno=$?
          if [ "$errno" != 0 ]
          then
             echo "Invalid UNIX data in Syncsort sysin"
             exit $errno
          fi
                    $SYNCSORT_EXEC `eval echo $aa` > $resultsfile 2>&1
                 ;;
              esac
              sort_status=$?
              cat $resultsfile
           else
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
          errno=$?
          if [ "$errno" != 0 ]
          then
             echo "Invalid UNIX data in Syncsort sysin"
             exit $errno
          fi
                    $SYNCSORT_EXEC `eval echo $aa`
                 ;;
              esac
              sort_status=$?
           fi
           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
           DD_SORTOUT=$DD_SORTOUT_SAVE; export DD_SORTOUT
           $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
            ;;
     cosort | COSORT)

          case "$BATCH_MODE" in
             vse | VSE)
                cosort_vse
             ;;
             *)
                # The COSORT 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
             ;;
          esac
       ;;
     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 }

####################################################################
#
#                    SORTIN and SORTOUT are FS type {
# if we get to this section of code, then both are FS
#
####################################################################
#if SORTIN and SORTOUT are FS type
  if [ "a${DD_SYSIN}" = "a" ]
  then
     if [ $validate -ne 0 ]
     then
       echo DD_SYSIN not defined
       echo 0 > status.${JON}
       exit 0
     else
       echo DD_SYSIN not defined
       echo 2 > status.${JON}
       exit 2
     fi
  fi
  if [ ! -f $DD_SYSIN ]
  then
     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
     fi
  else
# sysin exists but is empty
     if [ $validate -ne 0 ]
     then
       echo $DD_SYSIN is empty
       echo 0 > status.${JON}
       exit 0
     else
       echo $DD_SYSIN is empty
       echo 2 > status.${JON}
       exit 2
     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
#
          #
          # set the optional syncsort format for any
          # of the DSN_xxx files.  The user may have
          # $SYNCFMT_{dsname} set in the syncsort sysin
          # stream.  i.e.  /infile \$DD_SORTOUT \$SYNCFMT_SORTOUT
          # where $SYNCFMT_SORTOUT={fixed|variable} based on the
          # $SORTOUT_RECFMT.  Default is fixed
          #
          # on some platforms, the export of a var, within a
          # while loop only has scope for that while loop.
          # save the value to a file and resource once outside
          # of the while loop to preserve
          #
          rm -rf syncfmt.${EBMSYS}.${JON} 2>$DEVNULL
          touch syncfmt.${EBMSYS}.${JON}

          env | grep "^DSN_" | cut -f1 -d'=' | cut -f2 -d'_' | while read dsname
          do
             eval SYNCFMT_${dsname}="fixed"
             export `eval echo SYNCFMT_${dsname}`
             name=$`eval echo ${dsname}_RECFMT`
             value=`eval echo $name`
             if [ "$value" = "recordv" ]
             then
                eval SYNCFMT_${dsname}="variable"
                export `eval echo SYNCFMT_${dsname}`
             else
                if [ "$value" = "mfrcdv" ]
                then
                   eval SYNCFMT_${dsname}="mfvariable"
                   export `eval echo SYNCFMT_${dsname}`
                fi
             fi
             tmpvarnm=`eval echo SYNCFMT_${dsname}`
             tmpx=$`eval echo $tmpvarnm`
             tmpvarval=`eval echo $tmpx`
             echo "${tmpvarnm}=${tmpvarval}" >> syncfmt.${EBMSYS}.${JON}
             echo "export ${tmpvarnm}" >> syncfmt.${EBMSYS}.${JON}
          done
          . syncfmt.${EBMSYS}.${JON}
          rm -rf syncfmt.${EBMSYS}.${JON}
          #
# if the file is a base name gdg or concat
# set the multiple DD_SORTIN variables
   gdgconc="no"
# on NT we must go through the process of 
# "normalizing" the path name becuase with
# NutCracker it can be all fouled up with
# /C=xxx formats.
        case "$EBM_OS_NAME" in
           NT)
              # splitconcpaths will create, space separated, common
              # NT path names in the form C:<filepath> [C:<filepath>]
              tmp_dd_sortin=`ebmsed $DD_SORTIN splitconcpaths`
              gdginst=`echo $tmp_dd_sortin | grep " " > $DEVNULL`
              temp1=$?
           ;;

           *)
              tmp_dd_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
              gdginst=`echo $DD_SORTIN | grep ":" > $DEVNULL`
              temp1=$?
           ;;
        esac

   if [ $temp1 = "0" ]
   then
     gdgconc="yes"
     num=0
     for file in $tmp_dd_sortin
     do
#
# on NT we have a problem, again with NutCracker, getting the
# NT equivalent of /dev/null, NUL, to work.  NutCracker fouls 
# it up.  So, for NT, do NOT generate any DD_SORTINxx for null
# files.  For UNIX, continue as before.
#
            case "$EBM_OS_NAME" in
               NT)
                  if [ $validate -ne 0 ]
                  then
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        echo DD_SORTIN${ind}=$file
                        export `eval echo DD_SORTIN${ind}`
                        echo export DD_SORTIN${ind}
                        num=`expr 1 + $num`
                     fi
                  else
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        export DD_SORTIN${ind}
                        num=`expr 1 + $num`
                     fi
                  fi
               ;;

               *)
                  if [ $validate -ne 0 ]
                  then
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                        echo DD_SORTIN${ind}=$file
                     else
                        eval DD_SORTIN${ind}=$DEVNULL
                        echo DD_SORTIN${ind}=$DEVNULL
                     fi
                     export `eval echo DD_SORTIN${ind}`
                     echo export DD_SORTIN${ind}
                     num=`expr 1 + $num`
                  else
                     ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                     if [ -f $file ]
                     then
                        eval DD_SORTIN${ind}=$file
                     else
                        eval DD_SORTIN${ind}=$DEVNULL
                     fi
                     export DD_SORTIN${ind}
                     num=`expr 1 + $num`
                  fi
               ;;
            esac

     done
   fi
# invoke the syncsort
        # if we have a gdg or concatenate, then if we have
        # an \infile $DD_SORTIN xxxxxx
        # we must expand the \infile $DD_SORTIN xxxxx into N
        # number of \infile $DD_SORTINnn xxxxx lines.  One for
        # each number of DD_SORTINnn files that we created
        # earlier when we broke out the DD_SORTIN
        grep " \$DD_SORTIN" $DD_SYSIN
        if [ $? = 0 ]
        then
           # make sure its not DD_SORTIN0n
           grep " \$DD_SORTIN0" $DD_SYSIN
           if [ $? != 0 ]
           then
              # we have now determined we have a $DD_SORTIN in our sysin
              if [ "$gdgconc" = "yes" ]
              then
                 # we know we have a concatenate from earlier setting of gdgconc
                 # call ebm_syncsort_expand of ebmsed to create the
                 # multiple /infile lines
                 #
                 aa=`ebmsed EBM_SYNCSORT_EXPAND`
              else
                 aa=`cat $DD_SYSIN`
              fi
           else
              aa=`cat $DD_SYSIN`
           fi
        else
           aa=`cat $DD_SYSIN`
        fi
        if [ $validate -ne 0 ]
        then
          echo $SYNCSORT_EXEC `eval echo $aa`
          echo 0 > status.${JON}
          exit 0
        else
#
# if a DSN_SYSOUT exists, syncsort on the mainframe would put
# the statistics to that file.  we will get the filename from
# the DD_SYSOUT variable and 'tee' the results there.
#
           if [ "a$DSN_SYSOUT" != "a" ]
           then
              resultsfile=`eval echo $DD_SYSOUT`
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$ > $resultsfile 2>&1
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
          errno=$?
          if [ "$errno" != 0 ]
          then
             echo "Invalid UNIX data in Syncsort sysin"
             exit $errno
          fi
                    $SYNCSORT_EXEC `eval echo $aa` > $resultsfile 2>&1
                 ;;
              esac
         sort_status=$?
         cat $resultsfile
           else
              case "$EBM_OS_NAME" in
                 NT)
                    xx=`eval echo $aa`
                    echo $xx > /tmp/syncsortsysin$$
                    $SYNCSORT_EXEC /insert /tmp/syncsortsysin$$
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
          errno=$?
          if [ "$errno" != 0 ]
          then
             echo "Invalid UNIX data in Syncsort sysin"
             exit $errno
          fi
                   $SYNCSORT_EXEC `eval echo $aa`
                 ;;
              esac
         sort_status=$?
           fi
      echo $sort_status > status.${JON}
      exit $sort_status
        fi
;;
   
cosort | COSORT)

   case "$BATCH_MODE" in
       vse | VSE)
          cosort_vse
       ;;
       *)
          #
          # 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
       ;;
   esac
;;
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
#end of SORTIN and SORTOUT are FS type }
