|
|
1a066f |
#!/bin/bash
|
|
|
1a066f |
#
|
|
|
1a066f |
# createmodule.sh - Takes the name of a environment init script and
|
|
|
1a066f |
# produces a modulefile that duplicates the changes made by the init script
|
|
|
1a066f |
#
|
|
|
1a066f |
# Copyright (C) 2010-2012 by Orion E. Poplawski <orion@cora.nwra.com>
|
|
|
1a066f |
#
|
|
|
1a066f |
# This program is free software: you can redistribute it and/or modify
|
|
|
1a066f |
# it under the terms of the GNU General Public License as published by
|
|
|
1a066f |
# the Free Software Foundation, either version 2 of the License, or
|
|
|
1a066f |
# (at your option) any later version.
|
|
|
1a066f |
|
|
|
1a066f |
# This program is distributed in the hope that it will be useful,
|
|
|
1a066f |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
1a066f |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
1a066f |
# GNU General Public License for more details.
|
|
|
1a066f |
|
|
|
1a066f |
# You should have received a copy of the GNU General Public License
|
|
|
1a066f |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
1a066f |
|
|
|
1a066f |
usage="Usage: $0 [-p prefix] <initscript> [args]"
|
|
|
1a066f |
|
|
|
1a066f |
usage() {
|
|
|
1a066f |
echo $usage 1>&2
|
|
|
1a066f |
exit 1
|
|
|
1a066f |
}
|
|
|
1a066f |
|
|
|
1a066f |
while getopts "p:" opt
|
|
|
1a066f |
do
|
|
|
1a066f |
case $opt in
|
|
|
1a066f |
p) prefix=$OPTARG; shift 2;;
|
|
|
1a066f |
*) usage;;
|
|
|
1a066f |
esac
|
|
|
1a066f |
done
|
|
|
1a066f |
|
|
|
1a066f |
# Need a script name
|
|
|
1a066f |
[ -z "$1" ] && usage
|
|
|
1a066f |
|
|
|
1a066f |
# Need to be a readable script
|
|
|
1a066f |
if [ ! -r "$1" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
echo "ERROR: Cannot read $1" 1>&2
|
|
|
1a066f |
exit 1
|
|
|
1a066f |
fi
|
|
|
1a066f |
|
|
|
1a066f |
#Will print out array assignment list
|
|
|
1a066f |
printenvarray () {
|
|
|
1a066f |
env | while read x
|
|
|
1a066f |
do
|
|
|
1a066f |
key=${x%%=*}
|
|
|
1a066f |
value=${x#*=}
|
|
|
1a066f |
echo [$key]="'$value'"
|
|
|
1a066f |
done
|
|
|
1a066f |
}
|
|
|
1a066f |
|
|
|
1a066f |
#Apparently we need to declare the associative arrays
|
|
|
1a066f |
declare -A env1 env2
|
|
|
1a066f |
|
|
|
1a066f |
#Record starting environment
|
|
|
1a066f |
eval env1=(`printenvarray`)
|
|
|
1a066f |
|
|
|
1a066f |
#Source the environment script
|
|
|
1a066f |
. "$@"
|
|
|
1a066f |
|
|
|
1a066f |
#Record ending environment
|
|
|
1a066f |
eval env2=(`printenvarray`)
|
|
|
1a066f |
|
|
|
1a066f |
#Print out the modulefile
|
|
|
1a066f |
echo "#%Module 1.0"
|
|
|
1a066f |
|
|
|
1a066f |
#Prefix
|
|
|
1a066f |
[ -n "$prefix" ] && echo -e "\nset prefix $prefix\n"
|
|
|
1a066f |
|
|
|
1a066f |
#Subshell so we can sort the output
|
|
|
1a066f |
(
|
|
|
1a066f |
dedup() {
|
|
|
1a066f |
list=`mktemp`
|
|
|
1a066f |
echo $1 | sed -r -e 's,[^/]+/\.\./,,g' -e 's,[^/]+/\.\./,,g' -e 's/:/\n/g' |
|
|
|
1a066f |
while read x
|
|
|
1a066f |
do
|
|
|
1a066f |
grep -Fx ${x} $list && continue
|
|
|
1a066f |
if [ -n "$prefix" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
echo $x | sed -e s,$prefix,\$prefix,
|
|
|
1a066f |
else
|
|
|
1a066f |
echo $x
|
|
|
1a066f |
fi
|
|
|
1a066f |
echo $x >> $list
|
|
|
1a066f |
done | tr '\n' : | sed -e 's/:$//'
|
|
|
1a066f |
rm $list
|
|
|
1a066f |
}
|
|
|
1a066f |
|
|
|
1a066f |
#Keys that changed
|
|
|
1a066f |
for key in "${!env1[@]}"
|
|
|
1a066f |
do
|
|
|
1a066f |
if [ "${env1[$key]}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
#Working directory change
|
|
|
1a066f |
if [ "$key" = PWD ]
|
|
|
1a066f |
then
|
|
|
1a066f |
if [ -n "$prefix" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
echo -e "chdir\t\t${env2[PWD]}" | sed -e s,$prefix,\$prefix,g
|
|
|
1a066f |
else
|
|
|
1a066f |
echo -e "chdir\t\t${env2[PWD]}"
|
|
|
1a066f |
fi
|
|
|
1a066f |
#Test for delete
|
|
|
1a066f |
elif [ -z "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
echo -e "unsetenv\t${key}\t${env2[$key]}"
|
|
|
1a066f |
#Test for prepend
|
|
|
1a066f |
elif [ "${env2[$key]%${env1[$key]}}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
added=$(dedup ${env2[$key]%:${env1[$key]}})
|
|
|
1a066f |
echo -e "prepend-path\t$key\t${added}"
|
|
|
1a066f |
#Test for prepend plus : added at end (MANPATH)
|
|
|
1a066f |
elif [ "${env2[$key]%${env1[$key]}:}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
added=$(dedup ${env2[$key]%${env1[$key]}:})
|
|
|
1a066f |
echo -e "prepend-path\t$key\t${added}"
|
|
|
1a066f |
#Test for append
|
|
|
1a066f |
elif [ "${env2[$key]#${env1[$key]}}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
added=$(dedup ${env2[$key]#:${env1[$key]}})
|
|
|
1a066f |
echo -e "append-path\t$key\t${added}"
|
|
|
1a066f |
#Test for prepend plus append
|
|
|
1a066f |
elif [ "${env2[$key]%${env1[$key]}:*}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
added=$(dedup ${env2[$key]%:${env1[$key]}*})
|
|
|
1a066f |
echo -e "prepend-path\t$key\t${added}"
|
|
|
1a066f |
added=$(dedup ${env2[$key]#*${env1[$key]}:})
|
|
|
1a066f |
echo -e "append-path\t$key\t${added}"
|
|
|
1a066f |
else
|
|
|
1a066f |
#Unhandled
|
|
|
1a066f |
echo "Unhandled change of $key" 1>&2
|
|
|
1a066f |
echo "Before <${env1[$key]}>" 1>&2
|
|
|
1a066f |
echo "After <${env2[$key]}>" 1>&2
|
|
|
1a066f |
fi
|
|
|
1a066f |
fi
|
|
|
1a066f |
#Delete keys we've handled
|
|
|
1a066f |
unset env1[$key]
|
|
|
1a066f |
unset env2[$key]
|
|
|
1a066f |
done
|
|
|
1a066f |
|
|
|
1a066f |
#New keys
|
|
|
1a066f |
for key in "${!env2[@]}"
|
|
|
1a066f |
do
|
|
|
1a066f |
if [ "$key" = OLDPWD ]
|
|
|
1a066f |
then
|
|
|
1a066f |
continue
|
|
|
1a066f |
fi
|
|
|
1a066f |
#Use prepend-path for new paths
|
|
|
1a066f |
if [ "${key/PATH/}" != "$key" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
# TODO - Need to handle stripping of default MANPATH
|
|
|
1a066f |
echo -e "prepend-path\t${key}\t"$(dedup ${env2[$key]})
|
|
|
1a066f |
else
|
|
|
1a066f |
if [ -n "$prefix" ]
|
|
|
1a066f |
then
|
|
|
1a066f |
echo -e "setenv\t\t${key}\t${env2[$key]}" | sed -e s,$prefix,\$prefix,g
|
|
|
1a066f |
else
|
|
|
1a066f |
echo -e "setenv\t\t${key}\t${env2[$key]}"
|
|
|
1a066f |
fi
|
|
|
1a066f |
fi
|
|
|
1a066f |
done
|
|
|
1a066f |
) | sort
|