#! /bin/sh
#
#this script checks in some known places for a version of
#libpam_misc.so and then updates a specified file with this 
#library.  the specified file must have a token 'LD_PRELOAD=@@@'
#which gets replaced with 'LD_PRELOAD=<pam library path>'.
#this is used for 'smwd' to make sure the libary can be loaded
#and login can occur.
#
#written by Chris Chisholm 10/06/2005
#property of SyAM Software
#
#

#check to make sure a command-line argument is given
if [ $# -ne 1 ] 
then 
	#echo "Usage: findPamLib <file>" 
	exit
fi

#make sure that argument is a valid file
ls $1 > /dev/null 2> /dev/null
ret=$?
if [ $ret -ne 0 ]
then
	#echo "Invalid file specified"
	exit
fi

#echo "---------------------------------"

#function to determine if a directory contains some usable version
#of libpam_misc.so
check_dir()
{
	dir=$1

	#echo "looking in $dir for libpam_misc..."


	result=`find $dir -name libpam_misc* -print` #get 'find' command output
	c=($result) #break results into an array
	if [[ "${c[0]}" == *libpam_misc* ]] #library was found
	then
		lib=${c[0]}
		#echo "pam library found: $lib"
		update_file $lib $2
	fi
	#echo "library not found"
}

#function to replace LD_PRELOAD=@@@ with LD_PRELOAD=<discovered pam library>
update_file()
{
	val=$1
	file=$2

	#echo "updating $file with $val..."

	sed s%LD_PRELOAD=@@@%LD_PRELOAD=$val% $file > /etc/null 2> /etc/null >> templqpazf && mv -f templqpazf $file
	chmod 777 $file
	#echo "---------------------------------"
	exit
}

#determine where a version of libpam_misc.so is located
#this checks in a few known places to look for it

check_dir "/lib" $1
check_dir "/usr/lib" $1
check_dir "/lib64" $1
check_dir "/usr/lib64" $1

#echo "no libraries found, please set manually in $1"
#echo "---------------------------------"

