#!/bin/sh

#    Dec 08/95 - F.Majaess (Replace 'hostname' calls by "$HOSTID")
#    Oct 19/95 - F.Majaess (Revise for ibm "file" output for binary files)
#    Apr 01/92 - E. Chan: recognize shell scripts as ascii files.
#    Sep 11/91 - E. Chan

#id  copyd   - copies contents of one file into another and emulates Fortran
#id            (or COS) file pointer positioning for ascii text files 

#    AUTHOR  - E. Chan

#hd  PURPOSE - "copyd" appends all lines of ascii text from the input file
#hd            onto the output file and keeps track of the number of lines
#hd            copied. This emulates the file pointer positioning of Fortran
#hd            (or COS). If the input file does not contain ascii text, the 
#hd            contents are simply copied into the output file using the 
#hd            standard UNIX cp operation. This means that all non-ascii files
#hd            are always automatically rewound by UNIX after they are closed.
#hd 
#hd            If the input file is not specified, the file 'Input_Cards' is
#hd            used as the default. 'Input_Cards' is a file which contains the
#hd            input card images for all CCRN programs invoked in a job. If 
#hd            the output file is not specified, then standard output is 
#hd            used instead.
#hd 
#hd            "Copyd" emulates Fortran (or COS) file pointer positioning
#hd            by accumulating all the copied lines in a temporary file
#hd            and replacing the input file with a null file. The temporary
#hd            file is necessary in the restoration of the original input file
#hd            if a subsequent "rewind" operation is executed.
#hd             
#hd            This script is designed to emulate the ascii text handling
#hd            capabilities of the "copyd" operation under the Cray Operating 
#hd            System.
   
#pr  PARAMETERS:
#pr 
#pr    PRIMARY
#pr 
#pr      i      = name of input file (='Input_Cards')
#pr      o      = name of output file (default: standard output) 

#ex  EXAMPLE:
#ex    
#ex   1) copyd i=file1 o=file2 
#ex 
#ex      The above example copies all lines from 'file1' to the end of 
#ex      'file2'.   

#  * Reset field separators (otherwise those defined in the parent process
#  * will be used and these may cause problems if they include special 
#  * characters used in this script). 

IFS=' '
export IFS

#  * Obtain the file names and any specified option.
 
arg_list=$@
for arg in $arg_list
do 
  case $arg in
    i=*)  eval "$arg"      ;;
    o=*)  eval "$arg"      
  esac
done

#  * Set the defaults. 

i=${i:=Input_Cards}
o=${o:=Standard_Output} 

#  * Verify that the input file exists.

if [ ! -f "$i" ] ; then
  echo "Abort in COPYD: file $i does not exist"
  exit 1
fi

#
#  * Case 1: Non-ascii file.
#

#  * Perform a standard UNIX cp operation if the data is not ascii text.

info=`file $i 2>/dev/null `
infox=`expr "$info" : '.*\(data.*\).*'`
if [ "$infox" != 'data or International Language text' ] ; then
 file_status=`expr "$info" : '.*\(text\).*'`
 file_status=${file_status:=`expr "$info" : '.*\(character\).*'`}
 file_status=${file_status:=`expr "$info" : '.*\(script\).*'`}
fi

if [ -z "$file_status" ] ; then

  if [ "$o" != 'Standard_Output' ] ; then
    cp $i $o
    echo "Warning in COPYD: non-ascii data in file $i, UNIX cp"
    echo "                  performed instead." 
    exit 0
  else
    echo "Abort in COPYD: cannot copy non-ascii data in file $i"
    echo "                to standard output."
    exit 1
  fi 

fi

#
#  * Case 2: Ascii text file. 
#

#  * Generate output.

if [ "$o" != 'Standard_Output' ] ; then 
  cat $i >> $o
else
  cat $i 
fi 

#  * Emulate Fortran (or COS) file pointer positioning by appending the 
#  * input file into a temporary file of the same name but with a 
#  * '_Copied_Lines' extension added at the end. This new temporary file is 
#  * necessary if the original file is to be restored later with a "rewind".

#  * This section is not done if input is taken from the file 'Input_Cards'
#  * (used as the equivalent of COS standard input) because COS standard
#  * input cannot be rewound.

if [ "$i" != 'Input_Cards' ] ; then
  cat $i >> ${i}_Copied_Lines
fi

#  * Replace the input file with a null file.

cp /dev/null $i

exit

