#!/bin/bash

set -e -u

BUILD=/data/$USER/build/canflash_flashall
# git@git.seesslen.net:
GIT_SERVER=/srv/git
# Having it unclean is anoying
CLEAN=yes
DEVEL=
# /opt/tcb/tcb-1.11.x-stm32-x86_64
TOOLCHAIN=/opt/tcb/tcb-1.6.7-stm32-x86_64
TOOLCHAIN=/opt/tcb-1.8.2-stm32-x86_64
config="all.conf"
bootloader=
CMAKE_ARGS=""
REPO=
# -pedantic-errors 
# CMAKE_ARGS+="-DCMAKECFLAGS=\"-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wundef -Werror\""
# CMAKE_ARGS+=" -DCMAKE_CXX_FLAGS=\"-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wundef -Werror\""


usage() { 
    echo "Usage: $0 [-f <file>] [-d] [-c] [-b] [-p] [-r <repo>]" 1>&2;
    echo "   -f <file>   Use configuration file. Per default 'all.conf' is used." 1>&2;
    echo "   -d          Use 'devel' branches on all repositories." 1>&2;
    echo "   -c          Use clean build. Obsolete." 1>&2;
    echo "   -b          Also flash bootloader on devices." 1>&2;
    echo "   -p          Add compiler flasgs for pedantic error checking." 1>&2;
    echo "   -r <repo>   Only compile and flash the project <repo>." 1>&2;
    exit 1; 
}

flash()
{
    ${BUILD}/canflash/CANFlash -h
    BIN="${BUILD}/$PRJ/$PRJ/$PRJ.bin"
    if [ ! -e "${BIN}" ]; then
	echo "Error: Could not find binary for project $PRJ"
	exit 1
    fi

    BIN="${BUILD}/$PRJ/$PRJ/$PRJ.bin"
    if [ ! -e "${BIN}" ]; then
	echo "Error: Could not find binary for project $PRJ"
	exit 1
    fi

    BIN_BL="${BUILD}/$PRJ/$PRJ/${PRJ}_bootloader.bin"
    if [ ! -e "${BIN_BL}" ]; then
	echo "Error: Could not find bootloader binary for project $PRJ"
	exit 1
    fi

    BIN_BL_RV="${BUILD}/$PRJ/$PRJ/${PRJ}_bootloader_reverse.bin"
    if [ ! -e "${BIN_BL_RV}" ]; then
	echo "Error: Could not find (reverse) bootloader binary for project $PRJ"
	exit 1
    fi

    for n in $@; do
        if [ ${bootloader} ]; then
	    echo "### ${BUILD}/canflash/CANFlash -n $n -S localhost -w -f ${BIN_BL_RV}"
    	    echo "### ${BUILD}/canflash/CANFlash -n $n -S localhost -W -f ${BIN_BL}"
	fi
      echo "### ${BUILD}/canflash/CANFlash -n $n -S localhost -w -f ${BIN}"
      echo "### ${BUILD}/canflash/CANFlash -n $n -S localhost -B"
    done
}

parseLine()
{
    if [ $# -lt 1 ]; then
	# empty line
	return
	# echo "Count: $#"
    fi

    
    _pwd=$PWD
    echo "Line: $@"
    echo "Project: $1"
    PRJ="$1"
    shift
    BRANCH="$1"
    shift

    if [ $REPO ]; then
	if [ "$PRJ" != "$REPO" ] && [ "$PRJ" != "canflash" ] ; then
	    return
	fi
    fi

    if [ ${DEVEL} ]; then
	BRANCH=devel
    fi

    mkdir -p ${BUILD}/sources #/$PRJ
    cd ${BUILD}/sources
    [ -e $PRJ ] || git clone ${GIT_SERVER}/$PRJ
    cd $PRJ

    git fetch -a
    git checkout $BRANCH
    # Probably not on a branch but on a tag
    MY_BRANCH=$(git symbolic-ref -q HEAD) || echo -n
    if [ ! -z "${MY_BRANCH}" ]; then
	git pull
    fi
    git submodule init
    git submodule update
    if [ ${DEVEL} ]; then
        git submodule foreach git co devel
        git submodule foreach git pull
    fi

    mkdir -p ${BUILD}/$PRJ
    cd ${BUILD}/$PRJ

    if [ ${CLEAN} ]; then
        rm -rf *
    fi

    echo "### cmake ${BUILD}/sources/$PRJ -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo ${CMAKE_ARGS}"

    if [ "$PRJ" != "canflash" ]; then
        cmake ${BUILD}/sources/$PRJ \
    	-DCMAKE_C_COMPILER=${TOOLCHAIN}/usr/bin/arm-none-eabi/arm-none-eabi-gcc \
    	-DCMAKE_CXX_COMPILER=${TOOLCHAIN}/usr/bin/arm-none-eabi/arm-none-eabi-g++ \
	-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
	${CMAKE_ARGS}
    else
        cmake ${BUILD}/sources/$PRJ \
	-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
	${CMAKE_ARGS}
    fi

    make

    if [ "$PRJ" != "canflash" ]; then
        flash $@
    fi

    cd ${_pwd}
}


while getopts "cf:bdpr:" o; do
    case "${o}" in
        c)
	    CLEAN=yes
	    ;;
	f)
            config=${OPTARG}
            ;;
        b)
            bootloader=yes
            ;;
        d)
            DEVEL=yes
            ;;
	p)
	    export CFLAGS="-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wundef -Werror -pedantic-errors"
	    export CXXFLAGS="-Wall -Wextra -Wpedantic -Wconversion -Wshadow -Wundef -Werror -pedantic-errors"
            ;;
	r)
	    REPO=${OPTARG}
	    ;;
        *)
	    echo "Unknown param: ${o}"
	    ## "${OPTARG}"
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${config}" ]; then
    usage
fi

if [ $bootloader ]; then
    echo "Bootloader"
fi

# parseLine canflash devel none

while read l; do
    if [ "${l:0:1}" != "#" ]; then
        parseLine $l
    fi
done < $config

echo "[$0] Fin."

#--- Fin .---------------------------------------------------------------------
