#!/bin/sh
# Portions Copyright (c) 2003 Apple Computer, Inc.  All rights reserved.
if [ -d /afs -a ! -h /afs ]; then
   rmdir /afs
fi
majorvers=`uname -r | sed 's/\..*//'`
if [ $majorvers -ge 7 ]; then
  # /Network is now readonly, so put AFS in /afs; make sure /afs is a directory
  if [ -e /afs ]; then
    if [ -h /afs -o ! -d /afs ]; then
      rm -f /afs
      mkdir /afs
    fi
  else
    mkdir /afs
  fi
else
  mkdir -p /Network/afs
  if [ ! -h /afs ]; then
    ln -s /Network/afs /afs
  fi
fi

cd /var/db/openafs/etc

if [ ! -f cacheinfo ]; then
   cp cacheinfo.sample cacheinfo
fi

if [ `grep /Network/afs cacheinfo` ]; then
    cat cacheinfo |sed s#/Network/afs#/afs# > cacheinfo.new
    mv cacheinfo.new cacheinfo
fi

if [ ! -f ThisCell -a -f ThisCell.sample ]; then
   cp ThisCell.sample ThisCell
fi

if [ ! -f config/afsd.options -a -f config/afsd.options.sample ]; then
   cp config/afsd.options.sample config/afsd.options
fi

# testing case -- upgrading from pre 1.2.7, but .last file exists.
# merge the .save and .last....
if [ ! -f CellServDB -a -f CellServDB.save -a -f CellServDB.master.last ]; then
   cp CellServDB.save CellServDB
fi

# upgrading between 2 post-1.2.7 releases (or the testing case)
if [ -f CellServDB.master.last ]; then
  if cmp -s CellServDB.master.last CellServDB.master ; then
    echo No CellServDB updates
    done=1
  fi
fi

# the local changes were included in the update
if [ -z "$done" -a  -f CellServDB ]; then
  if cmp -s CellServDB CellServDB.master ; then
    echo CellServDB is up to date.
    cp CellServDB.master CellServDB.master.last
    if [ ! -f CellServDB ]; then
      cp CellServDB.master CellServDB
    fi
    done=1
  fi
fi

if [ -z "$done" ]; then
  # there are local changes to be merged
  if [ -f CellServDB -a -f CellServDB.master.last ]; then
    echo Merging CellServDB entries....
    perl $1/Contents/Resources/csrvdbmerge.pl
    echo done
  else
    # this is a new install, or an upgrade from pre-1.2.7
    echo "Installing new CellServDB"
    cp CellServDB.master CellServDB.master.last
    if [ ! -f CellServDB ]; then
	cp CellServDB.master CellServDB
    fi
  fi
fi

if [ $majorvers -ge 7 ]; then
  # make config/settings.plist if it doesn't exist
  if [ ! -e config/settings.plist -a -e config/settings.plist.orig ]; then
    cp config/settings.plist.orig config/settings.plist
  fi
elif [ -e config/afssettings ]; then
  # turn off execution of afssettings
  chmod a-x config/afssettings
fi

#here we should run tools which configure the client, and then if it's enabled:
/Library/StartupItems/OpenAFS/OpenAFS start

