#!/bin/bash

# Init-Datei fuer Out-of-Tree-Build

o_path=$PWD
br_dir=$(realpath $(dirname "$0")/../..)

defcfg=cv_octavo_prod_br2023_defconfig
defcfg_short=${defcfg:0:${#defcfg}-10}
ubootcfg_dir=$br_dir/board/cv/cv_octavo_produktion/u-boot/
ubootcfg=cv_prod_34_39_500_defconfig
export bootscr_dir=$br_dir/board/cv/cv_octavo_produktion/boot_scripts/
export bootscr=produktion.cmd

help()
{
    echo "To build a linux image, create a directory, go into that directory, and call this script:"
    echo "i.e. your cv-buildroot dir is in /home/user/cvbuildroot and your output directory should be"
    echo "/home/user/output_updcard:"
    echo "    mkdir /home/user/output_output_updcard"
    echo "    cd /home/user/output_output_updcard"
    echo "    ../cvbuildroot/board/cv/"$(basename "$0")
    echo "    ../output_stdflash/images/cv_source_or_exec_me.sh"
    echo "    make"
    echo "    dd if=images/sdcard.img of=/dev/sdb bs=1MB"
    echo "Replace /dev/sdb with the path of your sd card!"
    echo "-h: help"
    echo "-o: old board, 34_39_500 instead 34_39_500A/B/..."
    echo "-d x: boot messages"
    echo "   -d 0: no boot messages (default)"
    echo "   -d 1: boot messages on display"
    echo "   -d 2: boot messages and console on serial"
    echo "-t: build a toolchain too"
    echo "-s: local SVN repos for uboot and linux (CREATE_SVN_REPOS=0 is set by default)"
    echo "-f: new FPGA KiTi-ECP5"
    exit
}

old=0
tc=0
bootdisp=0
nosvn=1
kiti=0
while getopts hod:tsf flag
do
    case "${flag}" in
        h) help;;
        o) old=1;;
        d) bootdisp=${OPTARG};;
        t) tc=1;;
        s) nosvn=0;;
        f) kiti=1;;
    esac
done

rm -f $ubootcfg_dir/new_$ubootcfg
rm -f $ubootcfg_dir/$ubootcfg\_old
rm -f $br_dir/configs/new_$defcfg
rm -f $br_dir/configs/$defcfg_short\_new_defconfig
rm -f $br_dir/configs/$defcfg_short\_old_defconfig
rm -f $br_dir/configs/$defcfg_short\_with_tc_defconfig
echo "" > config_vars.sh

