#!/bin/sh

set -e

if [ ! -f configure.sh ]
then
	echo "This script needs to be run from the top of the source tree" 1>&2
	exit 1
fi

if [ ! -f helpers/makeline ]
then
	echo "Please run make in the helpers directory first" 1>&2
	exit 1
fi

# start looking at our invokation line

if [ "$1" = --clean ]
then
	clean_flag=1
	shift
else
	clean_flag=0
fi

if [ $# -lt 2 ]
then
	echo "usage: $0 [--clean] target config [vars]" 1>&2
	exit 1
fi

TARGET="$1"
CONFIG="$2"

if [ ! -f "targets/$TARGET.conf" -o ! -f "targets/$TARGET.h" ]
then
	echo "Error: target $TARGET not known" 1>&2
	exit 1
fi

if [ ! -f "configs/$CONFIG" ]
then
	echo "Error: configuration $CONFIG not known" 1>&2
	exit 1
fi

# target defaults that can be overridden by $TARGET.conf

CHIPSET=10
DSP=36
RF=12
DISABLE_SLEEP=0

. "targets/$TARGET.conf"

# settings derived from the target-defined DSP version

case "$DSP" in
	33)
		AMR=0
		L1_DYN_DSP_DWNLD=0
		L1_VOICE_MEMO_AMR=0
		MELODY_E2=0
		SPEECH_RECO=0
		;;
	34)
		AMR=1
		L1_DYN_DSP_DWNLD=0
		L1_VOICE_MEMO_AMR=0
		MELODY_E2=0
		SPEECH_RECO=0
		;;
	36)
		AMR=1
		L1_DYN_DSP_DWNLD=1
		L1_VOICE_MEMO_AMR=1
		MELODY_E2=1
		SPEECH_RECO=1
		;;
	*)
		echo "Error: DSP=$DSP setting not understood" 1>&2
		exit 1
esac

# UI and RVTMUX config default based on target

if [ "$TARGET" = c11x -o "$TARGET" = c139 -o "$TARGET" = c155 ]
then
	UI_CONFIG=84x48
	RVTMUX_ON_MODEM=1
else
	UI_CONFIG=bigcolor
	RVTMUX_ON_MODEM=0
fi

# display driver configuration

DSAMPLE_FULL_COLOR=1

# miscellaneous configurable feature settings

ALLOW_CSIM_GSM=1
SERIAL_DYNAMIC_SWITCH=0
TRACEMASK_IN_FFS=0
TR_BAUD_CONFIG=TR_BAUD_115200
USE_STR2IND=0

SUFFIX=

# allow the user to override these defaults

