#!/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 19 ]; then
  # Root mount point is read-only. To workaround this restriction, add a
  # synthetic entity for afs into /etc/synthetic.conf. For more information,
  # please read man synthetic.conf(5).
  if [ $majorvers -ge 21 ]; then
    # On macOS 12, SIP is not allowing our LaunchDaemon to unmount /afs on
    # restart/shutdown. If /afs is mounted on the user's directory, this problem
    # doesn't happen.
    sed -i "" "/OpenAFS\/afs/d" /etc/synthetic.conf &> /dev/null
    sed -i "" "/^afs$/d" /etc/synthetic.conf &> /dev/null
    echo "afs\tUsers/$USER/OpenAFS/afs" >> /etc/synthetic.conf
    mkdir -p /Users/$USER/OpenAFS/afs
  else
    grep -qs '^afs$' /etc/synthetic.conf || echo 'afs' >> /etc/synthetic.conf
  fi
elif [ $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 [ $majorvers -ge 21 ]; then
    sed -i "" "s/\/afs:/\/Users\/$USER\/OpenAFS\/afs:/" cacheinfo
fi

# if the installer plugin generated ThisCell and/or CellAlias
# files, copy them in here
tmpthiscell=/private/tmp/org.OpenAFS.Install.ThisCell.$USER
tmpcellalias=/private/tmp/org.OpenAFS.Install.CellAlias.$USER
if [ -f $tmpthiscell ]; then
    cp $tmpthiscell ThisCell
    rm -f $tmpthiscell
fi
if [ -f $tmpcellalias ]; then
    cp $tmpcellalias CellAlias
    rm -f $tmpcellalias
fi

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

# force reversion of 10.4 memcache mistake
rm -f config/afsd.options.broken
echo '-memcache -afsdb -stat 2000 -dcache 800 -daemons 3 -volumes 70 -dynroot -fakestat-all' > config/afsd.options.broken
if cmp -s config/afsd.options.broken config/afsd.options ; then
   rm -f config/afsd.options
fi
rm -f config/afsd.options.broken

# if the user hasn't changed these settings, then they should just use the new
# afsd.conf file
rm -f config/afsd.options.old
echo '-afsdb -stat 2000 -dcache 800 -daemons 3 -volumes 70 -dynroot -fakestat-all' >config/afsd.options.old
if cmp -s config/afsd.options.old config/afsd.options ; then
   rm -f config/afsd.options
fi
rm -f config/afsd.options.old

if [ ! -f config/afs.conf -a -f config/afs.conf.sample ]; then
   cp config/afs.conf.sample config/afs.conf
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
  else
    /usr/libexec/PlistBuddy -c "Add :Darwin:All:FSEvents bool" config/settings.plist  && /usr/libexec/PlistBuddy -c "Set :Darwin:All:FSEvents true" config/settings.plist
  fi
elif [ -e config/afssettings ]; then
  # turn off execution of afssettings
  chmod a-x config/afssettings
fi

# properly, we should acquire a certificate from a real CA and ship 
# signed binaries. for now, make Application Firewall (Security prefs pane)
# happy like this. See TN2206
if [ $majorvers -lt 14 ]; then
    if [ -f /usr/bin/codesign ]; then
	codesign -s - /usr/sbin/afsd
    fi
fi

#here we should run tools which configure the client, and then if it's enabled:
#start the new launchd daemon
launchctl load -w /Library/LaunchDaemons/org.openafs.filesystems.afs.plist

# Install and start our privileged helper
PRIVHELPER="/Library/PreferencePanes/OpenAFS.prefPane/Contents/Library/LaunchServices/org.openafs.privhelper"
if [[ -f "$PRIVHELPER" ]]; then
    if [[ ! -d "/Library/PrivilegedHelperTools" ]]; then
	mkdir -p "/Library/PrivilegedHelperTools"
	chmod 755 "/Library/PrivilegedHelperTools"
	chown -R root:wheel "/Library/PrivilegedHelperTools"
    fi

    cp -f "$PRIVHELPER" "/Library/PrivilegedHelperTools"
    if [[ $? -eq 0 ]]; then
	chmod 755 "/Library/PrivilegedHelperTools/org.openafs.privhelper"
	PRIVDAEMON="/Library/LaunchDaemons/privhelper-launchd.plist"
	cat > "$PRIVDAEMON" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>org.openafs.privhelper</string>
        <key>MachServices</key>
        <dict>
                <key>org.openafs.privhelper</key>
                <true/>
        </dict>
        <key>ProgramArguments</key>
        <array>
                <string>/Library/PrivilegedHelperTools/org.openafs.privhelper</string>
        </array>
</dict>
</plist>
EOF
	chmod 644 "$PRIVDAEMON"
	launchctl load -w "$PRIVDAEMON"
    fi
fi

if [ $majorvers -ge 19 ]; then
    # Assume that, if /afs is not present, either OpenAFS is being installed for
    # the first time or the system was not rebooted after the installation.
    # If so, inform what needs to be done to load the client. This procedure is
    # unnecessary if OpenAFS is being reinstalled.
    if [ ! -d /afs ]; then
	if [ $majorvers -ge 22 ]; then
		osascript -e 'display alert "OpenAFS successfully installed" message ¬
		"If OpenAFS is being installed for the first time on this machine, " & ¬
		"follow the steps below:\n\n" & ¬
		"1. Navigate to System Settings > Privacy & Security;\n\n" & ¬
		"2. Under the Security section towards the bottom of the window, allow " & ¬
		"the OpenAFS kernel extension to load by clicking on Allow;\n\n" & ¬
		"3. Reboot the machine;\n\n" & ¬
		"Note: An entry for /afs was added in /etc/synthetic.conf. This " & ¬
		"change takes effect only after rebooting the system."'
	else
		osascript -e 'display alert "OpenAFS successfully installed" message ¬
		"If OpenAFS is being installed for the first time on this machine, " & ¬
		"follow the steps below:\n\n" & ¬
		"1. Navigate to System Preferences > Security & Privacy;\n\n" & ¬
		"2. Under the General tab towards the bottom of the window, allow " & ¬
		"the OpenAFS kernel extension to load by clicking on Allow;\n\n" & ¬
		"3. Reboot the machine;\n\n" & ¬
		"Note: An entry for /afs was added in /etc/synthetic.conf. This " & ¬
		"change takes effect only after rebooting the system."'
	fi
    fi
fi
