#!/bin/ksh
# touchvi - Author: Edwin Arneson - $Revision: 1.2 $ - $Date: 2007/03/10 19:38:11 $
#	Q&D script to touch a temp file before editing the named file, then
#	touching the newly modified file referencing the temp file to reset the datestamp.
# FORMAT:	touchvi
# CHANGE HISTORY:
#	070215 EJA 1.1	- Created
#	070310 EJA 1.2	- Added the -e flag so that automatic edits can be made to the
#					specified file instead of manually editing it.
# CALLED BY:
# CALLS:
# SEE ALSO:
# SCRIPTS:
# PERMISSIONS:
# HOSTS THIS FILE IS IDENTICAL ON:
# FILES:
# GLOBALS:
# LOCALS:
# COMMANDS:
# INPUT:
# OUTPUT:
# OUTPUT USED BY:
# EXAMPLES:
# HINTS:
# RESTRICTIONS:
# BUGS:
# WARNINGS:
#	40 - The localize script could not be found. Global machine settings can not be set.
# ERROR CODES:
#	51 - An unknown option was passed to the script, or syntax of the parameters is wrong.
#	52 - Unknown edit command >$ecmd<
#	53 - The file >$1< is not writable and, hence, can not be edited.
#	56 - The USAGE was requested using -z or -?.
# DIAGNOSTICS:
# RCS.SH VERSION: 00010
# RCS INFORMATION:
#         $Id: touchvi,v 1.2 2007/03/10 19:38:11 earneson Exp $
#     $Author: earneson $
#       $Date: 2007/03/10 19:38:11 $
#   $Revision: 1.2 $
#     $Source: /Users/cvs/scripts/all/Users/earneson/bin/touchvi,v $
#      $State: Exp $

#######
####### Initialization
#######
# Special check for first parameter of "-x" to turn on tracing of the script execution
if [[ "$1" = "-x" ]] ; then
	set -x
fi

# Find and execute the localization script designed to set common environment variables based name characteristics of the executing computer
binDir=`dirname $0` # get the directory this script is in and check for localize there
if [[ -r "$binDir/localize" ]] ; then
	. $binDir/localize
else
	echo "$0: WARNING 40 - The localize script could not be found. Global machine settings can not be set."
fi

# Ensure these three are set
if [[ -z "$localhost" ]] ; then	localhost="`uname -n`"	; export localhost ; fi
if [[ -z "$awkname" ]] ; then	if [[ -x "/usr/bin/nawk" ]] ; then awkname="nawk" ; else awkname="awk" ; export awkname ; fi ; fi 
if [[ -z "$OStype" ]] ; then	OStype="`uname -s`"		; export OStype ; fi
#######
####### Functions
#######

#######
####### Other variables
#######
basename=`basename $0`
USAGE="$basename restores the original datestamp on a file after editing it
	\$Revision: 1.2 $ - \$Date: 2007/03/10 19:38:11 $
usage: $basename [-e cmd [-d]] [-x]* [-X]*
	<-d> diff the before and after files (-e only)
	<-e cmd> instead of interactively editing a file, perform an edit operation on it, where cmd is:
		delcr - delete carriage return characters
		cr2nl - change carriage return characters to newline characters
	<-x> does 'set -x' (may be used in functions and external script calls)
	<-X> sets debugging level--the number of times -X is specified"
debuglevel=0	# -X: stores the number of times -X is specified (debugging level)
xlevel=0		# -x: stores the number of times -x is specified (for optional "set -x" call in functions) and does 'set -x'
dflag=""		# -d:
ecmd=""			# -e:

#######
####### Parse parameters.
#######
while getopts :de:xXz optchar ; do
	if [[ $debuglevel -ge 1 ]] ; then echo ">optchar=$optchar< >OPTARG=$OPTARG> >OPTIND=$OPTIND<" ; fi
	case $optchar in
		d)	dflag="diff" ;;
		e)	ecmd="$OPTARG" ;;
		x)	xcmd="$xcmd -x" ; ${xcmd:+set -x} ; let "xlevel=$xlevel + 1" ;;	# xcmd stores any -x parameters for passing to other scripts
		X)	Xcmd="$Xcmd -X " ; let "debuglevel=$debuglevel + 1" ;;			# Xcmd stores any -X parameters for passing to other scripts
		\?|z)	echo "$USAGE" >&2 ; exit 56 ;;
		*)	if [[ -z "$OPTARG" ]] ; then
				case "$optchar" in
					?)	errflag="-$optchar" ;;
					+?)	errflag="$optchar" ;;
					*)	errflag="unknown1" ;;
				esac
			else
				errflag="$OPTARG"
			fi
			echo "$basename: ERROR 51 - Unknown option >$errflag<" >&2
			echo "$USAGE" >&2
			exit 51
			;;
	esac
done
shift `expr $OPTIND - 1`

#######
####### Additional checks
#######
if [[ ! -w $1 ]] ; then
	echo "$basename: ERROR 53 - The file >$1< is not writable and, hence, can not be edited." >&2
	echo "$USAGE" >&2
	exit 53
fi

#######
####### Main processing
#######
tmpstr="$$.$RANDOM.$RANDOM"
tmpfile="/tmp/$basename.$tmpstr"
touch -r $1 $tmpfile
case "$ecmd" in
	cr2nl)	tr "\015" "\012" <$1 >$1.$tmpstr 
			cp $1.$tmpstr $1
			rm $1.$tmpstr
			;;
	delcr)	tr -d "\015" <$1 >$1.$tmpstr 
			cp $1.$tmpstr $1
			rm $1.$tmpstr
			;;
	fixmus1)
			case "$1" in
				music_book_catalog-by_book.html) sedcmd="s/XYXYXY/Music Book Catalog--By Music Book/" ;;
				music_book_catalog-by_title.html) sedcmd="s/XYXYXY/Music Book Catalog--By Title|First Line Verse|First Line Chorus/" ;;
			esac
			(
				echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
				tr "\015" "\012" <$1 \
				| sed \
					-e 's%<tr height=13>%<tr>%' \
					-e 's/width=72/width=122/' \
					-e 's/width=347/width=500/' \
					-e 's/width=78/width=118/' \
					-e 's/white-space:nowrap/white-space:wrap/' \
					-e 's/display:none/display:inline/' \
					-e 's%<style>%<title>XYXYXY</title><style type="text/css">%' \
				| sed "$sedcmd"
			) >$1.$tmpstr 
			fsize=`wc -c $1.$tmpstr  | awk '{print $1}'`
			if [[ $fsize -ge 1000 ]] ; then
				if [[ -n "$dflag" ]] ; then
					diff $1.$tmpstr $1
				fi
				cp $1.$tmpstr $1
			else
				echo ""
				echo "$basename: ERROR 54 - The generated file >$1.$tmpstr< is too small. A script error probably occurred and the original file will not be overwritten." >&2
				echo "$USAGE" >&2
				exit 54
			fi
			rm $1.$tmpstr
			;;
	"")		vi -b $1
			;;
	*)		echo "$basename: ERROR 52 - Unknown edit command >$ecmd<" >&2
			echo "$USAGE" >&2
			exit 52
			;;
esac
touch -r $tmpfile $1

#######
####### Cleanup
#######

#######
####### The end.
#######
exit