shift
shift
while [ $# != 0 ]
do
	eval "$1"
	shift
done

BUILD_DIR="build-$TARGET-$CONFIG$SUFFIX"

echo "Building configuration $CONFIG for target $TARGET in $BUILD_DIR"

if [ "$clean_flag" = 1 ]
then
	rm -rf $BUILD_DIR
fi
mkdir -p $BUILD_DIR

: > $BUILD_DIR/lcfgen

# shell functions to be used in the configuration recipe

build_lib() {
	if [ $# -lt 1 -o $# -gt 2 ]
	then
		echo "Error: build_lib takes 1 or 2 arguments" 1>&2
		exit 1
	fi
	scripts/mk-component.sh "$1" "$2"
	SUBDIR="$SUBDIR $1"
	current_lib=$1/$1.lib
	LIBS="$LIBS $current_lib"
}

blob_lib_std() {
	if [ $# != 1 ]
	then
		echo "Error: blob_lib_std takes 1 argument" 1>&2
		exit 1
	fi
	current_lib="../blobs/libs/$1.lib"
	LIBS="$LIBS $current_lib"
}

blob_lib_gpf() {
	if [ $# != 1 ]
	then
		echo "Error: blob_lib_gpf takes 1 argument" 1>&2
		exit 1
	fi
	current_lib="../blobs/gpflibs/$1.lib"
	LIBS="$LIBS $current_lib"
}

blob_lib_os() {
	if [ $# != 1 ]
	then
		echo "Error: blob_lib_os takes 1 argument" 1>&2
		exit 1
	fi
	current_lib="../blobs/oslibs/$1.lib"
	LIBS="$LIBS $current_lib"
}

blob_lib_custom() {
	if [ $# != 2 ]
	then
		echo "Error: blob_lib_custom takes 2 arguments" 1>&2
		exit 1
	fi
	if [ -z "$1" ]
	then
		echo "Error: this config is not supported on this target" 1>&2
		exit 1
	fi
	cp $1 $BUILD_DIR/$2.lib
	current_lib=$2.lib
	LIBS="$LIBS $current_lib"
}

lib_link_magic() {
	if [ $# != 1 ]
	then
		echo "Error: lib_link_magic takes 1 argument" 1>&2
		exit 1
	fi
	if [ -z "$current_lib" ]
	then
	  echo "Error: lib_link_magic called before build_lib or blob_lib" 1>&2
	  exit 1
	fi
	SPECIAL_LINK_LIBS="$SPECIAL_LINK_LIBS $current_lib"
	echo "$1" >> $BUILD_DIR/lcfgen
}

# invoke the configuration recipe

export BUILD_DIR TARGET USE_STR2IND
export CHIPSET DSP RF
export AMR L1_DYN_DSP_DWNLD L1_VOICE_MEMO_AMR MELODY_E2 SPEECH_RECO
export DISABLE_SLEEP
export INIT_blob
export DSAMPLE_FULL_COLOR RVTMUX_ON_MODEM UI_CONFIG
export ALLOW_CSIM_GSM SERIAL_DYNAMIC_SWITCH TR_BAUD_CONFIG
export TRACEMASK_IN_FFS

SUBDIR=
LIBS=
SPECIAL_LINK_LIBS=
current_lib=

. "configs/$CONFIG"

# str2ind magic

if [ -z "$str2ind_blobs_used" ]
then
	echo "Error: configs/$CONFIG must set str2ind_blobs_used" 1>&2
	exit 1
fi

if [ "$str2ind_blobs_used" = 1 ]
then
	cp blobs/str2ind.tab $BUILD_DIR
fi

if [ "$str2ind_blobs_used" = 0 -a "$USE_STR2IND" = 0 ]
then
	echo 'char *str2ind_version = "&0";' > $BUILD_DIR/str2ind.c
fi

# generate the top level Makefile!

helpers/makeline def CONFIG_NAME "$CONFIG$SUFFIX" > $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
helpers/makeline def TARGET $TARGET >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
helpers/makeline def SUBDIR $SUBDIR >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
helpers/makeline def LIBS $LIBS >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
helpers/makeline def SPECIAL_LINK_LIBS $SPECIAL_LINK_LIBS >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
helpers/makeline def LINK_SCRIPT_SRC ../$LINK_SCRIPT_SRC >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile

if [ -n "$RAM_LINK_SCRIPT_SRC" ]
then
	helpers/makeline def RAM_LINK_SCRIPT_SRC ../$RAM_LINK_SCRIPT_SRC \
		>> $BUILD_DIR/Makefile
	echo >> $BUILD_DIR/Makefile
fi

helpers/makeline def FLASH_BASE_ADDR $FLASH_BASE_ADDR >> $BUILD_DIR/Makefile
helpers/makeline def FLASH_SECTOR_SIZE $FLASH_SECTOR_SIZE >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile

cat makefile-frags/first-part >> $BUILD_DIR/Makefile

if [ "$USE_STR2IND" = 1 ]
then
	cat makefile-frags/str2ind-tab-depend >> $BUILD_DIR/Makefile
fi

if [ "$str2ind_blobs_used" = 1 -o "$USE_STR2IND" = 1 ]
then
	cat makefile-frags/str2ind-c-gen >> $BUILD_DIR/Makefile
fi

cat makefile-frags/link-steps >> $BUILD_DIR/Makefile

case "$TARGET" in
	c11x|c139)
		cat makefile-frags/m0-to-bin-c139 >> $BUILD_DIR/Makefile
		;;
	c155)
		cat makefile-frags/m0-to-bin-c155 >> $BUILD_DIR/Makefile
		;;
	*)
		cat makefile-frags/m0-to-bin-std >> $BUILD_DIR/Makefile
		;;
esac

cat makefile-frags/flash-script-gen >> $BUILD_DIR/Makefile

if [ -n "$RAM_LINK_SCRIPT_SRC" ]
then
	cat makefile-frags/ram-link-steps >> $BUILD_DIR/Makefile
fi

cat makefile-frags/clean-always >> $BUILD_DIR/Makefile
if [ "$str2ind_blobs_used" = 0 ]
then
	echo '	rm -f str2ind.tab str2ind.log' >> $BUILD_DIR/Makefile
fi
if [ "$str2ind_blobs_used" = 1 -o "$USE_STR2IND" = 1 ]
then
	echo '	rm -f str2ind.c' >> $BUILD_DIR/Makefile
fi
echo >> $BUILD_DIR/Makefile
echo 'FRC:' >> $BUILD_DIR/Makefile

# All done!

echo "Run make in $BUILD_DIR to compile the firmware"
