#!/bin/bash

BUILDROOT=$PWD
BSPRELEASE=rocko
BSPPATH=$BUILDROOT/fsl-community-bsp
BSPGIT=https://github.com/Freescale/fsl-community-bsp-platform
LOCALCONFFILE=$BSPPATH/build/conf/local.conf
BBLAYERSFILE=$BSPPATH/build/conf/bblayers.conf
METALAYERGIT=https://github.com/DeviceSolutions/meta-opal6-demos.git
METALAYERMASTERBRANCH=rocko

LOCALCONFOPAL6='ACCEPT_FSL_EULA = "1" \
IMAGE_FSTYPES = "tar.bz2" \
LICENSE_FLAGS_WHITELIST = " commercial_libav commercial_x264 commercial" \
\
'

BBLAYERSOPAL6='\
  \\ \
  ${BSPDIR}/sources/meta-openembedded/meta-python \\ \
  ${BSPDIR}/sources/meta-openembedded/meta-networking \\ \
  ${BSPDIR}/sources/meta-openembedded/meta-gnome \\ \
  ${BSPDIR}/sources/meta-openembedded/meta-xfce \\ \
  ${BSPDIR}/sources/meta-opal6-demos \\ \
  ${BSPDIR}/sources/meta-mono \\ \
'

EXTRAIMAGEFEATURES=' tools-debug tools-sdk'

function install_dependencies
{
	sudo apt-get install gawk wget git-core diffstat unzip texinfo build-essential chrpath libsdl1.2-dev xterm curl

	mkdir -p $HOME/bin
	curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > $HOME/bin/repo
	chmod a+x $HOME/bin/repo
	export PATH=$PATH:$HOME/bin
	
	echo "Configure git user and email and run again"
	echo "git config --global user.name <your name>"
	echo "git config --global user.email <your email>"
	exit 1
}

function install_yocto
{
	echo Installing YOCTO
	mkdir -p $BSPPATH
	cd $BSPPATH
	$HOME/bin/repo init -u $BSPGIT -b $BSPRELEASE
	$HOME/bin/repo sync
	MACHINE=opal6q DISTRO=fslc-wayland source setup-environment build | read -p 'q'
}

function install_meta_opal6_demos
{
	echo Installing meta-opal6-demos
	cd $BSPPATH/sources
	git clone -b $METALAYERBRANCH $METALAYERGIT
}

function install_meta_mono
{
	echo Installing meta_mono
	cd $BSPPATH/sources
	git clone -b $BSPRELEASE git://git.yoctoproject.org/meta-mono.git
}

function configure_yocto
{
	echo Configuring YOCTO
	echo Configuring local.conf
	sed -i 's/MACHINE ??= .*/MACHINE ??= \"opal6q\"/' $LOCALCONFFILE
	sed -i 's/\(EXTRA_IMAGE_FEATURES = \".*\)\"/\1'"$EXTRAIMAGEFEATURES"'\"/' $LOCALCONFFILE
	sed -i '$s/^/'"$LOCALCONFOPAL6"'/' $LOCALCONFFILE
	echo Configuring bblayers.conf
	sed -i ':a;N;$!ba;s|BBLAYERS = \"\(.*\)\n\"|BBLAYERS = \"\1'"$BBLAYERSOPAL6"'\"|' $BBLAYERSFILE
	echo done.
}

function show_help
{
	echo "Usage: sh install_opal6.sh [OPTION]..."
	echo "Install YOCTO build system for opal6 boards"
	echo "  -c, --clean          Clean old YOCTO repository"
	echo "  -b, --branch=LABEL   Set opal6 branch to LABEL"
	echo "  -r, --releases       List available release branches from server"
	echo "  -h, --help           Show help"
	echo "e.g. sh install_opal6.sh -c -l opal6-4.1.15-001"
}

### Install ###
unset CLEAN
unset LABEL
unset ENVONLY
unset LISTRELEASES

while [ "$#" -gt 0 ]; do
	case "$1" in
		-b) LABEL="$2"; shift 1;;
		-c|--clean) CLEAN=y;;
		-h|--help) show_help; exit 1;;
		-e) ENVONLY=y;;
		-r|--releases) LISTRELEASES=y;;
		
		--branch=*) LABEL="${1#*=}"; shift 1;;
    	--branch) echo "$1 requires an argument" >&2; exit 1;;

		-*) echo "unknown option: $1" >&2; exit 1;;
		*) echo Unknown argument "$1";;
	esac
	shift 1
done

if [ "$LISTRELEASES" = "y" ]
then
	echo Available release branches: 
	git ls-remote $METALAYERGIT | sed -n 's/.*refs\/heads\/\(opal6.*\)/\1/p'
	exit 1
fi

if [ -z "$ENVONLY" ]
then
	if [ -z "$LABEL" ]
	then
		METALAYERBRANCH=$METALAYERMASTERBRANCH
		echo No release branch provided, defaulting to \"$METALAYERBRANCH\"
	else
		if [[ "$LABEL" =~ "opal6" ]]
		then
			METALAYERBRANCH=$LABEL
			echo Release branch set to \"$METALAYERBRANCH\"
		else
			echo Invalid release branch \"$LABEL\"
			exit 1
		fi
	fi

	if [ -d "$BSPPATH" ]
	then
		if [ -z "$CLEAN" ]
		then
			echo YOCTO $BSPRELEASE files already exist. Delete manually or use '-c' flag to auto remove.
			echo Note that package downloads folder will be left intact. These need to be delete manually.
			exit 1
		else
			echo Deleting old YOCTO files.
			rm -rf $BSPPATH/.repo
			rm -rf $BSPPATH/sources
			rm -rf $BSPPATH/build
		fi
	fi
	
	# Assume if repo tool doesn't exist then this is a clean install and needs all dependencies installed
	if [ ! -f "$HOME/bin/repo" ]
	then
		echo Installing dependencies
		install_dependencies
	fi

	mkdir -p $BUILDROOT
	cd $BUILDROOT
	install_yocto
	install_meta_opal6_demos
	install_meta_mono
	configure_yocto
	cd $BUILDROOT
else
	echo "Environment loaded."
fi
