#################################################################################################
# .profile_sub_file		(c) R. H. Reepe 12th December 1996	Version 1.0		#
#################################################################################################
# 961212 RHR Genesis

#===========================================================================================
s_prog()			# (c) R. H. Reepe. Returns the Calling Script Name
#===========================================================================================
{
	echo "`basename $0`"
}

#===========================================================================================
s_file()			# (c) R. H. Reepe. Returns a Generic File Name
#===========================================================================================
# Arg_1 File Extension
# Arg_2 Unique Identifier
{
	_file_name="/tmp/db_`s_prog`_$$_$2.$1"
	rm -f $_file_name
	touch $_file_name
	chmod 777 $_file_name
	echo $_file_name
}

#===========================================================================================
s_rmfile()			# (c) R. H. Reepe. Returns a Generic File Name
#===========================================================================================
# Arg_1 [File Extension]
# Arg_2 [Unique Identifier]
{
	rm -f /tmp/db_`s_prog`_$$_${2:-"*"}.${1:-"*"}
}

#===========================================================================================
s_tmp()			# (c) R. H. Reepe. Returns a Temporary File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file tmp ${1:-$$}
}

#===========================================================================================
s_rmtmp()			# (c) R. H. Reepe. Removes all Temporary files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile tmp ${1:-$$}
}

#===========================================================================================
s_log()			# (c) R. H. Reepe. Returns a Log File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file log ${1:-$$}
}

#===========================================================================================
s_rmlog()			# (c) R. H. Reepe. Removes all Log files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile log ${1:-$$}
}
#===========================================================================================
s_sql()			# (c) R. H. Reepe. Returns a sql File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file sql ${1:-$$}
}

#===========================================================================================
s_rmsql()			# (c) R. H. Reepe. Removes all sql files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile sql ${1:-$$}
}
#===========================================================================================
s_dat()			# (c) R. H. Reepe. Returns a dat File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file dat ${1:-$$}
}

#===========================================================================================
s_rmdat()			# (c) R. H. Reepe. Removes all dat files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile dat ${1:-$$}
}
#===========================================================================================
s_ftp()			# (c) R. H. Reepe. Returns a ftp File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file ftp ${1:-$$}
}

#===========================================================================================
s_rmftp()			# (c) R. H. Reepe. Removes all ftp files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile ftp ${1:-$$}
}
#===========================================================================================
s_lst()			# (c) R. H. Reepe. Returns a lst File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file lst ${1:-$$}
}

#===========================================================================================
s_rmlst()			# (c) R. H. Reepe. Removes all lst files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile lst ${1:-$$}
}
#===========================================================================================
s_mnu()			# (c) R. H. Reepe. Returns a mnu File Name
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_file mnu ${1:-$$}
}

#===========================================================================================
s_rmmnu()			# (c) R. H. Reepe. Removes all mnu files
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile mnu ${1:-$$}
}

#===========================================================================================
s_cleanup()			# (c) R. H. Reepe. Removes all Generic files created by PID
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	s_rmfile '*' ${1:-"*"}
}

#===========================================================================================
s_purge()			# (c) R. H. Reepe. Removes ALL Generic files created by db
#===========================================================================================
# Arg_1 [Unique Identifier]
{
	rm -f /tmp/db_*
}

#===========================================================================================
s_plog()			# (c) R. H. Reepe. Returns the Process Log file_name
#===========================================================================================
{
	echo "$ORACLE_LOG/oracle_`s_prog`.log"
}

#===========================================================================================
s_syntax()			# (c) R. H. Reepe. Checks Script Syntax Rules
#===========================================================================================
# Arg_1 = $#	(literally $# in calling script)
# Arg_2-n = List of Required Args
{
	_arg_list=`echo $@ | cut -f2- -d\ `
	if [ "$_arg_list" = "" ]
	then
		_required_args=0
	else
		_required_args=`s_arg_count $_arg_list`
	fi
	if [ $1 != $_required_args ]
	then
		echo ""
		echo "Syntax Error in $0"
		echo "Usage: $0 $_arg_list"
		echo ""
		exit
	else
		break
	fi
}

#===========================================================================================
s_truncate()			# (c) R. H. Reepe. Truncates extension from a file_name
#===========================================================================================
# Arg_1 = FILENAME
{
	_dot=`s_in_string "\." $1`
	_dot=`expr $_dot - 1`
	echo $1 | cut -c1-$_dot
}

#===========================================================================================
s_running()			# (c) R. H. Reepe. Detects if calling script running twice
#===========================================================================================
{
	if [ -f $ORACLE_LOG/"`s_prog`_running" ]
	then
		echo "Script `s_prog` is already running - Aborting"
		exit
	else
		touch $ORACLE_LOG/"`s_prog`_running"
	fi
}

#===========================================================================================
s_stopping()			# (c) R. H. Reepe. Removes "running" flag for script
#===========================================================================================
{
	rm -f $ORACLE_LOG/"`s_prog`_running"
}

#===========================================================================================
s_file_length_lines()		# (c) R. H. Reepe. Returns Count of Lines in FILE
#===========================================================================================
# Arg_1 = filename
{
	_length=`wc -l $1 | cut -c1-8`
	expr $_length + 0
}

#===========================================================================================
s_file_length_words()		# (c) R. H. Reepe. Returns Count of Words in FILE
#===========================================================================================
# Arg_1 = filename
{
	_length=`wc -w $1 | cut -c1-8`
	expr $_length + 0
}

#===========================================================================================
s_file_length_chars()		# (c) R. H. Reepe. Returns Count of Chars in FILE
#===========================================================================================
# Arg_1 = filename
{
	_length=`wc -c $1 | cut -c1-8`
	expr $_length + 0
}

#===========================================================================================
s_permission()			# (c) R. H. Reepe. EXIT's if file not USER's
#===========================================================================================
# Arg_1 = filename
{
	if [ -f $1 ] && [ ! -r $1 ] && [ ! -w $1 ] && [ ! -x $1 ]
	then
		echo "File permissions for $1 are not correct!"
		ls -lisa $1
		exit 1
	else
		_dir=`dirname $1`
		if [ -d $_dir ] && [ ! -r $_dir ] || [ ! -w $_dir ] || [ ! -x $_dir ]
		then
			echo "File permissions for $1 are not correct!"
			ls -lisa $1
			exit 1
		fi
	fi
	return 0
}

#===========================================================================================
s_extract_contents()		# (c) R. H. Reepe. Lists contents of GRIMM Extract
#===========================================================================================
# Arg_1 = EXTRACT_DAY_NUMBER
{
	_file="grimm_extract_day_$1"
	_log="$ORACLE_LOG/oracle_contents.log"
	if [ -f $_file ]
	then
		echo "The contents of file $_file are as follows:"							
>> $_log
		echo "==============================================================================
==========="	>> $_log
		head -2 $_file | tail -1 | cut -c1-90									
>> $_log
		echo "==============================================================================
==========="	>> $_log
		grep "VIEW" $_file | grep "000000" | cut -c1-80								
>> $_log
		echo "==============================================================================
==========="	>> $_log
	else
		echo "File $_file does not exist"
	fi
}