|
|
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%%=*}
|
|
|
5352b7 |
value=`printenv "$key"`
|
|
|
5352b7 |
if [ $? -eq 0 ]
|
|
|
5352b7 |
then
|
|
|
5352b7 |
echo [$key]="'$value'"
|
|
|
5352b7 |
fi
|
|
|
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 |
|
|
|
5352b7 |
# dedup - remove duplicate entries from a list
|
|
|
1a066f |
#Subshell so we can sort the output
|
|
|
1a066f |
(
|
|
|
1a066f |
dedup() {
|
|
|
1a066f |
list=`mktemp`
|
|
|
5352b7 |
[ -n "$2" ] && sep=$2 || sep=:
|
|
|
5352b7 |
echo $1 | sed -r -e 's,[^/]+/\.\./,,g' -e 's,[^/]+/\.\./,,g' -e "s/\\$sep/\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
|
|
|
5352b7 |
done | tr '\n' $sep | sed -e "s/\\$sep\$//"
|
|
|
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
|
|
|
5352b7 |
added=${env2[$key]%${env1[$key]}}
|
|
|
5352b7 |
sep=${added: -1}
|
|
|
5352b7 |
added=${added%$sep}
|
|
|
5352b7 |
added=$(dedup $added $sep)
|
|
|
5352b7 |
if [ $sep = : ]
|
|
|
5352b7 |
then
|
|
|
5352b7 |
echo -e "prepend-path\t$key\t${added}"
|
|
|
5352b7 |
else
|
|
|
5352b7 |
echo -e "prepend-path\t--delim $sep\t$key\t${added}"
|
|
|
5352b7 |
fi
|
|
|
1a066f |
#Test for prepend plus : added at end (MANPATH)
|
|
|
5352b7 |
elif [ "${key: -4}" = PATH -a "${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
|
|
|
5352b7 |
added=${env2[$key]#${env1[$key]}}
|
|
|
5352b7 |
sep=${added:0:1}
|
|
|
5352b7 |
added=${added#$sep}
|
|
|
5352b7 |
added=$(dedup $added $sep)
|
|
|
5352b7 |
if [ $sep = : ]
|
|
|
5352b7 |
then
|
|
|
5352b7 |
echo -e "append-path\t$key\t${added}"
|
|
|
5352b7 |
else
|
|
|
5352b7 |
echo -e "append-path\t--delim $sep\t$key\t${added}"
|
|
|
5352b7 |
fi
|
|
|
1a066f |
#Test for prepend plus append
|
|
|
5352b7 |
elif [ "${env2[$key]%${env1[$key]}*}" != "${env2[$key]}" ]
|
|
|
1a066f |
then
|
|
|
5352b7 |
prepended=${env2[$key]%${env1[$key]}*}
|
|
|
5352b7 |
presep=${prepended: -1}
|
|
|
5352b7 |
prepended=${prepended%$presep}
|
|
|
5352b7 |
prepended=$(dedup $prepended $presep)
|
|
|
5352b7 |
appended=${env2[$key]#*${env1[$key]}}
|
|
|
5352b7 |
appsep=${appended:0:1}
|
|
|
5352b7 |
appended=${appended#$appsep}
|
|
|
5352b7 |
appended=$(dedup $appended $appsep)
|
|
|
5352b7 |
if [ $presep != $appsep -o -z "$prepended" -o -z "$appended" ]
|
|
|
5352b7 |
then
|
|
|
5352b7 |
#Unhandled
|
|
|
5352b7 |
echo "Unhandled change of $key" 1>&2
|
|
|
5352b7 |
echo "Before <${env1[$key]}>" 1>&2
|
|
|
5352b7 |
echo "After <${env2[$key]}>" 1>&2
|
|
|
5352b7 |
else
|
|
|
5352b7 |
if [ $presep = : ]
|
|
|
5352b7 |
then
|
|
|
5352b7 |
echo -e "prepend-path\t$key\t${prepended}"
|
|
|
5352b7 |
echo -e "append-path\t$key\t${appended}"
|
|
|
5352b7 |
else
|
|
|
5352b7 |
echo -e "prepend-path\t--delim $presep\t$key\t${prepended}"
|
|
|
5352b7 |
echo -e "append-path\t--delim $appsep\t$key\t${appended}"
|
|
|
5352b7 |
fi
|
|
|
5352b7 |
fi
|
|
|
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
|
|
|
5352b7 |
#Delete keys we have 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
|
|
|
5352b7 |
if [ "${key: -4}" = PATH -o "${key: -4}" = DIRS -o "${key: -4}" = FILES ]
|
|
|
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
|