#!/bin/bash
# Portions Copyright (c) 2003, 2006 Apple Computer, Inc.  All rights reserved.

if [ -z "$1" ]; then
    echo Usage: buildpkg binary-dir
    echo '  or'
    echo 'Usage: buildpkg [-firstpass] binary-dir'
    echo '            (customize pkgroot)'
    echo '       buildpkg [-secondpass]'
    exit 1
fi

firstpass=yes
secondpass=yes
if [ "$1" = "-firstpass" ]; then
    secondpass=no
    shift
elif [ "$1" = "-secondpass" ]; then
    firstpass=no
    shift
fi

BINDEST=`cd $1 && pwd`
CURDIR=`pwd`
RESSRC=`dirname $0`
RESSRC=`cd $RESSRC && pwd`
majorvers=`uname -r | sed 's/\..*//'`
if [ $majorvers -eq 7 ]; then
  RELNAME="Panther"
fi
if [ $majorvers -eq 8 ]; then
  RELNAME="Tiger"
fi
if [ $majorvers -eq 9 ]; then
  RELNAME="Leopard"
fi
if [ $majorvers -eq 10 ]; then
  RELNAME="Snowleopard"
fi
if [ $majorvers -eq 11 ]; then
  RELNAME="Lion"
fi
if [ $majorvers -eq 12 ]; then
  RELNAME="MountainLion"
fi
if [ $majorvers -eq 13 ]; then
  RELNAME="Mavericks"
fi
if [ $majorvers -eq 14 ]; then
  RELNAME="Yosemite"
fi
if [ $majorvers -eq 15 ]; then
  RELNAME="El Capitan"
fi


PKGROOT=$CURDIR/pkgroot
PKGRES=$CURDIR/pkgres
DPKGROOT=$CURDIR/dpkgroot
DPKGRES=$CURDIR/dpkgres
PACKAGEMAKER=""
if [ -e "/Developer/usr/bin/packagemaker" ]; then
    PACKAGEMAKER="/Developer/usr/bin/packagemaker"
else
    while IFS= read -d $'\0' -r file ; do
        if [ -e "$file/Contents/MacOS/PackageMaker" ]; then
            PACKAGEMAKER="$file/Contents/MacOS/PackageMaker"
        fi
    done < <(mdfind -0 "(kMDItemCFBundleIdentifier == 'com.apple.PackageMaker')")
fi
if [ -z "$PACKAGEMAKER" ]; then
    echo "packagemaker not found"
    exit 1
fi

