#!/bin/sh

#
# Script to install tpctl and its friends
#
# Sets options and path to modules in /etc/modules.conf or in /etc/conf.modules
# Adds path to libraries to library search path
#

DIR_MODDIR=$1
DIR_LIB=$2

if [ "$DIR_MODDIR" = "" ]; then
	DIR_MODDIR='/lib/modules/`uname -r`' ;
fi

if [ "$DIR_LIB" = "" ]; then
	DIR_LIB="/usr/local/lib" ;
fi

which perl >> /dev/null && which grep >> /dev/null
if [ ! $? = 0 ]; then
	echo "tpctlify: error: could not run perl or grep"
	echo "tpctlify: please make these available on root's path and run tpctlify again"
	echo 'tpctlify: aborting.'
	exit 1 ;
fi

###### Set up path, alias and options for thinkpad module in modules config file ######

### Find the config file

if [ -f /etc/modules.conf ]; then
	FILE_MODCONF=/etc/modules.conf ;
elif [ -f /etc/conf.modules ]; then
	FILE_MODCONF=/etc/conf.modules ;
else
	echo 'tpctlify: neither /etc/modules.conf nor /etc/conf.modules found.'
	echo 'tpctlify: please create one of these files and run tpctlify again.'
	echo 'tpctlify: aborting.'
	exit 1 ;
fi

echo "tpctlifying $FILE_MODCONF ..."

### Back up the current file

cp -fp ${FILE_MODCONF} ${FILE_MODCONF}-prev

### Strip out old path and alias lines

perl -e '$lastwaskeep = 0; while (<STDIN>) { print if ! /path\[sampi\]/ && ! /path\[thinkpad\]/ && ! /path\[tp600\]/ && ! /char-major-10-154/ && ! /char-major-10-168 +smapi/ && ! /char-major-10-168 +thinkpad/ && ! /char-major-10-170/ && ! ( /keep/ && $lastwaskeep ) && ! ( /^\s*$/ && $lastwasblank ); $lastwaskeep = ( /keep/ || ( $lastwaskeep && /^\s*$/ )); $lastwasblank = /^\s*$/; }' < ${FILE_MODCONF}-prev > ${FILE_MODCONF}-new

# may leave one dangling "keep" but that's okay I guess

echo "" >> ${FILE_MODCONF}-new

### Add 'options thinkpad' line if none is there

grep '^[[:space:]]*\options[[:space:]]*thinkpad' ${FILE_MODCONF} >> /dev/null
if [ ! $? = 0 ]; then
	echo "options thinkpad enable_smapi=1 enable_superio=1 enable_rtcmosram=1  # Added by tpctlify" >> ${FILE_MODCONF}-new ;
	echo "tpctlify: added options line for thinkpad module to ${FILE_MODCONF}"
fi

### Add correct path and alias lines

echo "keep     # Added by tpctlify" >> ${FILE_MODCONF}-new
echo "path[thinkpad]=${DIR_MODDIR}    # Added by tpctlify" >> ${FILE_MODCONF}-new
echo "alias char-major-10-170 thinkpad    # Added by tpctlify" >> ${FILE_MODCONF}-new

echo "" >> ${FILE_MODCONF}-new

echo "tpctlify: added new path and alias for thinkpad module to ${FILE_MODCONF}"

mv -f ${FILE_MODCONF}-new ${FILE_MODCONF}

echo "tpctlify: the old ${FILE_MODCONF} was saved as ${FILE_MODCONF}-prev ."


###### Set up path to the library in ld config file

FILE_LDCONF=/etc/ld.so.conf

if [ ! -f ${FILE_LDCONF} ]; then
	echo "tpctlify: the file ${FILE_LDCONF} was not found."
	echo "tpctlify: please create the file and run tpctlify again."
	echo 'tpctlify: aborting.'
	exit 1 ;
fi

echo "tpctlifying $FILE_LDCONF ..."

cat ${FILE_LDCONF} | grep -e ^${DIR_LIB} >> /dev/null

if [ $? = 1 ]; then
	cp -vpf ${FILE_LDCONF} ${FILE_LDCONF}-prev
	cp -pf ${FILE_LDCONF} ${FILE_LDCONF}-new
	echo "" >> ${FILE_LDCONF}-new
	echo "${DIR_LIB}    # Added by tpctlify" >> ${FILE_LDCONF}-new
	mv -f ${FILE_LDCONF}-new ${FILE_LDCONF}
	echo "tpctlify: the path '${DIR_LIB}' has been added to ${FILE_LDCONF} ."
	echo "tpctlify: the old ${FILE_LDCONF} was saved as '${FILE_LDCONF}-prev'."
	echo "tpctlify: executing the command \"ldconfig\" in order to update your ld path..." ;
	ldconfig
else
	echo "tpctlify: the path '${DIR_LIB}' is already in ${FILE_LDCONF}.  Good!" ;
fi

echo "tpctlify: done."

