# This script removes every Carriage Return (hex 0D) and End Of File mark
# (hex 1A) from a file.  It is used to convert MS-DOS files to UNIX format.
# UNIX uses Line Feeds only (hex 0A) as as line delimiters.
#
 
echo
echo "Sunbelt UNIX rmcr Command Script   Version 10.6    02 Oct 2023"
echo "(C) Copyright 2018-2023   Sunbelt Computer Software.   N999999"
echo "(C) Copyright 1987-2023   Steve White\\n"

if [ "$1" = "-?" -o $# = 0 ]
 then
  echo "rmcr - remove Carriage Returns (hex 0D) & End Of File marks (hex 1A) in"
  echo "   files copied from MS-DOS to UNIX.  The files are converted in place."
  echo
  echo "Options: -ff - Remove Form Feed characters also"
  echo
  echo "example:  rmcr progname.dbs   (A specific program to convert)"
  echo "          rmcr *.dbs          (All files with an extension .dbs)"
  echo "          rmcr -ff *.lst      (Removes Form Feeds also)"
  echo
  exit 1
fi
 
DELCHR="\015\032"
 
NEXT=1
if [ "$1" = "-ff" ]
  then
    DELCHR="\014\015\032"
    shift
    echo "Deleting Form Feed characters also"
    echo
  else
    NEXT=0
fi
 
while [ $# -ge 1 ]
  do
    a="-"
    if [ -f $1$a ]
      then
        a="."
        if [ -f $1$a ]
          then
            a=".-"
            if [ -f $1$a ]
              then
                a="-."
                if [ -f $1$a ]
                  then
                    a="--"
                    if [ -f $1$a ]
                      then
                        a=".."
                        if [ -f $1$a ]
                          then
                            echo Unable to create temporary file for $1.
                            shift
                            continue
                        fi
                    fi
                fi
            fi
        fi
    fi
    if [ -f $1 -a -r $1 -a -w $1 ]
      then
        tr -d "$DELCHR" < $1 > $1$a
        if [ $? = 0 ]
          then
            mv $1$a $1
            if [ $? = 0 ]
              then
                echo $1 successfully converted!
            else
                echo Unable to complete the conversion of $1 - Check $1$a.
            fi
        else
            echo Unable to convert $1 - Check access privileges for $1$a.
        fi
    else
        echo Cannot convert $1 - Check for valid file name or access privileges.
    fi
  shift
done
