#!/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 24/91 - E. Chan

#id  copyr   - copies contents of one file into another with extra features
#id            to handle lines of ascii text and emulation of Fortran 
#id            (or COS) file pointer positioning

#    AUTHOR  - E. Chan

#hd  PURPOSE - "copyr" appends a specified number of lines of ascii text from 
#hd            the input file onto the output file and keeps track of the 
#hd            number of lines copied. This emulates the file pointer 
#hd            positioning of Fortran (or COS). If the input file does not
#hd            contain ascii text, the contents are simply copied into the 
#hd            output file using the standard UNIX cp operation. This means
#hd            that all non-ascii files are always automatically rewound by 
#hd            UNIX after they are closed. 
#hd             
#hd            The strings '*EOR' and '*EOF' are recognized by the script as
#hd            partitions separating different sections of text. "Copyr"  
#hd            copies text up to but not including these strings. 
#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            "Copyr" emulates Fortran (or COS) file pointer positioning
#hd            by accumulating the copied lines in a separate file and
#hd            replacing the original input file with the remaining uncopied
#hd            lines. Subsequent copy operations on the same input file name
#hd            will result in the further editing of these two files.
#hd            A subsequent "rewind" operation is just a concatenation of 
#hd            the two files back into the original file.  
#hd 
#hd            This script is designed to emulate the ascii text handling 
#hd            capabilities  of the "copyr" operation under the Cray 
#hd            Operating System.

#pr  PARAMETERS:
#pr 
#pr    PRIMARY
#pr 
#pr      i      = name of input file (='Input_Cards')
#pr      o      = name of output file (default: standard output)
#pr  
#pr    PRIMARY/SECONDARY
#pr 
#pr      nr     = number of lines to be copied -- also may be used as a
#pr               switch to enable copying of all lines up to but not 
#pr               including the strings '*EOR' or '*EOF' (=1/'nr')

#ex  EXAMPLES:
#ex    
#ex   1) copyr i=file1 o=file2 nr=3
#ex 
#ex      The above example copies three lines from 'file1' to the end of 
#ex      'file2'. 'File1' is left with the first three lines stripped away
#ex      (assuming that the strings '*EOR' or '*EOF' are not encountered). 
#ex    
#ex   2) copyr i=file1 nr
#ex 
#ex      The above example copies all lines up to but not including the
#ex      strings '*EOR' or '*EOF' from 'file1' to standard output.
#ex 
#ex   3) copyr o=file2
#ex 
#ex      The above example copies one line from the file 'Input_Cards'
#ex      to '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"                             ;;
    nr=*) nr=`expr $arg : 'nr=\([0-9]*\)'`        ;;
      nr) nr='nr'
  esac
done

#  * Set the defaults. 

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

#  * Verify that the input file exists.

if [ ! -f "$i" ] ; then
  echo "Abort in COPYR: 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

    echo "Abort in COPYR: cannot copy non-ascii data in file $i"
    echo "                to standard output."
    exit 2

  elif [ "$nr" != 'nr' ] ; then

    echo "Abort in COPYR: cannot do a partial copy of non-ascii data" 
    echo "                from file $i to file $o."
    exit 3

  else

    cp $i $o
    echo "Warning in COPYR: non-ascii data in file $i, UNIX cp"
    echo "                  performed instead." 
    exit 0

  fi 

fi

#
#  * Case 2: Ascii text file. 
#

#  * Emulate Fortran (or COS) file pointer positioning by splitting up 
#  * the input file into two parts. The first part contains the lines
#  * to be copied and the second part contains the lines that will
#  * replace those in the input file.

#  * Generate a sed script to split the input file. Lines are copied into
#  * file 'Part1' up to but not including the strings '*EOR' or '*EOF'. 
#  * Remaining lines are copied into file 'Part2'.

  cat > Sed_script <<..End
  /^\*EO[RF]/,\$b label
  w Part1
  b                                 
  :label
  w Part2
..End

if [ "$nr" != 'nr' ] ; then

  #  * Case 2a: Copy 'nr' number of lines up to but not including the 
  #  *          strings '*EOR' and '*EOF'. In this case, an extra line must
  #  *          be added to the beginning of the sed script to ensure that
  #  *          no more than 'nr' number of lines are copied.

  sed -n -e "`expr $nr + 1`,\$b label" -f Sed_script $i
    
else

  #  * Case 2b: Copy all lines up to but not including '*EOR' or '*EOF'.

  sed -n -f Sed_script $i

fi

#  * Generate output.

if [ "$o" != 'Standard_Output' ] ; then 
  cat Part1 >> $o
else
  cat Part1 
fi 
 
#  * 'Part2' is moved into the original input file and 'Part1' is
#  * appended onto a file generated with the original input filename and the
#  * extension '_Copied_Lines'. A COS rewind operation is then just a
#  * concatenation of these two files back together again. 
 

#  * If input is taken from the file 'Input_Cards' (used as the equivalent
#  * of COS standard input) then there is no need to keep the copied lines
#  * because COS standard input cannot be rewound.

mv Part2 $i

if [ "$i" != 'Input_Cards' ] ; then
  cat ${i}_Copied_Lines Part1 > Part2 2> /dev/null
  mv Part2 ${i}_Copied_Lines
fi

#  * Remove scratch files.

rm -f Sed_script Part*

exit