if [ $firstpass = yes ]; then
    if [ -x /usr/bin/curl ]; then
	/usr/bin/curl -f -O http://dl.central.org/dl/cellservdb/CellServDB
    fi

    if [ ! -f CellServDB ]; then
       echo "A CellServDB file must be placed in the working directory"
       die=1
    else
       if grep -q 'GCO Public CellServDB' CellServDB ; then
         touch CellServDB
       else
          echo "A proper CellServDB file must be placed in the working directory"
          die=1
       fi
    fi
    FILES="ReadMe.rtf.$majorvers License.rtf CellServDB.list OpenAFS.info OpenAFS.post_install OpenAFS.pre_upgrade csrvdbmerge.pl 2.0.txt"
    for f in $FILES; do
       if [ ! -f $RESSRC/$f ]; then
	 echo "file missing: " $RESSRC/$f
	 die=1
       fi
    done
    if [ "$die" ]; then
      echo "Correct above errors; then retry"
      exit 1
    fi
    if [ ! -f $BINDEST/bin/translate_et ]; then
      die=1
    fi
    if [ ! -f $BINDEST/root.client/usr/vice/etc/afs.kext/Contents/MacOS/afs ]; then
      die=1
    fi
    if [ "$die" ]; then
       echo $BINDEST " is not a valid binary dir. it should be the result of"
       echo "make dest"
       exit 1
    fi

    SEP=:

    rm -rf $PKGROOT
    mkdir $PKGROOT

    mkdir -p $PKGROOT/Library
    mkdir -p $PKGROOT/Library/PreferencePanes
    mkdir -p $PKGROOT/Library/Security/SecurityAgentPlugins
    chown root${SEP}admin $PKGROOT
    chmod 755 $PKGROOT $PKGROOT/Library $PKGROOT/Library/PreferencePanes $PKGROOT/Library/Security/SecurityAgentPlugins
    (cd $BINDEST/tools && pax -rw OpenAFS.prefPane $PKGROOT/Library/PreferencePanes)
    (cd $BINDEST/tools && pax -rw aklog.bundle $PKGROOT/Library/Security/SecurityAgentPlugins)
    mkdir -p $PKGROOT/Library/OpenAFS/Tools
    (cd $BINDEST && pax -rw * $PKGROOT/Library/OpenAFS/Tools)
    cd $RESSRC
    mkdir -p $PKGROOT/Library/LaunchDaemons
    chmod -R o-w $PKGROOT/Library
    chmod -R g-w $PKGROOT/Library
    cp openafs.launchd.plist $PKGROOT/Library/LaunchDaemons/org.openafs.filesystems.afs.plist
    chmod 644 $PKGROOT/Library/LaunchDaemons/org.openafs.filesystems.afs.plist
    chown root${SEP}admin $PKGROOT/Library
    chown -R root${SEP}wheel $PKGROOT/Library/LaunchDaemons
    chown -R root${SEP}wheel $PKGROOT/Library/OpenAFS/Tools
    chmod -R og-w $PKGROOT/Library/OpenAFS/Tools

    mkdir -p $PKGROOT/private/var/db/openafs/cache
    mkdir -p $PKGROOT/private/var/db/openafs/etc/config
    cp openafs.launchdaemon $PKGROOT/private/var/db/openafs/etc/launchafs.sh
    chmod 755 $PKGROOT/private/var/db/openafs/etc/launchafs.sh
    cp $CURDIR/CellServDB $PKGROOT/private/var/db/openafs/etc/CellServDB.master
    echo grand.central.org > $PKGROOT/private/var/db/openafs/etc/ThisCell.sample
    if [ $majorvers -ge 7 ]; then
	echo /afs:/var/db/openafs/cache:100000 > $PKGROOT/private/var/db/openafs/etc/cacheinfo.sample
        cp -RP $PKGROOT/Library/OpenAFS/Tools/etc/afssettings $PKGROOT/private/var/db/openafs/etc/config
	cp settings.plist $PKGROOT/private/var/db/openafs/etc/config/settings.plist.orig
    else
	echo /Network/afs:/var/db/openafs/cache:30000 > $PKGROOT/private/var/db/openafs/etc/cacheinfo.sample
    fi
    cp afs.conf $PKGROOT/private/var/db/openafs/etc/config/afs.conf.sample
    cp krb5-weak.conf $PKGROOT/private/var/db/openafs/etc/krb5-weak.conf

    cp decode-panic $PKGROOT/Library/OpenAFS/Tools/tools
    chmod a+x $PKGROOT/Library/OpenAFS/Tools/tools/decode-panic

    if [ $majorvers -ge 9 ]; then
	rm -rf $DPKGROOT
	mkdir $DPKGROOT
	mkdir -p $DPKGROOT/Library/OpenAFS/Debug
	mv $PKGROOT/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext.dSYM $DPKGROOT/Library/OpenAFS/Debug
	cp -RP $PKGROOT/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext $DPKGROOT/Library/OpenAFS/Debug
	chown -R root${SEP}wheel $DPKGROOT/Library/OpenAFS/Debug
	chmod -R og-w $DPKGROOT/Library/OpenAFS/Debug
    fi

    strip -X -S $PKGROOT/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext/Contents/MacOS/afs

    cp -RP $PKGROOT/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext $PKGROOT/private/var/db/openafs/etc
    cp -RP $PKGROOT/Library/OpenAFS/Tools/root.client/usr/vice/etc/C $PKGROOT/private/var/db/openafs/etc

    chown -R root${SEP}wheel $PKGROOT/private
    chmod -R og-w $PKGROOT/private
    chmod  og-rx $PKGROOT/private/var/db/openafs/cache

    mkdir -p $PKGROOT/usr/bin $PKGROOT/usr/lib $PKGROOT/usr/sbin $PKGROOT/usr/share/man/man1 $PKGROOT/usr/share/man/man5 $PKGROOT/usr/share/man/man8

    BINLIST="fs klog klog.krb pagsh pagsh.krb pts sys tokens tokens.krb unlog unlog.krb aklog"
    ETCLIST="vos"
    SHLIBLIST="libafsauthent.dylib libafsauthent.dylib.1 libafsauthent.dylib.1.1 libafshcrypto.dylib libafshcrypto.dylib.1 libafshcrypto.dylib.1.1 libafsrpc.dylib libafsrpc.dylib.1 libafsrpc.dylib.1.4 libkopenafs.dylib libkopenafs.dylib.1 libkopenafs.dylib.1.1 librokenafs.dylib librokenafs.dylib.1 librokenafs.dylib.1.1"

