#!/bin/sh
set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# bin/$RCSfile: mod_config,v $  $Revision: 1.26 $

export place=`pwd`

#if [ `whoami` = "root" -o `whoami` = "enstore" ]; then
if [ `whoami` != "enstore" ]; then
    echo "You can not be `whoami` while you modify the enstore config file" >&2
    exit 1
fi

#node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
node=`$ENSTORE_DIR/sbin/hostname.py | cut -f1 -d\.`
gang=`gang`
short_name=`echo $ENSTORE_CONFIG_HOST | cut -f1 -d\.`
case $node in
   stkensrv2|d0ensrv2|cdfensrv2)
          loadit=1
          ;;
   ${ENSTORE_CONFIG_HOST})
          loadit=1
	  ;;
   ${short_name})
          loadit=1
          ;;	  
   *)     loadit=0
          ;;
esac


usage() { echo "USAGE: $0 config_file  <optional editor>  where config_file is stk, cdf or sam"; }

abort() { cd $place
          echo ${1-""}
          if [ -z ${3-} ]; then usage; fi
          exit ${2:-1}
        }

# we need enstore to be setup
if [ -z "${ENSTORE_DIR-}"  ]; then
   . /usr/local/etc/setups.sh
   setup enstore
fi

# all the action is here
#cd $ENSTORE_DIR/etc

# we need a config file to change
if [ -n "${ENSTORE_CONFIG_FILE-}" ]; 
then 
 echo "The following configuration file will be modified: `basename $ENSTORE_CONFIG_FILE`";
 dir=`dirname $ENSTORE_CONFIG_FILE`
 #cp -rpf $d .
 #cp -rpf $ENSTORE_CONFIG_FILE .
  config=`basename $ENSTORE_CONFIG_FILE`;
elif 
    [ -z "${1-}"  ]; then abort  "you need to specify a config file"; 
else
    dir=$ENSTORE_DIR/etc
    config=$1.conf
fi
cd $dir

#config=$1.conf
if [ ! -r $config ]; then abort "can not read $config"; fi
#config2=/tmp/$1.conf-check.py
config2=/tmp/$config.conf-check.py

# if the user wants a specific editor, use it
# otherwise use vi since it is always there
if [ -z "${2-}"  ]; then
     edcmd=${EDITOR:-vi}
else
     edcmd=$2
fi
export EDITOR=$edcmd # probably not needed, just did it so it is set
export VISUAL=$edcmd # needed for cvs check in

# make sure we are using the latest config from the repository
echo "doing cvs update on $config"
cvs update -A $config
if [ $? -ne 0 ]; then abort "Something wrong with cvs update. Investigate"; fi
echo "success"
echo
cvs status $config
echo

# make a copy and edit it
rm -f $config2
cp $config $config2

# edit the file allowing the user to make changes
#echo "DISPLAY=${DISPLAY:-<notset>}, about to edit with command $edcmd $config"
echo "DISPLAY=${DISPLAY:-<notset>}, about to edit with command $edcmd $config2"
#$edcmd $config
$edcmd $config2
if [ $? -ne 0 ]; then abort "Something wrong with editting. Investigate"; fi
echo "success"
echo

# check if python file
#sed -n '1p' $config | grep python 
sed -n '1p' $config2 | grep python 
if [ $? -eq 0 ]; then
    # see if python finds any errors
    #echo "Checking $config by running python $config"
    #python $config
    echo "Checking $config2 by running python $config2"
    python $config2
    if [ $? -ne 0 ]; then abort "Running python found errors."; fi
    echo "success"
    echo

    # overkill - run through lint too
    #echo "Checking $config by running python linter"
    echo "Checking $config2 by running python linter"
    #rm -f $config2
    #cp $config $config2
    #python $ENSTORE_DIR/tools/mylint.py $config
    python $ENSTORE_DIR/tools/mylint.py $config2
    if [ $? -ne 0 ]; then abort "Running lint found errors"; fi
    echo "success"
    echo
# check if it is an authorized volume file
elif [ `echo $config | cut -c1-4` = "auth" ]; then
    # make sure the auth file is correct
    loadit=2
    echo "Checking authorized file $config"
    # ignore comment and empty lines
    data="`grep -v "#" $config | grep -v "^[:space:]*$"`"
    total=`echo "$data" | wc -l`
    count="1"
    while [ $count -le $total ]; do
	# make sure each line has exactly 4 items on it.
	line=`echo "$data" | sed -n ${count}p`
	word_count=`echo "$line" | wc -w`
	if [ $word_count -ne 4 ]; then
	    abort "Error checking authorized volume file $config"
	fi
	count=`expr $count + 1`
    done
    echo "success"
    echo
fi

# ok, time to commit - confirm
echo -n 'About to commit changes, is this what you want [Y/n] '
read ans
answer=`echo ${ans:-Y}|cut -c1 | tr [a-z] [A-Z]`
if [ "$answer" != "Y" ]; then
   abort 'ok, aborting' 1 0
fi

# copy $config2 to $config before committing
cp $config2 $config
rm -f $config2

cvs commit $config
if [ $? -ne 0 ]; then abort "Something wrong with commit. Investigate"; fi
echo "success"
echo

# bless the changes - make production status
echo "Setting production tag"
$ENSTORE_DIR/tools/bless.py $config
if [ $? -ne 0 ]; then abort "Something wrong with setting production tag.. Investigate"; fi
echo "success"
echo

cvs status $config
echo
# update config file on all machines
echo "syncronizing $config"
rgang $gang "cd ${dir}; cvs -q update -r production $config"

# check for situations where loading in the config file will not work.
if [ `echo $config | cut -c1-4` = "auth" ]; then
    # authority config files do not need to be loaded.
    echo
elif [ $loadit -ne 1 ]; then
  abort "Not on stkensrv2, cdfensrv2 or d0ensrv2. Do not know how to load config file.  Try: enstore config --load --config_file=$ENSTORE_DIR/etc/$config" 0 0
fi

# all is good, load the config file.
if [ $loadit -eq 1 ]; then
    # ok, time to load new config file
    echo -n 'About to load new config file into server, is this what you want [Y/n] '
    read ans
    answer=`echo ${ans:-Y}|cut -c1 | tr [a-z] [A-Z]`
    if [ "$answer" != "Y" ]; then
	abort 'ok, done' 1 0
    fi
    enstore config --load --config_file=$ENSTORE_DIR/etc/$config
elif [ $loadit -eq 2 ]; then
    echo "You need to "
    echo "   becom enstore on  stkensrv2 (ksu enstore)"
    echo "   cd ~/enstore/etc"
    echo "   cvs -q update -r production auth_stk.conf"
fi
