#!/bin/sh
#
# Script to make a CUPS root compatible with the darwinup command.  Standard
# instructions for people getting these roots are:
#
#    sudo darwinup install ~/Desktop/cups-LABEL
#    sudo killall cupsd
#    cupsctl --debug-logging
#
# Usage:
#
#   makedarwinup radar-number-or-label
#

if test $# != 1; then
	echo "Usage: makedarwinup [-build build] [-train name] radar-number-or-label"
	exit 1
fi

build=""
train=""

while test $# -gt 0; do
        opt="$1"
        shift

        case "$opt" in
        	-build)
        		build="$1"
        		shift
        		;;
        	-train)
        		train="$1"
        		shift
        		;;
        	-*)
        		echo "Usage: makedarwinup [-build build] [-train name] radar-number-or-label"
        		exit 1
        		;;
        	*)
        		label="$opt"
        		;;
        esac
done

if test "x$train" = x; then
	if test "x$build" = x; then
		build=`sw_vers -buildVersion`
	fi

	train=`xbs getTrainForBuild $build --quiet`
fi

echo Building cups for $train...

echo sudo xbs buildit . -update Prevailing$train -project cups -arch i386 -arch x86_64 -rootsDirectory /private/var/tmp AD_HOC_CODE_SIGN_IDENTITY=- -archive -archiveName cups-$label -archiveOutputDir ~/Desktop -dsymsInDstroot -buildAllAliases -noverify
sudo xbs buildit . -update Prevailing$train -project cups -arch i386 -arch x86_64 -rootsDirectory /private/var/tmp AD_HOC_CODE_SIGN_IDENTITY=- -archive -archiveName cups-$label -archiveOutputDir ~/Desktop -dsymsInDstroot -buildAllAliases -noverify || exit 1
rm -f ~/Desktop/Shared_cups-${label}_HDRDSTROOT_osx.tar.gz
mv -f ~/Desktop/Shared_cups-${label}_DSTROOT_osx.tar.gz ~/Desktop/cups-$label.tar.gz

echo "Send ~/Desktop/cups-$label.tar.gz to tester."
echo ""
echo "Use the following commands to install the root:"
echo ""
echo "  sudo darwinup install ~/Desktop/cups-$label.tar.gz"
echo "  sudo killall cupsd"
echo "  cupsctl --debug-logging"
echo ""
echo "Use the following commands to remove the root:"
echo ""
echo "  sudo darwinup uninstall cups-$label.tar.gz"
echo "  sudo killall cupsd"
