Newsgroups: comp.binaries.apple2
Path: news.uiowa.edu!hobbes.physics.uiowa.edu!math.ohio-state.edu!news.cyberstore.ca!nntp.cs.ubc.ca!unixg.ubc.ca!acs.ucalgary.ca!cpsc.ucalgary.ca!debug!griffin!dockery
From: dockery@griffin.cuc.ab.ca (Sean Dockery)
Subject: UNIX based text conversion script (atou)
Message-ID: <CHAKEI.9pB@griffin.cuc.ab.ca>
Organization: Griffin Software Development
Date: Tue, 30 Nov 1993 06:23:06 GMT
Lines: 182

The following is based on a much older script that I had.  It was lost
and I decided to rewrite it from scratch.  As a result, I own the
copyright to this one.  :-)

This script automates a couple conversion for text file owners.

-it will mask bit 7 of all bytes in a file
-it will convert newline characters between the following systems:
  apple to unix, unix to apple, apple to ms-dos, ms-dos to apple, unix
  to ms-dos, and ms-dos to unix.

It requires a copy of basename, find, mv, sh, and tr on your system.

Place this in your private bin directory under the name atou.
------------------------------------------------------------------------
#!/bin/sh

########################################################################
# atou (convert apple to unix)
#
# Copyright (C) 1993 by Sean Dockery.
#
# Description:
#  This script will convert the following files when invoked with a given
#  command name:
#
#  atou: Apple newlines to UNIX newlines
#  utoa: UNIX newlines to Apple newlines
#  atom: Apple newlines to MS-DOS newlines
#  mtoa: MS-DOS newlines to Apple newlines
#  utom: UNIX newlines to MS-DOS newlines
#  mtou: MS-DOS newlines to UNIX newlines
#  maketext: binary characters to text characters
#
# Installation:
#  You require the following commands to be present on your UNIX
#  system to use this script:
#      -basename
#      -find
#      -mv
#      -sh
#      -tr
#
#  Install this script in your own $HOME/bin directory--make sure that
#  $HOME/bin is part of your $PATH variable (preferably located early
#  in the $PATH priority,) under the name 'atou'.  Create soft links
#  to this script by using the 'ln' command.
#
#  Example:
#    $ ln -s atou utoa
#
#  Do this for all of the above named commands (utoa, atom, etc ...)
#  If your UNIX system does not have a link command, use 'cp' instead.
#
# Syntax:
#    $ atou [-v] [-c] <filespec>
#  or
#    $ cat foo | atou [-v] -
#
# Primary Options:
#        -v: Give terse diagnostic output.
#   default: Normally no diagnostic output is given.
#
# Secondary Options:
#         -: Input will be taken from stdin and output directed to stdout.
#             No files may be declared when this option is used.
#        -c: Input will be taken from files and output directed to stdout.
#             Files must also be declared with this option.
#
# Bugs and Limitations:
#  This script will reset the file permissions of the file(s) that it
#  acts upon to the user's umask value.
#
#  This script also has no way to know if a file that it is dealing with
#  is binary in nature; such files will always be corrupted as a result.
#
########################################################################

# Determine the base command name.
command=`basename $0`

# Determine (based on the command name) which type of conversion that
# we will be undertaking.
case $command in
        atou) # Apple to UNIX
              oct1="'\015'"
              oct2="'\012'"
              ;;
        utoa) # UNIX to Apple
              oct1="'\012'"
              oct2="'\015'"
              ;;
        atom) # Apple to MS-DOS
              oct1="'\015'"
              oct2="'\015\012'"
              ;;
        mtoa) # MS-DOS to Apple
              oct1="'\015\012'"
              oct2="'\015'"
              ;;
        utom) # UNIX to MS-DOS
              oct1="'\012'"
              oct2="'\015\012'"
              ;;
        mtou) # MS-DOS to UNIX
              oct1="'\015\012'"
              oct2="'\012'"
              ;;
    maketext) # binary to text
              oct1="'[\201-\377]'"
              oct2="'[\001-\177]'"
              ;;
           *) # unknown
              echo "$command: unknown function \"$command\""
              exit 1
              ;;
esac

# Determine if we are to be quiet or loud.
verbose=0
case $1 in
    -v) # verbose mode
        verbose=1
        shift
        ;;
esac

# Determine if we are in stdin, stdout, or file mode.
if ( [ $# -ne 0 ] && [ $1 = '-c' ] )
    then
        stdin=0
        stdout=1
        shift
    elif ( [ $# -ne 0 ] && [ $1 = '-' ] )
        then
            stdin=1
            stdout=1
            shift
    else
            stdin=0
            stdout=0
fi

# Make sure we have the correct number of arguements for the mode we are in.
if ( [ $stdin -eq 1 ] && [ $# -gt 0 ] ) || ( [ $stdin -eq 0 ] && [ $# -eq 0 ] )
    then
        echo "usage: $command [-v] [-c] <filespec>, or cat <filespec> | $command [-v] -"
        exit 1
fi

if [ $stdin -eq 0 ]
    then
        for file in $*
            do
                if [ `find . -name $file -print` ]
                    then
                        if [ -f $file ]
                            then
                                if [ $stdout -eq 0 ]
                                    then
                                        mv $file "$command_$$"
                                        [ $verbose -eq 1 ] && echo -n "$command: $file..."
                                        tr "$oct1" "$oct2" < "$command_$$" > $file
                                        rm "$command_$$"
                                        [ $verbose -eq 1 ] && echo "done."
                                    else
                                        tr "$oct1" "$oct2" < $file
                                fi
                            else
                                [ $verbose -eq 1 ] && echo "$command: $file is a directory."
                        fi
                    else
                        [ $verbose -eq 1 ] && echo "$command: $file does not exist."
                fi
        done
    else
        tr "$oct1" "$oct2"
fi
------------------------------------------------------------------------
-- 
Sean Dockery
dockery@griffin.cuc.ab.ca