# Should these be linked into /usr too?
    OTHER_BINLIST="bos cmdebug rxgen translate_et udebug xstat_cm_test xstat_fs_test"
    OTHER_ETCLIST="rxdebug"

    for f in $BINLIST; do
       ln -s ../../Library/OpenAFS/Tools/bin/$f $PKGROOT/usr/bin/$f
    done
    for f in $ETCLIST; do
       ln -s ../../Library/OpenAFS/Tools/etc/$f $PKGROOT/usr/sbin/$f
    done
    for f in $SHLIBLIST; do
       ln -s ../../Library/OpenAFS/Tools/lib/$f $PKGROOT/usr/lib/$f
    done
    for f in `ls $PKGROOT/Library/OpenAFS/Tools/man/man1`; do
	gzip -9 $PKGROOT/Library/OpenAFS/Tools/man/man1/$f
	ln -s ../../../../Library/OpenAFS/Tools/man/man1/$f.gz $PKGROOT/usr/share/man/man1/$f.gz
    done
    for f in `ls $PKGROOT/Library/OpenAFS/Tools/man/man5`; do
	gzip -9 $PKGROOT/Library/OpenAFS/Tools/man/man5/$f
	ln -s ../../../../Library/OpenAFS/Tools/man/man5/$f.gz $PKGROOT/usr/share/man/man5/$f.gz
    done
    for f in `ls $PKGROOT/Library/OpenAFS/Tools/man/man8`; do
	gzip -9 $PKGROOT/Library/OpenAFS/Tools/man/man8/$f
	ln -s ../../../../Library/OpenAFS/Tools/man/man8/$f.gz $PKGROOT/usr/share/man/man8/$f.gz
    done

    ln -s ../../Library/OpenAFS/Tools/bin/kpasswd $PKGROOT/usr/bin/kpasswd.afs

    ln -s ../../Library/OpenAFS/Tools/root.client/usr/vice/etc/afsd $PKGROOT/usr/sbin/afsd

#    mkdir -p $PKGROOT/Library/Kerberos\ Plug-Ins
#    ln -s ../../Library/OpenAFS/Tools/root.client/Library/Kerberos\ Plug-Ins/aklog.loginLogout $PKGROOT/Library/Kerberos\ Plug-Ins/

    chown -R root${SEP}wheel $PKGROOT/usr
    chmod -R og-w $PKGROOT/usr
fi

