#!/bin/sh
#
#                       Systools Convert Script
# This script is meant to be executed by the command 'ups convert systools'.
# It is provided as an aid in upgrading existing systools library functions
# to the new upsII/systools v6_0 system.  Its purpose is to copy existing
# systools files from the old /usr/local/systools/library directory to the
# new target directory, and make necessary changes so that they can work with
# the new ups/systools scheme.  There is no guarantee that the edits made by
# this routine are sufficient or even completely correct, since the original
# scripts may have virtually anything in them.
#
# The original files, after conversion, will be marked with the suffix
# '.CONV'.  These converted scripts should be compared with the new files
# which are part of the systools packages, and the differences merged.
#
# Wayne Baisley
# User Application Support
# uas-group@fnal.gov
#

if [ ! -d /usr/local/systools/library ]
then 
  echo "no /usr/local/systools/library directory found to convert"
  exit
fi
cd /usr/local/systools/library
if [ ! $? ]; then exit $?; fi

if [ ${CMDSCRIPTS_DIR:-x} = "x" ]
then
  echo "cmdscripts is not setup."
  exit 1
fi

if [ ! -d $CMDSCRIPTS_DIR/library ]
then
  echo "The $CMDSCRIPTS_DIR/library does not exist."
  exit 1
fi

LIB_DIR=$CMDSCRIPTS_DIR/library

echo "Converting existing files from older systools installation."
echo ""
echo "You may wish to merge the converted versions (which have a"
echo ".*CONV suffix) with the files from the new installation."
echo ""

save_mask=`umask`
umask 0

for file in ./*
do
 FILE=`basename $file`
 OUT=${LIB_DIR}/$file.CONV
 if [ -d $file ]; then continue; fi
 if [ -f ${OUT} ]; then continue; fi
 if [ "`echo $FILE | grep ':'`" ]; then continue; fi
 if [ "`echo $FILE | grep '\.'`" -a ! "`echo $FILE | grep 'sys\.env$'`" ]; then cp $file ${LIB_DIR}/$file.UNCONV; continue; fi
 if [ "`echo $FILE | grep '~'`" ]; then continue; fi
 if [ "`echo $FILE | grep 'functions$'`" ]; then cp $file ${OUT}; continue; fi
 if [ "`echo $FILE | grep 'disks$'`" ]; then cp $file ${OUT}; continue; fi
 if [ "`echo $FILE | grep 'echo$'`" ]; then continue; fi
 if [ "`echo $FILE | grep 'genpasswd$'`" ]; then continue; fi
 if [ "`echo $FILE | grep 'greeting$'`" ]; then cp $file ${OUT}; continue; fi
 if [ "`echo $FILE | grep 'junk$'`" ]; then continue; fi
 if [ "`echo $FILE | grep 'shells$'`" ]; then cp $file ${OUT}; continue; fi
 if [ "`echo $FILE | grep 'signal$'`" ]; then continue; fi

 echo Converting $FILE
 cp $file ${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/bin/tr :$TR :g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's: tr : \$TR :g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\./adduser.work/genpasswd:$CMDBIN_DIR/bin/genpasswd:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\./genpasswd:$CMDBIN_DIR/bin/genpasswd:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/genpasswd:$CMDBIN_DIR/bin/genpasswd:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/cwi:$CMDBIN_DIR/bin/cwi:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/getlock:$CMDBIN_DIR/bin/getlock:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/grpname:$CMDSCRIPTS_DIR/library/grpname:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/logdir:$CMDSCRIPTS_DIR/library/logdir:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/loggid:$CMDSCRIPTS_DIR/library/loggid:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/obtain:$CMDSCRIPTS_DIR/library/obtain:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\=/usr/local/systools/library/echo:=$CMDBIN_DIR/bin/echo:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/systools/library/echo:$ECHO:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/systools/library/greeting:$CMDSCRIPTS_DIR/library/greeting:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/systools/library/:$CMDSCRIPTS_DIR/library/:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/systools/locks:$SYSTOOLS_DIR/locks:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/systools/logfile:$SYSTOOLS_DIR/logfile:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\=\`echo:\=\`\$ECHO:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/etc/std:$TEMPLATE_HOME_DIR/dotfiles.localized/.:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/funame:$UPS_DIR/bin/funame:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/local/bin/dropit:$UPS_DIR/bin/dropit:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/usr/bin/egrep:$EGREP:g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/bin/expr :$EXPR :g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\`expr :\`$EXPR :g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:/bin/ed :/bin/ex :g' ${OUT}.$$.temp >${OUT}
 mv ${OUT} ${OUT}.$$.temp
 sed -e 's:\[a-z_\]\[a-z0-9_-\]:[a-zA-Z_][a-zA-Z0-9_-]:g' ${OUT}.$$.temp >${OUT}
 rm ${OUT}.$$.temp
done

echo ""
echo "Copying existing *.work files"
echo ""

for dir in adduser kill rm
do
 if [ -d ./$dir.work ]
 then
  for file in ./$dir.work/* DUMMY_FOR_EMPTY_DIR
  do
   FILE=`basename $file`
   if [ $FILE != "DUMMY_FOR_EMPTY_DIR" ]
   then
    OUT=${LIB_DIR}/$dir.work/$FILE
    if [ -d $file ]; then continue; fi
    if [ "`echo $FILE | grep 'unix\.[ug]id[_id]*\.list$'`" ]; then continue; fi
    if [ "`echo $FILE | grep 'genpasswd$'`" ]; then continue; fi
    if [ -f ${OUT} ]; then continue; fi
    echo Copying $dir.work/$FILE
    if [ "`echo $FILE | grep '^user\.[a-zA-Z0-9]*$'`" ]; then umask 027; fi
    if [ "`echo $FILE | grep '^group\.[a-zA-Z0-9]*$'`" ]; then umask 027; fi
    cp $file ${OUT}
    umask 0
   fi
  done
 fi
done

umask $save_mask
