#!/bin/sh
DAEMON=/private/var/db/openafs/etc/launchafs.sh
DAEMON_LAUNCHD_SCRIPT=/Library/LaunchDaemons/org.openafs.filesystems.afs.plist
PRIVHELPER_LAUNCHD_SCRIPT=/Library/LaunchDaemons/privhelper-launchd.plist
DAEMON_UP=$(ps -ef | grep "$DAEMON_NAME" | grep -v grep | wc -l)
PREFERENCE_PANE=/Library/PreferencePanes/OpenAFS.prefPane

cd /var/db/openafs/etc
# only do these checks if we're upgrading from a pre-1.2.7
if [ -f CellServDB -a ! -f CellServDB.master ]; then 
    CK=`md5 CellServDB | awk '{ print $4 }'`
    if grep -q $CK $1/Contents/Resources/CellServDB.list; then
       # it matched one of our previously released files
       echo Unmodified CellServDB -- not preserving
       :
    else
       if [ ! -f CellServDB.master.last ]; then
          echo Modified CellServDB -- saving as /var/db/openafs/etc/CellServDB.save
          echo Please re-merge it manually -- this will not be required for future upgrades
       fi
       cp CellServDB CellServDB.save
    fi
fi
if [ -f config/afsd.options -a -f config/afsd.options.sample ]; then
  if cmp -s config/afsd.options config/afs.options.sample; then
    echo Removing no-longer needed afsd.options file
    rm -f config/afsd.options
  fi
fi

if [ -f /Library/StartupItems/OpenAFS/OpenAFS ]; then
  /Library/StartupItems/OpenAFS/OpenAFS stop
  rm /Library/StartupItems/OpenAFS/OpenAFS
else
  if [ -f /Library/LaunchDaemons/org.openafs.filesystems.afs.plist ]; then
	if ps ax | grep -v grep | grep $DAEMON > /dev/null
	then
		launchctl stop org.openafs.filesystems.afs
	else
		echo "$DAEMON is not running"
	fi

	launchctl unload -w $DAEMON_LAUNCHD_SCRIPT
  fi

  if [ -f $PRIVHELPER_LAUNCHD_SCRIPT ]; then
	launchctl unload -w $PRIVHELPER_LAUNCHD_SCRIPT
  fi
fi

if [ -d /Library/PreferencePanes/OpenAFS.prefPane ]; then
	rm -R /Library/PreferencePanes/OpenAFS.prefPane
fi