if [ $secondpass = yes ]; then
    rm -rf $PKGRES
    mkdir $PKGRES
    cd $RESSRC
    if [ $majorvers -ge 9 ]; then
	rm -rf $DPKGRES
	mkdir $DPKGRES
	cp License.rtf $DPKGRES
	cp InstallationCheck.$majorvers $DPKGRES/InstallationCheck
	chmod a+x $DPKGRES/InstallationCheck
	mkdir -p $DPKGRES/English.lproj
	cp InstallationCheck.strings.$majorvers \
		$DPKGRES/English.lproj/InstallationCheck.strings
	cp background.jpg $DPKGRES/background.jpg
	chown -R root${SEP}wheel $DPKGRES
	rm -rf $CURDIR/OpenAFS-debug-extension.pkg
	echo $PACKAGEMAKER -build -p $CURDIR/OpenAFS-debug-extension.pkg -f $DPKGROOT -r $DPKGRES \
	    -i OpenAFS-debug.Info.plist -d OpenAFS-debug.Description.plist
	"$PACKAGEMAKER" -build -p $CURDIR/OpenAFS-debug-extension.pkg -f $DPKGROOT -r $DPKGRES \
	    -i OpenAFS-debug.Info.plist -d OpenAFS-debug.Description.plist
    fi

    cd $RESSRC
    if [ $majorvers -ge 7 ]; then
	cp OpenAFS.post_install $PKGRES/postinstall
	cp OpenAFS.pre_upgrade $PKGRES/preupgrade
	cp OpenAFS.post_install $PKGRES/postupgrade
	cp background.jpg $PKGRES/background.jpg
	if [ $majorvers -ge 8 ]; then
	    cp InstallationCheck.$majorvers $PKGRES/InstallationCheck
	    mkdir -p $PKGRES/English.lproj
	    cp InstallationCheck.strings.$majorvers $PKGRES/English.lproj/InstallationCheck.strings
	    chmod a+x $PKGRES/InstallationCheck
	fi
	chmod a+x $PKGRES/postinstall $PKGRES/postupgrade $PKGRES/preupgrade
    else
	cp OpenAFS.post_install OpenAFS.pre_upgrade $PKGRES
	cp OpenAFS.post_install $PKGRES/OpenAFS.post_upgrade
	chmod a+x $PKGRES/OpenAFS.post_install $PKGRES/OpenAFS.post_upgrade $PKGRES/OpenAFS.pre_upgrade
    fi
    cp License.rtf $PKGRES
    cp ReadMe.rtf.$majorvers $PKGRES/ReadMe.rtf
    cp csrvdbmerge.pl $PKGRES
    chmod a+x $PKGRES/csrvdbmerge.pl
    cp CellServDB.list $PKGRES
    chown -R root${SEP}wheel $PKGRES
    rm -rf $CURDIR/OpenAFS.pkg
    if [ $majorvers -ge 7 ]; then
	echo $PACKAGEMAKER -build -p $CURDIR/OpenAFS.pkg -f $PKGROOT -r $PKGRES \
	    -i OpenAFS.Info.plist -d OpenAFS.Description.plist
	"$PACKAGEMAKER" -build -p $CURDIR/OpenAFS.pkg -f $PKGROOT -r $PKGRES \
	    -i OpenAFS.Info.plist -d OpenAFS.Description.plist
    else
	echo $PACKAGEMAKER $PKGROOT $RESSRC/OpenAFS.info -r $PKGRES
	(cd $CURDIR && "$PACKAGEMAKER" $PKGROOT $RESSRC/OpenAFS.info -r $PKGRES)
	#old versions of package didn't handle this properly
	if [ ! -r $CURDIR/OpenAFS.pkg/Contents ]; then
		mkdir -p $CURDIR/OpenAFS.pkg/Contents/Resources
		mv $CURDIR/OpenAFS.pkg/OpenAFS.* $CURDIR/OpenAFS.pkg/Contents/Resources
		mv $CURDIR/OpenAFS.pkg/*.rtf $CURDIR/OpenAFS.pkg/Contents/Resources
		mv $CURDIR/OpenAFS.pkg/csrvdbmerge.pl $CURDIR/OpenAFS.pkg/Contents/Resources
		mv $CURDIR/OpenAFS.pkg/CellServDB* $CURDIR/OpenAFS.pkg/Contents/Resources
	fi
    fi

    rm -rf $PKGROOT $PKGRES
    rm -rf $DPKGROOT $DPKGRES
    # here we want to build installer plugin and install
    # for Tiger and later only
    if [ $majorvers -ge 8 ]; then
	AFSCELLBUNDLE=$BINDEST/installer/afscell.bundle
	cd $CURDIR
	if [ -d $AFSCELLBUNDLE -a -f $BINDEST/installer/InstallerSections.plist ]; then
	    mkdir -p $CURDIR/OpenAFS.pkg/Contents/Plugins
	    cp -R $AFSCELLBUNDLE $CURDIR/OpenAFS.pkg/Contents/Plugins
	    cp $BINDEST/installer/InstallerSections.plist $CURDIR/OpenAFS.pkg/Contents/Plugins
	else
	    echo "afscell.bundle not built successfully"
	    echo "... or InstallerSections.plist file not found"
	    exit 1
	fi
    fi

    mkdir $CURDIR/dmg
    mv $CURDIR/OpenAFS.pkg $CURDIR/dmg
    if [ $majorvers -ge 8 ]; then
	mv $CURDIR/OpenAFS-debug-extension.pkg $CURDIR/dmg
    fi
    rm -rf $CURDIR/OpenAFS-@PACKAGE_VERSION@-$RELNAME.dmg
    cp $RESSRC/Uninstall $CURDIR/dmg/Uninstall.command
    cp $RESSRC/DS_Store $CURDIR/dmg/.DS_Store
    mkdir $CURDIR/dmg/.background
    cp $RESSRC/afslogo.jpg $CURDIR/dmg/.background
#    hdiutil create -srcfolder $CURDIR/dmg -volname OpenAFS -anyowners $CURDIR/OpenAFS.dmg
    hdiutil makehybrid -hfs -hfs-volume-name OpenAFS -hfs-openfolder $CURDIR/dmg $CURDIR/dmg -o $CURDIR/TMP.dmg
    hdiutil convert -format UDZO $CURDIR/TMP.dmg -o $CURDIR/OpenAFS-@PACKAGE_VERSION@-$RELNAME.dmg
    rm $CURDIR/TMP.dmg
    rm -rf $CURDIR/dmg
    # Unfortunately, sudo sets $USER to root, so I can't chown the 
    #.pkg dir back to myself
    #chown -R $USER $CURDIR/OpenAFS.pkg
else
    echo "First pass completed.  Customize pkgroot and then run:"
    echo "    $0 -secondpass"
fi
