#!/bin/bash

###########################################################################
# Copyright (c) 2004-2005 Hai Zaar and Gil Ran                            #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of version 2 of the GNU General Public License as       #
# published by the Free Software Foundation.                              #
#                                                                         #
###########################################################################

# Configurator for ldbash
# It creates the .cache file for ldbash (usually /etc/ldbash.cache)

prefix=/usr
exec_prefix=${prefix}

LD_BASH_PATH=${exec_prefix}/lib/bash

LD_BASH_CACHE=/etc/ldbash.cache

# We want to use getopt_long...
source $LD_BASH_PATH/getopts.sh
source $LD_BASH_PATH/hashstash.sh

#############################################################
###################       FUNCTIONS      ####################
#############################################################

#
# $retval ldbashconfig_returnDuplicates $retval returnDuplicates <arr>
#
#	Finds all the duplicated values in arr and returns them.
#
#	Parameters:
#		arr		- An array.
#	Return value:
#		A list of all the values that apear at least twice in the array.
returnDuplicates()
{
	retval=""

	# Set a value to $CurrVal, so it will enter the loop
	local CurrVal=1
	
	while [[ $CurrVal ]] ; do
		# Set the next value of the array in $CurrVal
		CurrVal=$1

		# Remove the next value from the array
		shift

		# Check if the value apears in the rest of the array
		if [[ " $* " = *" $CurrVal "* ]] ; then
			# Add the value to the retval
			retval="${retval} ${CurrVal}"
		fi
	done

	# Prevent duplications in the retval
	retval=`echo $retval | awk '{for (i=1;i<=NF;i++) print $i}' | sort -u`
}

##############################################################
##################          MAIN          ####################
##############################################################

getopt_long '-p|--prefix->LIBS_PREFIX
			 -v|--version->Version
			 -h|--help->Help' $*
eval $retval

if [[ $Help ]] ; then
	echo "usage: ldbashconfig"
	echo "		[-h|--help]"
	echo "		[-v|--version]"
	echo "		[-p|--prefix]"
	[[ $Version ]] || exit
fi

if [[ $Version ]] ; then
	echo "libbash 0.9.10b"
	echo "Writen by Gil Ran and Hai Zaar"
	exit
fi

if [[ $LIBS_PREFIX ]] ; then
	echo $LD_BASH_PATH
	exit
fi

# Keep the existing file (for a case there will be errors)
cp -f $LD_BASH_CACHE ${LD_BASH_CACHE}.prev$$ 2> /dev/null || echo "" > $LD_BASH_CACHE ${LD_BASH_CACHE}.prev$$

# grep the lines that define exports					(output looks like: ./script.sh: # EXPORT=func1 func2	)
# replace the .sh<white-space>#<white-space> with a _ 	(output looks like: ./script_EXPORT=func1 func2			)
# replace the dirname of the script with a _			(output looks like: _script_EXPORT=func1 func2			)
# replace the = with ="<space>							(output looks like: _script_EXPORT=" func1 func2		)
# put <space>" at the end of the line					(output looks like: _script_EXPORT=" func1 func2 "		)
grep -H EXPORT= $LD_BASH_PATH/*.sh | 		\
	sed -e 's/\.sh:[ |\t]*#[ |\t]*/_/g'	\
		-e 's/^.*\///g' 				\
		-e 's/=/=" /g'					\
		-e 's/$/ "/g' 						> $LD_BASH_CACHE

# This does the same for requirements
grep -H REQUIRE= $LD_BASH_PATH/*.sh | 		\
	sed -e 's/\.sh:[ |\t]*#[ |\t]*/_/g'	\
		-e 's/^.*\///g' 				\
		-e 's/=/=" /g' 					\
		-e 's/$/ "/g'						>> $LD_BASH_CACHE

# Get the names of all the functions exported in the file.
# Step by step:
# Get the lines that define EXPORT variables
# Take the value of the variable
# Remove any " from the result
AllFuncs=( `grep _EXPORT $LD_BASH_CACHE	|
			awk -F= '{print $2}' 		|
			sed -e 's/"//g'`)

# Check if any duplications found
returnDuplicates ${AllFuncs[*]}
if [[ $retval ]] ; then
	echo "Error - duplicated definition of:"
	echo "	$retval"
	echo "Restoring old configuration"
	cp -f ${LD_BASH_CACHE}.prev$$ $LD_BASH_CACHE 
fi

# Remove the old file
rm -f ${LD_BASH_CACHE}.prev$$
