#!/bin/ksh
get_cosort_version(){
#
# the following function is to determine which version of
# CoSort is being used.  As of Version 8, we are only going
# to pass in the SYSIN, we will not attempt to build parameters
# this is similar to the vse way.
#
version_next="no"
cosort_ver=8
export cosort_ver
b=`sortcl /VERSION | sed 's/^.*Ver/Ver/'`
for word in `echo $b`
do
   if [ "$version_next" = "yes" ]
   then
      cosort_ver=`echo $word|cut -f1 -d'.'`
      export cosort_ver
      break
   else
      lcword=`echo $word | tr -s '[:upper:]' '[:lower:]'`
      if [ "$lcword" = "version" ]
      then
         version_next="yes"
      fi
   fi
done
}

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
}

#
# Begin SORT
#
#-----------------------------------------------------------------------------
#  If SORTIN is a GDG base, ASSGNDD sets the variable
#  <ddname>_GDG_BASENAME = 1
#
#-----------------------------------------------------------------------------
sortin_gdg_basename=0
if [ "a$SORTIN_GDG_BASENAME" = "a1" ]
then
   sortin_gdg_basename=1
fi

DD_SORTIN=`ebmsed $DD_SORTIN`
DD_SORTOUT=`ebmsed $DD_SORTOUT`
DD_SYSIN=`ebmsed $DD_SYSIN`
if [ "a$VERBOSE" = "aYES" ]
then
   echo PGM=`basename $0` -----Starting in verbose mode--------
   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
