envmng/bin/envmng
agibert f6ac4f0426 - Minor cleanup,
- Use $Name CVS tag in version print out.
2009-01-13 14:07:53 +00:00

323 lines
7.6 KiB
Plaintext

# $RCS envmng,v $
# $Revision: 1.3 $
# $Name: $
# $Date: 2009/01/13 14:07:53 $
# $Author: agibert $
# envmng - Unix shell Environment Manager
# Copyright (C) 2004-2005 Arnaud G. Gibert
# mailto:arnaud@rx3.net
# http://www.rx3.org/dvp/?dvp=envmng
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
EnvMng_Cmd="$1"
EnvMng_Env="$2"
EnvMng_Opt="$3"
#-------------------------------------------------------------------------------
# Installation Configuration
#-------------------------------------------------------------------------------
envmng_prefix=/usr/local/bin
envmng_d=$envmng_prefix/etc/envmng.d
envmng_id=$envmng_prefix/etc/envmng_id
envmng_protvar=$envmng_prefix/etc/envmng_protvar
envmng_protalias=$envmng_prefix/etc/envmng_protalias
envmng_display=$envmng_prefix/etc/envmng_display
#-------------------------------------------------------------------------------
# System configuration
#-------------------------------------------------------------------------------
basename=/bin/basename
env=/bin/env
sed=/bin/sed
grep=/bin/grep
printf=/bin/printf
alias envmng=". $envmng_prefix/bin/envmng"
#-------------------------------------------------------------------------------
envmng_rlist()
{
rlist=""
for elem in $1
do
rlist="$elem $rlist"
done
echo $rlist
}
envmng_path_add()
{
if [ "$1" != "" ]
then
echo "$2:$1"
else
echo "$2"
fi
}
envmng_path_rm()
{
echo "$1" | $sed -e "s/$(echo "$2" | $sed "s/\//\\\\\//g")//" -e "s/\:\:/\:/g" -e "s/^\://" -e "s/\:$//"
}
#set +o nounset
case "$EnvMng_Cmd" in
-b | --bash)
if [ "$EnvMng_Env" != "" ]
then
export ENVMNG_ID=$EnvMng_Env
fi
if [ "$BASH" = "" ]
then
echo "envmng: new shell: [bash] seting id: [$ENVMNG_ID]..."
export NEW_SHELL=/usr/local/bin/bash
$NEW_SHELL
unset NEW_SHELL
else
echo "envmng: allready in: [bash] seting id: [$ENVMNG_ID]..."
. /usr/local/bin/envmng --setid $ENVMNG_ID $EnvMng_Opt
fi
;;
-k | --ksh)
if [ "$EnvMng_Env" != "" ]
then
export ENVMNG_ID=$EnvMng_Env
fi
if [ "$BASH" != "" ]
then
echo "envmng: new shell: [ksh] seting id: [$ENVMNG_ID]..."
export NEW_SHELL=/bin/ksh
$NEW_SHELL
unset NEW_SHELL
else
echo "envmng: allready in: [ksh] seting id: [$ENVMNG_ID]..."
. /usr/local/bin/envmng --setid $ENVMNG_ID $EnvMng_Opt
fi
;;
-S | --setid)
echo "envmng: seting id: [$EnvMng_Env]..."
export ENVMNG_ID=$EnvMng_Env
. $envmng_prefix/bin/envmng --reset "$($grep "^$EnvMng_Env:" $envmng_id | $sed "s/^$EnvMng_Env:[ \ ]*//")" $EnvMng_Opt
;;
-R | --reset)
echo "envmng: reseting env [$EnvMng_Env]..."
echo "envmng: reseting profile..."
norc=$EnvMng_Opt
env_list=$($env | $sed -e "s/=.*//")
if [ "$env_list" != "" ]
then
for var in $env_list
do
if [ $var != "$($grep ^$var$ $envmng_protvar)" ]
then
unset $var
fi
done
fi
export ENVMNG_LIST="$EnvMng_Env"
echo "envmng: reseting aliases..."
alias_list=$(alias | $grep ".*=" | $sed -e "s/alias //" -e "s/=.*//")
if [ "$alias_list" != "" ]
then
for alias in $alias_list
do
if [ $alias != "$($grep ^$alias$ $envmng_protalias)" ]
then
unalias $alias
fi
done
fi
. $envmng_prefix/bin/envmng --profile
if [ "$norc" != "norc" ]
then
. $envmng_prefix/bin/envmng --rc
fi
;;
-a | --add)
if [ "$(echo $ENVMNG_LIST | $sed -e s/.*\:$EnvMng_Env/$EnvMng_Env/ -e s/$EnvMng_Env\:.*/$EnvMng_Env/)" != "$EnvMng_Env" ]
then
env_list=$(echo $ENVMNG_LIST | $sed -e "s/\:/ /g")
if [ "$EnvMng_Opt" = "tail" ]
then
echo "envmng: adding [$EnvMng_Env] on [$EnvMng_Opt]..."
if [ "$env_list" != "" ]
then
. $envmng_prefix/bin/envmng --reset $ENVMNG_LIST:$EnvMng_Env
else
export ENVMNG_LIST=$EnvMng_Env
. $envmng_prefix/bin/envmng --profile $EnvMng_Env
. $envmng_prefix/bin/envmng --rc $EnvMng_Env
fi
else
EnvMng_Opt=head
echo "envmng: adding [$EnvMng_Env] on [$EnvMng_Opt]..."
export ENVMNG_LIST=$EnvMng_Env:$ENVMNG_LIST
. $envmng_prefix/bin/envmng --profile $EnvMng_Env
. $envmng_prefix/bin/envmng --rc $EnvMng_Env
fi
else
echo "envmng: skipping [$EnvMng_Env] env !"
fi
;;
-d | --del)
echo "envmng: deleting [$EnvMng_Env]..."
if [ "$(echo $ENVMNG_LIST | $sed -e s/.*\:$EnvMng_Env/$EnvMng_Env/ -e s/$EnvMng_Env\:.*/$EnvMng_Env/)" == "$EnvMng_Env" ]
then
export ENVMNG_LIST=$(echo $ENVMNG_LIST | $sed -e "s/$EnvMng_Env//" -e "s/\:\:/\:/g" -e "s/^\://" -e "s/\:$//")
. $envmng_prefix/bin/envmng --reset $ENVMNG_LIST
else
echo "envmng: skipping [$EnvMng_Env] profile !"
fi
;;
-p | --profile | -r | --rc)
if [ "$EnvMng_Cmd" = "-p" ] | [ "$EnvMng_Cmd" = "--profile" ]
then
mode="profiling"
else
mode="rcing"
fi
echo "envmng: $mode..."
if [ "$EnvMng_Env" != "" ]
then
echo "envmng: $mode [$EnvMng_Env]..."
. $envmng_d/$EnvMng_Env ${EnvMng_Cmd}
else
env_list=$(envmng_rlist "$(echo $ENVMNG_LIST | $sed -e "s/\:/ /g")")
if [ "$env_list" != "" ]
then
for envi in $env_list
do
echo "envmng: $mode [$envi]..."
. $envmng_d/$envi ${EnvMng_Cmd}
done
fi
fi
;;
-s | --status)
echo "ENVMNG_ID: [$ENVMNG_ID] ENVMNG_LIST: [$ENVMNG_LIST]"
;;
-l | --list)
echo "Id and EnvList:"
. $envmng_prefix/bin/envmng --listid
echo
echo "Env:"
. $envmng_prefix/bin/envmng --listenv
;;
-li | --listid)
for line in $( $grep -v "^#" $envmng_id | $sed "s/:[ \ ]*/#/")
do
$printf "%-16s %s\n" $( echo $line | $sed "s/#.*/:/") $( echo $line | $sed "s/^.*#//")
done
;;
-le | --listenv)
for file in $envmng_d/*
do
if [[ ( -f $file ) && ( $file != *~ ) && ( $file != */env_template ) ]]
then
$printf "%-16s " $( echo $( $basename $file):)
echo $( $grep "^# description: " $file | $sed "s/^. description: //")
fi
done
;;
-h | --help)
echo "Usage: envmng [COMMAND] [OPTION]"
echo " -b, --bash [<Id>] [norc] switch to bash and set id to <Id>"
echo " -k, --ksh [<Id>] [norc] switch to ksh and set id to <Id>"
echo " -S, --setid <Id> [norc] set id to <Id>"
echo " -R, --reset <EnvList> reset envlist to <EnvList>"
echo " -a, --add <Env> [head|tail] add <Env> in envlist head or tail"
echo " -d, --del <Env> del <Env> from envlist"
echo " -p, --profile [<Env>] do envlist or <Env> profile"
echo " -r, --rc [<Env>] do envlist or <Env> rc"
echo " -s, --status print status"
echo " -l, --list list Id(s), EnvList(s) and Env(s)"
echo " -li, --listid list Id(s) and EnvList(s)"
echo " -le, --listenv list Env(s)"
echo " -h, --help print this help"
echo " -V, --version print version"
;;
-V | --version)
echo -n "envmng V "
echo "$Name: $" | sed -e 's/^[^-]*-//' -e 's/ .*//' -e 's/_/./g'
;;
*)
echo "$0: Invalid command: [$EnvMng_Cmd] !"
. $envmng_prefix/bin/envmng --help
;;
esac