#! /bin/bash
#
#  
#  COPYRIGHT 2008, KRUPCZAK.ORG, LLC.
#
#  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 (at your option) 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
#
#  For more information, visit:
#  http://www.krupczak.org/

# restart cartographer daemon while doing an upgrade
# with any new files

versionStr="KorgVersion 0.53";

# find out what platform we are on so we can set daemon
# first arg passed in is the cartographer home directory

# echo "There are $# args";

if [ $# -lt 1 ]; then
   # default definitions for UNIX
   CARTOGRAPHERDIR=/opt/cartographer   
   echo "restartxmpd.sh: No cartographer dir, setting default";
else 
   CARTOGRAPHERDIR=$1
   echo "restartxmpd.sh: setting cartographer dir based on arg";
fi

PATH=$PATH:$CARTOGRAPHERDIR

# set LD_LIBRARY_PATH so that binaries can find our ssl/crypto libs
LD_LIBRARY_PATH=$CARTOGRAPHERDIR:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

echo "restartxmpd.sh: Cartographer dir is $CARTOGRAPHERDIR";

OSNAME=`uname -s`
ARCH=`uname -p`
DAEMON=xmpd-linux
case "$OSNAME" in 
      Linux)
	  DAEMON="./xmpd-linux"
          ;;

      SunOS)
          case "$ARCH" in
             sparc)
                DAEMON="./xmpd-solaris"
                ;;
             i386)
                DAEMON="./xmpd-solx86"
                ;;
             *)
                DAEMON="./xmpd-solx86"
                ;;
          esac
          ;;

      *)
          DAEMON="xmpd-linux"
          ;;
esac

# make sure we are in the cartographer homedirectory XXX
# before we chmod files
cd $CARTOGRAPHERDIR

echo "Daemon is $DAEMON"

# find out where I live
# echo `pwd` $PWD

# stop cartographer
if [ -f /etc/init.d/cartographer ]; then
   echo "init script present, stop"
   /etc/init.d/cartographer stop
else 
   echo "init script not present"
   pkill -u root xmpd
fi

# wait up to 30 seconds for Cartographer to exit gracefully 
sleep 35

# foreach .new file, rename corresponding current file
# to .old and rename .new to current
list=`ls *.new 2>/dev/null`
for i in $list ; do
   # ${i/.new/} is a bash thing only
   echo File $i becomes ${i/.new/}
   if [ -f ${i/.new/} ]; then
      /bin/mv ${i/.new/} ${i/.new/.old}
   fi
   /bin/mv $i ${i/.new/}
done

# make sure permissions were set correctly
# which translates to 0755
/usr/bin/chmod uog+rx $DAEMON

# sleep another few seconds to let the old socket/port close
# since it may be in FIN_WAIT
sleep 60

# restart cartographer
if [ -f /etc/init.d/cartographer ]; then
   echo "init script present, restarting"
   /etc/init.d/cartographer start
else 
   $DAEMON -b -g
fi

# check on status? 

# done
exit 0