if [ "$tc" -eq "1" ]; then
    gawk -f $br_dir/board/cv/common/add_toolchain_2config.awk VERSION="2023" $br_dir/configs/$defcfg > $br_dir/configs/$defcfg_short\_with_tc_defconfig
    defcfg=$defcfg_short\_with_tc_defconfig
    defcfg_short=${defcfg:0:${#defcfg}-10}
fi

if [ "$old" -eq "1" ]; then
    gawk '{ if (/CONFIG_CONS_INDEX/) {print("CONFIG_CONS_INDEX=1");} else {print($0);} }' $ubootcfg_dir/$ubootcfg > $ubootcfg_dir/$ubootcfg\_old
    sed -e 's/34_39_500A board\/cv\/common\/kernel\/kernel-patches-2/34_39_500/' $br_dir/configs/$defcfg > $br_dir/configs/temp_$defcfg
    gawk -v UBOOTCFG_DIR=$ubootcfg_dir -v UBOOTCFG=$ubootcfg '{ if (/BR2_TARGET_UBOOT_CUSTOM_CONFIG_FILE/) {printf("BR2_TARGET_UBOOT_CUSTOM_CONFIG_FILE=\"%s/%s_old\"\n", UBOOTCFG_DIR, UBOOTCFG);} else {print($0);} }' $br_dir/configs/temp_$defcfg > $br_dir/configs/$defcfg_short\_old_defconfig
    rm $br_dir/configs/temp_$defcfg
    defcfg=$defcfg_short\_old_defconfig
    defcfg_short=${defcfg:0:${#defcfg}-10}
fi

if [ "$bootdisp" -gt "0" ]; then
    if [ -f "$ubootcfg_dir/$ubootcfg\_old" ]; then
        cp -f $ubootcfg_dir/$ubootcfg\_old $ubootcfg_dir/temp_$ubootcfg
        echo cp -f $ubootcfg_dir/$ubootcfg\_old $ubootcfg_dir/temp_$ubootcfg
    else
        cp -f $ubootcfg_dir/$ubootcfg $ubootcfg_dir/temp_$ubootcfg
        echo cp -f $ubootcfg_dir/$ubootcfg $ubootcfg_dir/temp_$ubootcfg
    fi
    if [ "$bootdisp" -eq "1" ]; then
        gawk '{ if (/CONFIG_BOOTARGS/) {print("CONFIG_BOOTARGS=\"console=ttyO1,57600n8 root=/dev/mmcblk0p2 rw wait rootfstype=ext4 rootwait\"\n");} else {print($0);} }' $ubootcfg_dir/temp_$ubootcfg > $ubootcfg_dir/new_$ubootcfg
        gawk '{ if (/BR2_ROOTFS_POST_BUILD_SCRIPT/) {printf("%s $(BR2_EXTERNAL_CVBOARD_PATH)/common/post-build_disable-getty.sh\"\n", substr($0, 0, length($0)-1));} else {print($0);} }' $br_dir/configs/$defcfg > $br_dir/configs/$defcfg_short\_new_defconfig
        defcfg=$defcfg_short\_new_defconfig
    else
        gawk '{ if (/CONFIG_BOOTARGS/) {print("CONFIG_BOOTARGS=\"console=ttyO0,115200n8 root=/dev/mmcblk0p2 rw wait rootfstype=ext4 rootwait\"\n");} else {print($0);} }' $ubootcfg_dir/temp_$ubootcfg > $ubootcfg_dir/new_$ubootcfg
    fi
    rm $ubootcfg_dir/temp_$ubootcfg

    defcfg_short=${defcfg:0:${#defcfg}-10}

    if [ "$bootdisp" -eq "1" ]; then
        gawk '{ if (/^ *setenv console/) {print("setenv console ttyO1,57600n8");} else {print($0);} }' $bootscr_dir/$bootscr > $bootscr_dir/new_$bootscr
    else
        gawk '{ if (/^ *setenv console/) {print("setenv console ttyO0,115200n8");} else {print($0);} }' $bootscr_dir/$bootscr > $bootscr_dir/new_$bootscr
    fi

    gawk -v BOOTSCR_DIR=$bootscr_dir -v BOOTSCR=$bootscr '{ if (/BR2_PACKAGE_HOST_UBOOT_TOOLS_BOOT_SCRIPT_SOURCE/) {printf("BR2_PACKAGE_HOST_UBOOT_TOOLS_BOOT_SCRIPT_SOURCE=\"%s/new_%s\"\n", BOOTSCR_DIR, BOOTSCR);} else {print($0);} }' $br_dir/configs/$defcfg > $br_dir/configs/temp_$defcfg
    gawk -v UBOOTCFG_DIR=$ubootcfg_dir -v UBOOTCFG=$ubootcfg '{ if (/BR2_TARGET_UBOOT_CUSTOM_CONFIG_FILE/) {printf("BR2_TARGET_UBOOT_CUSTOM_CONFIG_FILE=\"%s/new_%s\"\n", UBOOTCFG_DIR, UBOOTCFG);} else {print($0);} }' $br_dir/configs/temp_$defcfg > $br_dir/configs/$defcfg_short\_new_defconfig
    rm $br_dir/configs/temp_$defcfg
    echo "BR2_ROOTFS_POST_BUILD_SCRIPT=\"\$(BR2_EXTERNAL_CVBOARD_PATH)/cv_octavo_produktion/post_build.sh \$(BR2_EXTERNAL_CVBOARD_PATH)/cv_octavo_produktion/post-build_switch-getty.sh\"" >> $br_dir/configs/$defcfg_short\_new_defconfig
    defcfg=$defcfg_short\_new_defconfig
    defcfg_short=${defcfg:0:${#defcfg}-10}
fi

if [ "$kiti" -eq "1" ]; then
#    sed -e 's/board\/cv\/cv_octavo_standard\/rootfs_overlay/board\/cv\/cv_octavo_standard\/rootfs_overlay board\/cv\/common\/KiTi\/rootfs_overlay/' $br_dir/configs/$defcfg > $br_dir/configs/temp_$defcfg
#    sed -e 's/kernel-patches-2\"/kernel-patches-2 board\/cv\/common\/KiTi\/kernel\/0001-osd335x-dmamcpy49.patch\"/' $br_dir/configs/temp_$defcfg > $br_dir/configs/$defcfg_short\_new_defconfig
    sed -e 's/board\/cv\/cv_octavo_standard\/rootfs_overlay/board\/cv\/cv_octavo_standard\/rootfs_overlay board\/cv\/common\/KiTi\/rootfs_overlay/' $br_dir/configs/$defcfg > $br_dir/configs/temp_$defcfg
    sed -e 's/BR2_PACKAGE_KM_CVKERNELACCESS=y/BR2_PACKAGE_CVFPGAKITI=y/' $br_dir/configs/temp_$defcfg > $br_dir/configs/$defcfg_short\_new_defconfig
    rm $br_dir/configs/temp_$defcfg
    defcfg=$defcfg_short\_new_defconfig
    defcfg_short=${defcfg:0:${#defcfg}-10}
	echo "#!/bin/sh" > copyImages.sh
	echo "td=\"\$1\"" >> copyImages.sh
	echo "cp \${td}/uImage.CV_34_39_500B_KiTi \${td}/uImage.CV_34_39_500B" >> copyImages.sh
	chmod +x copyImages.sh
fi

echo make BR2_EXTERNAL=$br_dir/board/cv BR2_DEFCONFIG=$br_dir/configs/$defcfg -C ../buildroot-2023.02.8/ O=$o_path defconfig

echo "# $0 $1 $2 $3 $4 $5 $6 $7 $8 $9" > cv_current_config.sh
echo "echo make BR2_EXTERNAL=$br_dir/board/cv BR2_DEFCONFIG=$br_dir/configs/$defcfg -C ../buildroot-2023.02.8/ O=$o_path defconfig" >> cv_current_config.sh
chmod +x cv_current_config.sh

echo "sudo dd if=images/sdcard.img of=/dev/sdb status=progress bs=1MB" > mkcard_sdb.sh
chmod +x mkcard_sdb.sh

make BR2_EXTERNAL=$br_dir/board/cv BR2_DEFCONFIG=$br_dir/configs/$defcfg -C ../buildroot-2023.02.8/ O=$o_path defconfig

if [ "$nosvn" -eq "0" ]; then
    cat Makefile | awk 'BEGIN{n=0;} !/export CREATE_SVN_REPOS/{if (++n==2){printf("export CREATE_SVN_REPOS = 2\n");} printf("%s\n", $0);}' > Makefile2
    mv Makefile2 Makefile
fi

echo
echo "It would be a good idea to compile first build_std_flash.sh"
echo
echo "And after that come back here again and run"
echo "../<build_std_dir>/images/cv_source_or_exec_me.sh"
echo
echo "Then it would also be nice to compile a corresponing prtsoft and run"
echo "<prtsoft_dir>/whereiam.sh"
echo
echo "Then this make knows where build_std and prtsoft are and copies them"
echo "automatically on the prod-image"
