#!/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 [-build build] [-project project] [-train train] radar-number-or-label
#

build=""
train=""
label=""
project="cups"
buildall="-buildAllAliases"

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

        case "$opt" in
        	-build)
        		build="$1"
        		shift
        		;;
        	-project)
        		project="$1"
			if test $project != cups; then
				buildall="-noBuildAllAliases"
			fi
        		shift
        		;;
        	-train)
        		train="$1"
        		shift
        		;;
        	-*)
        		echo "Usage: makedarwinup [-build build] [-project project] [-train name] radar-number-or-label"
        		exit 1
        		;;
        	*)
        		label="$opt"
        		;;
        esac
done

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

if test "x$train" = x; then
	case $project in
		cups_ios*)
			if test "x$build" = x; then
				build=`xcodebuild -sdk iphoneos -version ProductBuildVersion`
			fi

			train=`xbs getTrainForBuild --embedded $build --quiet`
			;;
		*)
			if test "x$build" = x; then
				build=`xcodebuild -sdk macosx -version ProductBuildVersion`
			fi

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

# See whether we are running at Apple or remotely...
address="`ifconfig en0 | grep 'inet 17.' | awk '{print $2}'`"
if test "x$address" = x; then
	tprefix="Stashed"
else
	tprefix="Prevailing"
fi

echo Building $project for $train...

echo sudo xbs buildit -update $tprefix$train -project $project -useSharedSDKContentRoot -sharedSDKContentRoot /private/tmp/printing.$train -archive -archiveName $project-$label -archiveOutputDir ~/Desktop -dsymsInDstroot $buildall -noverify -codesign .
sudo xbs buildit -update $tprefix$train -project $project -useSharedSDKContentRoot -sharedSDKContentRoot /private/tmp/printing.$train -archive -archiveName $project-$label -archiveOutputDir ~/Desktop -dsymsInDstroot $buildall -noverify -codesign .

if test $? = 0; then
	say "Darwin up build successful."
else
	say "Darwin up build failed."
	exit 1
fi

if test $project = cups; then
	rm -f ~/Desktop/Shared_$project-${label}_HDRDSTROOT_*.tar.gz
	mv -f ~/Desktop/Shared_$project-${label}_SDKContentRoot_*.tar.gz ~/Desktop/$project-sdk-$label.tar.gz
	mv -f ~/Desktop/Shared_$project-${label}_DSTROOT_*.tar.gz ~/Desktop/$project-$label.tar.gz
else
	rm -f ~/Desktop/$project-${label}_APIDSTROOT*.tar.gz
	mv -f ~/Desktop/$project-${label}_SDKContentRoot*.tar.gz ~/Desktop/$project-sdk-$label.tar.gz
	mv -f ~/Desktop/$project-${label}_DSTROOT*.tar.gz ~/Desktop/$project-$label.tar.gz
fi

echo "Send ~/Desktop/$project-$label.tar.gz to tester."

if test $project = cups; then
	echo ""
	echo "Use the following commands to install the root:"
	echo ""
	echo "  sudo darwinup install ~/Desktop/$project-$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 $project-$label.tar.gz"
	echo "  sudo killall cupsd"
fi
