#!/bin/sh

set -e

if [ ! -f configure-tms470.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 1 ]
then
	echo "usage: $0 [--clean] target [vars]" 1>&2
	exit 1
fi

TARGET="$1"

if [ ! -f "targets/$TARGET.conf" -o ! -f "targets/$TARGET.h" ]
then
	echo "Error: target $TARGET 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"

BUILD_DIR="build-$TARGET-tms470"

# 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

# miscellaneous configurable feature settings

ALLOW_CSIM_GSM=1
SERIAL_DYNAMIC_SWITCH=0
TR_BAUD_CONFIG=TR_BAUD_115200

# allow the user to override these defaults

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

echo "Building firmware 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 [ $# != 1 ]
	then
		echo "Error: build_lib takes 1 argument" 1>&2
		exit 1
	fi
	scripts/mk-component.sh "$1"
	SUBDIR="$SUBDIR $1"
	current_lib=$1/$1.lib
	LIBS="$LIBS $current_lib"
}

rts_blob_lib() {
	if [ $# != 1 ]
	then
		echo "Error: rts_blob_lib takes 1 argument" 1>&2
		exit 1
	fi
	current_lib="../tms470/rts/$1.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
export CHIPSET DSP RF
export AMR L1_DYN_DSP_DWNLD L1_VOICE_MEMO_AMR MELODY_E2 SPEECH_RECO
export DISABLE_SLEEP
export ALLOW_CSIM_GSM SERIAL_DYNAMIC_SWITCH TR_BAUD_CONFIG
export GPRS SRVC FCHG_STATE

scripts/config-headers.sh

SUBDIR=
LIBS=
SPECIAL_LINK_LIBS=
current_lib=

build_lib ccddata

# ACI libs
build_lib aci
build_lib aciext
build_lib aci_dti_mng
build_lib atiext
build_lib comlib

if [ "$GPRS" = 1 ]
then
	build_lib config_gprs_fl
	build_lib config_gprs_ir
else
	build_lib config_gsm_fl
	build_lib config_gsm_ir
fi
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'

# G23M protocol stack libs
build_lib alr
build_lib cc
build_lib dl
build_lib dti
build_lib l1_pei
build_lib mm
build_lib rr
build_lib sim_b_lib
build_lib sms
build_lib ss
build_lib uart_b_lib

# fax and data
if [ "$SRVC" != 0 ]
then
	build_lib l2r
	build_lib ra
	build_lib rlp
fi

# fax specific
if [ "$SRVC" = 1 ]
then
	build_lib fad
	build_lib t30
fi

# GPRS specific
if [ "$GPRS" = 1 ]
then
	build_lib cci
	build_lib cci_ir
	lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
	build_lib cl
	build_lib gmm
	build_lib grlc
	build_lib grlc_ir
	lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
	build_lib grr
	build_lib llc
	build_lib sm
	build_lib sndcp
	build_lib ppp
	build_lib ppp_ir
	lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
	build_lib upm
fi

# Condat drivers
build_lib gdi

# GPF
build_lib ccd_na7_db
build_lib frame_na7_db_fl
build_lib frame_na7_db_ir
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
build_lib misc_na7_db_fl
build_lib misc_na7_db_ir
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
build_lib osx_na7_db
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
build_lib tif_na7_db_fl
build_lib tif_na7_db_ir
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'

# core drivers
build_lib drivers_flash

# Layer 1
build_lib tpudrv
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
build_lib l1_ext
lib_link_magic '(BSS_LIBS (.l1s_global))'
build_lib l1_int
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'
build_lib l1_custom_ext
build_lib l1_custom_int
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'

build_lib riviera_core_flash
build_lib riviera_cust_flash

# services
build_lib audio
build_lib audio_bgd
build_lib cst
build_lib dar
build_lib dar_gbl_var
lib_link_magic '(BSS_DAR_LIB (.bss))'
build_lib etm
build_lib lls

# app drivers
build_lib abb
build_lib buzzer
if [ "$FCHG_STATE" = 1 ]
then
	build_lib fchg
fi
build_lib ffs
build_lib ffs_drv
build_lib ffs_pcm
build_lib kpd
build_lib power
build_lib rtc_drv
build_lib sim_drv
build_lib spi_drv
build_lib uart_drv

# system glue
build_lib main
build_lib bootloader
lib_link_magic '(CONST_BOOT_LIB (.const, .text, .text:v$3) BSS_BOOT_LIB (.bss))'

# Nucleus
build_lib nucleus_flash
build_lib nucleus_intram
lib_link_magic '(BSS_LIBS (.bss) CONST_LIBS (.text, .const))'

# TI's libc/libgcc equivalent
rts_blob_lib rts16le_flash
rts_blob_lib rts16le_int_ram
lib_link_magic '(BSS_LIBS (.bss))'

# generate the top level 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 tms470/makefile-frags/first-part >> $BUILD_DIR/Makefile

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

if [ "$TARGET" != c139 -a "$TARGET" != c11x ]
then
	cat tms470/makefile-frags/m0-to-bin-std >> $BUILD_DIR/Makefile
else
	cat tms470/makefile-frags/m0-to-bin-c139 >> $BUILD_DIR/Makefile
fi

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

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

cat tms470/makefile-frags/clean-always >> $BUILD_DIR/Makefile
echo >> $BUILD_DIR/Makefile
echo 'FRC:' >> $BUILD_DIR/Makefile

# All done!

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