#!/bin/sh

set -e

if [ ! -f configure-gcc.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

if [ ! -f "targets/$TARGET.m4" ]
then
	echo "Error: target $TARGET is not supported with gcc" 1>&2
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

# miscellaneous configurable feature settings

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

SUFFIX=

# allow the user to override these defaults

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

# validate key settings and build configuration name

case "$SRVC" in
	0)
		CONFIG_NAME=vo
		;;
	1)
		CONFIG_NAME=fd
		;;
	2)
		CONFIG_NAME=da
		;;
	*)
		echo "Error: SRVC=$SRVC setting is invalid" 1>&2
		exit 1
esac

case "$GPRS" in
	0)
		;;
	1)
		CONFIG_NAME=gprs
		;;
	*)
		echo "Error: GPRS=$GPRS setting is invalid" 1>&2
		exit 1
esac

case "$FCHG_STATE" in
	0)
		;;
	1)
		CONFIG_NAME="$CONFIG_NAME-chg"
		;;
	*)
		echo "Error: FCHG_STATE=$FCHG_STATE setting is invalid" 1>&2
		exit 1
esac

CONFIG_NAME="$CONFIG_NAME$SUFFIX-gcc"
BUILD_DIR="build-$TARGET-$CONFIG_NAME"

echo "Building firmware for target $TARGET in $BUILD_DIR"

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

# 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-gcc.sh "$1"
	SUBDIR="$SUBDIR $1"
	LIBS="$LIBS $1/$1.a"
}

# 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 MEMSUPER TRACEMASK_IN_FFS
export RVTMUX_ON_MODEM SERIAL_DYNAMIC_SWITCH TR_BAUD_CONFIG
export GPRS SRVC FCHG_STATE

scripts/config-headers.sh

SUBDIR=
LIBS=

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

# 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
	build_lib cl
	build_lib gmm
	build_lib grlc
	build_lib grlc_ir
	build_lib grr
	build_lib llc
	build_lib sm
	build_lib sndcp
	build_lib ppp
	build_lib ppp_ir
	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
build_lib misc_na7_db_fl
build_lib misc_na7_db_ir
build_lib osx_na7_db
build_lib tif_na7_db_fl
build_lib tif_na7_db_ir

# core drivers
build_lib drivers_flash

# Layer 1
build_lib tpudrv
build_lib l1_ext
build_lib l1_int
build_lib l1_custom_ext
build_lib l1_custom_int

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
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 main_ir

# Nucleus
build_lib nucleus_flash
build_lib nucleus_intram

# selective libc replacement
build_lib libsys_fl
build_lib libsys_ir

# generate the top level Makefile!

helpers/makeline def CONFIG_NAME $CONFIG_NAME > $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 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 gcc/makefile-body >> $BUILD_DIR/Makefile

# All done!

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