validate=0
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:
          #
          # 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 | tr ':' ' '`
                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
                       #------------------------------------------------------
                       # Mainframe behavior:
                       #
                       #
                       # GDG:
                       # If one or more generation data group instances
                       # do not exist, SORT does not abort.
                       #
                       # DD Concatenation:
                       # If any of the input files does not exist,
                       # job aborts.
                       #
                       # If processing GDG concatenation, set non-existing
                       # files to $DEVNULL. This causes Syncsort not to
                       # abort on non-existing files.
                       #
                       # If processing DD concatenation, take no action for
                       # non-existent files. Syncsort will abort.
                       #------------------------------------------------------
                       ind=`echo $num | awk '{ printf "%2.2d", $1 }'`
                       if [ -f $file ]
                       then
                          eval DD_SORTIN${ind}=$file
                          echo DD_SORTIN${ind}=$file
                       else
                          # Only redirect to $DEVNULL if GDG basename
                          if [ $sortin_gdg_basename -eq 1 ]
                          then
                             eval DD_SORTIN${ind}=$DEVNULL
                             echo DD_SORTIN${ind}=$DEVNULL
                          else
                             eval DD_SORTIN${ind}=$file
                             echo DD_SORTIN${ind}=$file
                          fi
                       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
                          if [ "a$VERBOSE" = "aYES" ]
                          then
                             echo "DD_SORTIN${ind}=$file"
                          fi
                       else
                          # Only redirect to $DEVNULL if GDG basename
                          if [ $sortin_gdg_basename -eq 1 ]
                          then
                             eval DD_SORTIN${ind}=$DEVNULL
                          else
                             eval DD_SORTIN${ind}=$file
                          fi
                       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 >$DEVNULL
        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 and handle any
              # $DD_SORTINnn lines.
              #
              aa=`ebmsed EBM_SYNCSORT_EXPAND`
           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
                    bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                    bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                    if [ "a$VERBOSE" = "aYES" ]
                    then
                       echo "$SYNCSORT_EXEC $bb"
                    fi
                    $SYNCSORT_EXEC "$bb" > $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
                    bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                    bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                    if [ "a$VERBOSE" = "aYES" ]
                    then
                       echo "$SYNCSORT_EXEC $bb"
                    fi
                    $SYNCSORT_EXEC "$bb"
                 ;;
              esac
              sort_status=$?
           fi
           echo $sort_status > status.${JON}
           exit $sort_status
        fi
       ;;
          
       cosort | COSORT)
   #
   # COSORT:
   #       
          case "$BATCH_MODE" in
             vse | VSE)
                cosort_vse
             ;;
             *)
                get_cosort_version
                if [ $cosort_ver -gt 7 ]
                then
                   # if we have concatenated files in our DD_SORTIN
                   # we must modifiy the format to match what CoSort
                   # expects.  The CoSort format is comma separated
                   # with a space optional.
                   #
                   # CoSort does not recognize /dev/null for a non-existent
                   # file, so if we are missing any files, we must skip them
                   # in the concatenated DD_SORTIN list.
                   #
                   sav_dd_sortin=`echo $DD_SORTIN`
                   concstat=`echo $DD_SORTIN | grep ":" > $DEVNULL`
                   concstat=$?
                   if [ $concstat = "0" ]
                   then
                      tmp_xx_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
                      have_file="no"
                      for file in $tmp_xx_sortin
                      do
                         if [ -f $file ]
                         then
                            if [ "$have_file" = "no" ]
                            then
                               tmp_dd_sortin=`echo ${file}`
                               have_file="yes"
                            else
                               tmp_dd_sortin=`echo ${tmp_dd_sortin},${file}`
                            fi
                         fi
                      done
                      if [ "$have_file" = "yes" ]
                      then
                         DD_SORTIN=`echo $tmp_dd_sortin` ; export DD_SORTIN
                      else
                         echo "WARNING: No valid DD_SORTIN files found"
                         DD_SORTIN=`echo $sav_dd_sortin` ; export DD_SORTIN
                      fi
                   fi
                   if [ "a$VERBOSE" = "aYES" -o $validate -ne 0 ]
                   then
                      echo "DD_SORTIN=$DD_SORTIN"
                      echo "sortcl /SPEC=$DD_SYSIN"
                      echo "sort parameters="
                      cat $DD_SYSIN
                   fi
                   if [ $validate -ne 0 ]
                   then
                      echo 0 > status.${JON}
                      exit 0
                   else
                      sortcl /SPEC=$DD_SYSIN
                      sort_status=$?
                      echo $sort_status > status.${JON}
                      exit $sort_status
                   fi
                else
                   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
                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 \$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_SORTIN \$SYNCFMT_SORTIN
          # where $SYNCFMT_SORTIN={fixed|variable} based on the
          # $SORTIN.  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 | tr ':' ' '`
              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
               ;;

               *)
                  #------------------------------------------------------
                  # Mainframe behavior:
                  #
                  #
                  # GDG:
                  # If one or more generation data group instances
                  # do not exist, SORT does not abort.
                  #
                  # DD Concatenation:
                  # If any of the input files does not exist,
                  # job aborts.
                  #
                  # If processing GDG concatenation, set non-existing
                  # files to $DEVNULL. This causes Syncsort not to
                  # abort on non-existing files.
                  #
                  # If processing DD concatenation, take no action for
                  # non-existent files. Syncsort will abort.
                  #------------------------------------------------------
                  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
                        # Only redirect to $DEVNULL if GDG basename
                        if [ $sortin_gdg_basename -eq 1 ]
                        then
                           eval DD_SORTIN${ind}=$DEVNULL
                           echo DD_SORTIN${ind}=$DEVNULL
                        else
                           eval DD_SORTIN${ind}=$file
                           echo DD_SORTIN${ind}=$file
                        fi
                     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
                        if [ "a$VERBOSE" = "aYES" ]
                        then
                           echo "DD_SORTIN${ind}=$file"
                        fi
                     else
                        # Only redirect to $DEVNULL if GDG basename
                        if [ $sortin_gdg_basename -eq 1 ]
                        then
                           eval DD_SORTIN${ind}=$DEVNULL
                        else
                           eval DD_SORTIN${ind}=$file
                        fi
                     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 >$DEVNULL
        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 and any
              # $DD_SORTINnn lines.
              #
              aa=`ebmsed EBM_SYNCSORT_EXPAND`
           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
                    bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                    bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                    if [ "a$VERBOSE" = "aYES" ]
                    then
                       echo "$SYNCSORT_EXEC $bb"
                    fi
                    $SYNCSORT_EXEC "$bb" > $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$$
                    sort_status=$?
                 ;;
                 *)
                    bb="echo `eval echo $aa`"
                    errno=$?
                    if [ "$errno" != 0 ]
                    then
                       echo "Invalid UNIX data in Syncsort sysin"
                       exit $errno
                    fi
                    bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                    bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                    if [ "a$VERBOSE" = "aYES" ]
                    then
                       echo "$SYNCSORT_EXEC $bb"
                    fi
                    $SYNCSORT_EXEC "$bb"
                    sort_status=$?
                 ;;
              esac
           fi
           if [ $sort_status -ne 0 ]
           then
              echo $SYNCSORT_EXEC `eval echo $aa` aborted
           fi
           echo $sort_status > status.${JON}
           exit $sort_status
        fi
            ;;
     cosort | COSORT)

          case "$BATCH_MODE" in
             vse | VSE)
                cosort_vse
             ;;
             *)
                get_cosort_version
                if [ $cosort_ver -gt 7 ]
                then
                   # if we have concatenated files in our DD_SORTIN
                   # we must modifiy the format to match what CoSort
                   # expects.  The CoSort format is comma separated
                   # with a space optional.
                   #
                   # CoSort does not recognize /dev/null for a non-existent
                   # file, so if we are missing any files, we must skip them
                   # in the concatenated DD_SORTIN list.
                   #
                   sav_dd_sortin=`echo $DD_SORTIN`
                   concstat=`echo $DD_SORTIN | grep ":" > $DEVNULL`
                   concstat=$?
                   if [ $concstat = "0" ]
                   then
                      tmp_xx_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
                      have_file="no"
                      for file in $tmp_xx_sortin
                      do
                         if [ -f $file ]
                         then
                            if [ "$have_file" = "no" ]
                            then
                               tmp_dd_sortin=`echo ${file}`
                               have_file="yes"
                            else
                               tmp_dd_sortin=`echo ${tmp_dd_sortin},${file}`
                            fi
                         fi
                      done
                      if [ "$have_file" = "yes" ]
                      then
                         DD_SORTIN=`echo $tmp_dd_sortin` ; export DD_SORTIN
                      else
                         echo "WARNING: No valid DD_SORTIN files found"
                         DD_SORTIN=`echo $sav_dd_sortin` ; export DD_SORTIN
                      fi
                   fi
                   if [ "a$VERBOSE" = "aYES" -o $validate -ne 0 ]
                   then
                      echo "DD_SORTIN=$DD_SORTIN"
                      echo "sortcl /SPEC=$DD_SYSIN"
                      echo "sort parameters="
                      cat $DD_SYSIN
                   fi
                   if [ $validate -ne 0 ]
                   then
                      echo 0 > status.${JON}
                      exit 0
                   else
                      sortcl /SPEC=$DD_SYSIN
                      sort_status=$?
                      echo $sort_status > status.${JON}
                      exit $sort_status
                   fi
                else
                   TMP_OUT=$TMPSORT/sortwk.$$;export TMP_OUT
                   touch $TMP_OUT
                   # 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}
                      rm -f $TMP_OUT
                      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
                fi
             ;;
          esac
       ;;
     sortx | SORTX)

       if [ "a$DD_SORTIN" = "a" ]
       then
          DD_SORTIN=$DEVNULL ; export DD_SORTIN
       elif [ ! -f $DD_SORTIN ]
       then
          DD_SORTIN=$DEVNULL ; export DD_SORTIN
       fi
       TMP_OUT=$TMPSORT/sortwk.$$;export TMP_OUT
       touch $TMP_OUT
       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}
         rm -f $TMP_OUT
         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 | tr ':' ' '`
              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
               ;;

               *)
                  #------------------------------------------------------
                  # Mainframe behavior:
                  #
                  #
                  # GDG:
                  # If one or more generation data group instances
                  # do not exist, SORT does not abort.
                  #
                  # DD Concatenation:
                  # If any of the input files does not exist,
                  # job aborts.
                  #
                  # If processing GDG concatenation, set non-existing
                  # files to $DEVNULL. This causes Syncsort not to
                  # abort on non-existing files.
                  #
                  # If processing DD concatenation, take no action for
                  # non-existent files. Syncsort will abort.
                  #------------------------------------------------------
                  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
                        # Only redirect to $DEVNULL if GDG basename
                        if [ $sortin_gdg_basename -eq 1 ]
                        then
                           eval DD_SORTIN${ind}=$DEVNULL
                           echo DD_SORTIN${ind}=$DEVNULL
                        else
                           eval DD_SORTIN${ind}=$file
                           echo DD_SORTIN${ind}=$file
                        fi
                     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
                        if [ "a$VERBOSE" = "aYES" ]
                        then
                           echo "DD_SORTIN${ind}=$file"
                        fi
                     else
                        # Only redirect to $DEVNULL if GDG basename
                        if [ $sortin_gdg_basename -eq 1 ]
                        then
                           eval DD_SORTIN${ind}=$DEVNULL
                        else
                           eval DD_SORTIN${ind}=$file
                        fi
                     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 >$DEVNULL
        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 and to handle and
              # $DD_SORTINnn lines.
              #
              aa=`ebmsed EBM_SYNCSORT_EXPAND`
           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
                    bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                    bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                    if [ "a$VERBOSE" = "aYES" ]
                    then
                       echo "$SYNCSORT_EXEC $bb"
                    fi
                    $SYNCSORT_EXEC "$bb" > $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
                   bb=`ebmsed EBMSED_EXPAND_STRING "$aa"`
                   bb=`ebmsed EBM_DELETE_BACK_SLASH "$bb"`
                   if [ "a$VERBOSE" = "aYES" ]
                   then
                      echo "$SYNCSORT_EXEC $bb"
                   fi
                   $SYNCSORT_EXEC "$bb"
                 ;;
              esac
              sort_status=$?
           fi
        echo $sort_status > status.${JON}
        exit $sort_status
        fi
;;
   
cosort | COSORT)

   case "$BATCH_MODE" in
       vse | VSE)
          cosort_vse
       ;;
       *)
          get_cosort_version
          if [ $cosort_ver -gt 7 ]
          then
             # if we have concatenated files in our DD_SORTIN 
             # we must modifiy the format to match what CoSort
             # expects.  The CoSort format is comma separated
             # with a space optional.
             # 
             # CoSort does not recognize /dev/null for a non-existent
             # file, so if we are missing any files, we must skip them
             # in the concatenated DD_SORTIN list.
             #
             sav_dd_sortin=`echo $DD_SORTIN`
             concstat=`echo $DD_SORTIN | grep ":" > $DEVNULL`
             concstat=$?
             if [ $concstat = "0" ]
             then
                tmp_xx_sortin=`echo $DD_SORTIN | sed -e 's/:/ /g'`
                have_file="no"
                for file in $tmp_xx_sortin
                do
                   if [ -f $file ]
                   then
                      if [ "$have_file" = "no" ]
                      then
                         tmp_dd_sortin=`echo ${file}`
                         have_file="yes"
                      else
                         tmp_dd_sortin=`echo ${tmp_dd_sortin},${file}`
                      fi
                   fi
                done
                if [ "$have_file" = "yes" ]
                then
                   DD_SORTIN=`echo $tmp_dd_sortin` ; export DD_SORTIN
                else
                   echo "WARNING: No valid DD_SORTIN files found"
                   DD_SORTIN=`echo $sav_dd_sortin` ; export DD_SORTIN
                fi
             fi
             if [ "a$VERBOSE" = "aYES" -o $validate -ne 0 ]
             then
                echo "DD_SORTIN=$DD_SORTIN"
                echo "sortcl /SPEC=$DD_SYSIN"
                echo "sort parameters="
                cat $DD_SYSIN
             fi
             if [ $validate -ne 0 ]
             then
                echo 0 > status.${JON}
                exit 0
             else
                sortcl /SPEC=$DD_SYSIN
                sort_status=$?
                echo $sort_status > status.${JON}
                exit $sort_status
             fi
          else
             #
             # 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
          fi
       ;;
   esac
;;
sortx | SORTX)

   if [ "a$DD_SORTIN" = "a" ]
   then
      DD_SORTIN=$DEVNULL ; export DD_SORTIN
   elif [ ! -f $DD_SORTIN ]
   then
      DD_SORTIN=$DEVNULL ; export DD_SORTIN
   fi
   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
     if [ "$DD_SORTIN" = "$DD_SORTOUT" ]
     then
        #
        # mainframe allows overwrite of input file as output of successful
        # sort.  if sort fails, do not overwrite input file
        #
        DD_SORTOUT=${DD_SORTOUT}.tmp ; export DD_SORTOUT
        if [ "a$VERBOSE" = "aYES" ]
        then
           echo "$SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK"
        fi
        $SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
        sort_status=$?
        if [ "$sort_status" = "0" ]
        then
           mv $DD_SORTOUT $DD_SORTIN
           shell_status=$?
           if [ "$shell_status" != "0" ]
           then
              sort_status=$shell_status
           fi
           DD_SORTOUT=$DD_SORTIN ; export DD_SORTOUT
        fi
     else
        if [ "a$VERBOSE" = "aYES" ]
        then
           echo "$SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK"
        fi
        $SORT_FS -i DD_SORTIN -o DD_SORTOUT -S $DD_SYSIN -t FS -s $LRECL_SORTIN $SORT_WK
        sort_status=$?
     fi
     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 }
