#!/usr/pubsw/bin/bash

#################################################
#
# AUTHOR: Seyon Verdtzabella
# DATE: June 4, 2002
# PROJECT: SSDS Formage Project
#
# NAME: conv
# VERSION: 0.3b
#
# USAGE:
# 
#	conv -l|p [-?] filename
#
# DESCRIPTION: 
# This program converts pipe delimited records in a flatfile
# database into formage logfile format or the other way around.
# Runs on sh, bash or ksh only
# 
# CHANGES THIS VERSION:
# Since the field order in a formage logfile is subject to change
# this conversion script must be enhanced to identify each field 
# of each record before converting to flatfile db format. 
#
# OPTIONS:
# The following command line options are acceptable:
#
#	-l	convert records in a FORMAGE LOGFILE to a  
#		pipe delimited format for a flat file database.
#		The field order may vary when converting from
#		the formage logfile format because formage is not
#		bound to a fixed write order when updating its
#		logfile.  So, there is no way to anticpate the
#		order of the fields when converting the formage
#		logfile into pipe delimited records.  
#		
#		So, the best option is to open the new
#		pipe delimited file in Microsoft Excel and 
#		"cut and paste" the columns to be in the
#		proper order (see -p option) before using the
#		pipe delimited file to convert back to a
#		formage logfile format.
#
#	-p	convert records in a PIP DELIMITED FLATFILE 
#		DATABASE to a formage logfile format.  The field
#		order is fixed and must be:
#			
#		id|datatitle|source|media|callnum|workstation|description
#
#	-?	help notes on program usage
#
#
# APPENDING RECORDS:
# To append converted records to the target file
# use the UNIX append operator >> in your command
# line statement.
#
#	$ conv -p cdrom.txt >> cdrom.db
#
# OVERWRITING RECORDS:
# To rewrite (erase all current records and replace
# with the newly converted records) the target file 
# use the UNIX write operator > in your command
# line statement.
#
#	$ conv -l cdrom.db > cdrom.txt
#
#
# EXIT CODES:
#
#	0 	- no errors
#	1	- target file not found
#	2 	- cannot read filename
#	3	- option does not match current file format
#	5	- no records in target file to convert
#	22 	- insufficient or invalid argument
#
#################################################


##################
# variable setup
program=`basename $0`
dir=`dirname $0`
errorfile="error.log"

##################
# make sure these files are somewhere
# in you HOME directory, readable and that
# the paths below to these files is correct
convawk_l="convawk_l"
convawk_p="convawk_p"


##################
# redirect all standard error
#exec 2>/dev/null


##################
# error log setup
if [ ! -f "$errorfile" ]; then
	>$errorfile

	if [ $? -ne 0 ]; then
		echo "$program: cannot create error log."
		exit 2
	else
		chmod 777 $errorfile
	fi
fi

echo "no errors" > $errorfile


##################
# process command line options

if [ "$#" -eq 0 ]; then
	echo "$program: insufficient arguments" > $errorfile
	exit 22

elif [ "$#" -eq 2 ]; then

	##################
	# check target file

	file=$2

	if [ ! -f "$file" ]; then
		echo "$program: cannot find $file in $dir" > $errorfile
		exit 1
	
	elif [ ! -s "$file" ]; then
		echo "$program: no records in $file to convert" > $errorfile
		exit 5
	
	elif [ ! -r "$file" ]; then
		echo "$program: cannot read $file" > $errorfile
		exit 2

	else
		case "$1" in 
			-l)	shift
				cat "$1" | sed 's/@=/^/' | awk -f $convawk_l 
				exit 0;;
								
			-p)	shift
				awk -f $convawk_p "$1"
				exit 0;;

			*)	echo "$program: insufficient arguments" > $errorfile
				exit 22;;
		esac
	fi

else
	if [ "$1" != "-?" ]; then
		echo "$program: insufficient arguments" > $errorfile
		exit 22
	else
		echo "-----------------------------------------------------"
		echo "-----------------------------------------------------"
		echo ""
		echo "	Usage: conv -l|p [-?] filename"
		echo ""
		echo ""
		echo "DESCRIPTION: "
		echo "This program converts pipe delimited records in a flatfile"
		echo "database into formage logfile format or the other way around."
		echo ""
		echo ""
		echo "OPTIONS:"
		echo "The following command line options are acceptable:"
		echo ""
		echo "	-l	convert records in a FORMAGE LOGFILE to a  "
		echo "		pipe delimited format for a flat file database"
		echo "		The field order may vary when converting from"
		echo "		the formage logfile format because formage is not"
		echo "		bound to a fixed write order when updating its"
		echo "		logfile.  So, there is no way to anticpate the"
		echo "		order of the fields when converting the formage"
		echo "		logfile into pipe delimited records.  "
		echo ""		
		echo "		So, the best option is to open the new"
		echo "		pipe delimited file in Microsoft Excel and" 
		echo "		"cut and paste" the columns to be in the"
		echo "		proper order (see -p option) before using the"
		echo "		pipe delimited file to convert back to a"
		echo "		formage logfile format."
		echo ""
		echo "	-p	convert records in a PIP DELIMITED FLATFILE "
		echo "		DATABASE to a formage logfile format.  The field"
		echo "		order is fixed and must be:"
		echo ""
		echo "		id|datatitle|source|media|callnum|workstation|description"
		echo ""
		echo "	-?	help notes on program usage"
		echo ""
		echo ""
		echo "APPENDING RECORDS:"
		echo "To append converted records to the target file"
		echo "use the UNIX append operator >> in your command"
		echo "line statement."
		echo ""
		echo "	$ conv -p cdrom.txt >> cdrom.db"
		echo ""
		echo ""
		echo "OVERWRITING RECORDS:"
		echo "To rewrite (erase all current records and replace"
		echo "with the newly converted records) the target file "
		echo "use the UNIX write operator > in your command"
		echo "line statement."
		echo ""
		echo "	$ conv -l cdrom.db > cdrom.txt"
		echo ""
		echo ""
		echo "EXIT CODES:"
		echo ""
		echo "	0 	- no errors"
		echo "	1	- target file not found"
		echo "	2 	- cannot read filename"
		echo "	3	- option does not match current file format"
		echo "	5	- no records in target file to convert"
		echo "	22 	- insufficient or invalid argument"
		echo ""
		echo "-----------------------------------------------------"
		echo "-----------------------------------------------------"
		exit 0
	fi 
fi



