#! /bin/bash

export DATE=$(date -u +%Y%m%d)

set -x
set -e

: ${RETRY:=false}

# process options
while true; do
case $1 in
--retry)
 	export RETRY=true
	shift
;;
--*)
	echo "unknown option $1"
	exit 2
;;
*)
	break
;;
esac
done

# Configuration
if [ -n "$1" ] ; then
    source $1
else
    source build.def
fi

case $BUILD_TYPE in
branch)
	DEF_BUILD_NAME=$SRC_RELEASE-$DATE
;;
tag)
	DEF_BUILD_NAME=$SRC_RELEASE
;;
*)
	echo "bad BUILD_TYPE=$BUILD_TYPE"
	exit 2
;;
esac

if [ -z "$BUILD_NAME" ]; then
	BUILD_NAME=$DEF_BUILD_NAME
fi

first-word() {
	echo "$1"
}

export JOB_DIR=$(pwd)
export DOWNLOAD_PATH=$JOB_DIR/manual-dependencies
export WORK_DIR=$JOB_DIR/$BUILD_NAME
export AUTO_INSTALL=yes
export FIRST_CONFIG=$(first-word $BUILD_CONFIGS)
export SRC_REF=release-reference-src

export BUILD_NAME BUILD_TYPE BOOTSTRAP_URL SRC_TAR_URL BUILD_CONFIG SRC_RELEASE

rm -rf $JOB_DIR/output >/dev/null 2>&1 || true
export OUTPUT=$JOB_DIR/output
mkdir -p $OUTPUT

if ! $RETRY ; then 
    echo "Removing any prior work area"
    rm -rf $WORK_DIR >/dev/null 2>&1 ||  true
fi

mkdir -p $WORK_DIR

local-prep() {
	echo "nothing to do in this release"
}

make-source-tarball() {
	# set up machine enough for bootstrap to work
	# (this should be part of bootstrap )
	if ! which git ; then
		if ! which apt-get ; then
			echo "don't have git and don't know how to install it"
			exit 2
		fi
		echo "installing git"
		sudo apt-get install -y git-core
	fi

	cd $WORK_DIR
	mkdir -p $SRC_REF

	# fetch and setup the project
	wget -O bootstrap $BOOTSTRAP_URL
	chmod +x bootstrap

	./bootstrap $SRC_RELEASE

	# we need to config to get uclibc source
	cd $WORK_DIR/linux-c6x-project
	./prj config $JOB_DIR/$FIRST_CONFIG

	# record reference information
	./prj git rev-parse HEAD >../$SRC_REF/src-release.src-record
	(cd $JOB_DIR; md5sum -b manual-dependencies/* >$WORK_DIR/$SRC_REF/manual-dependencies.md5sums)

	# clean up
	rm ../bootstrap
	./prj git clean -fdx
	./prj git reset --hard

	# tar it up
	cd $WORK_DIR/..
	tar czf $OUTPUT/$BUILD_NAME-src.tar.gz --exclude=".git" --exclude="product" --exclude="sdk" --exclude="Build" --exclude="gcc-c6x" --exclude="downloads" $BUILD_NAME
}

fake-build() {
	mkdir -p ../product; echo Hello >../product/vmlinux-$this_config
}

one-bin-build() {
	this_config_file=$1
	this_config=${this_config_file##setenv-}
	this_config=${this_config%%.build}

	THIS_REF=release-reference-$this_config-bin

	rm -rf $BUILD_NAME/$THIS_REF $BUILD_NAME/product || true
	if ! $RETRY ; then 
	    rm -rf $BUILD_NAME/Build $BUILD_NAME/opt || true
	fi
	mkdir -p $WORK_DIR/$THIS_REF

	cd $WORK_DIR/linux-c6x-project
	./prj config $JOB_DIR/$this_config_file
	source $(pwd)/setenv

	# build it
#	if fake-build ; then
	if make product ; then
		echo "Build OK"
	else
		echo "Build FAILED"; exit 2
	fi

	# package up this binary release
	cd $WORK_DIR
	cp $JOB_DIR/$this_config_file $THIS_REF
	cp linux-c6x-project/.setenv.local $THIS_REF/setenv-$this_config.local
	cp $SRC_REF/* $THIS_REF/
	cd $WORK_DIR/..
	tar czf $OUTPUT/$BUILD_NAME-$this_config-bin.tar.gz $BUILD_NAME/$THIS_REF $BUILD_NAME/product
}

# look for special commands
if [ "$1" == "local-prep" ]; then
    local-prep
    exit $?
fi

(make-source-tarball)

for config in $BUILD_CONFIGS; do
	(one-bin-build $config)
done
