#!/bin/bash

#####
##### Copyright (C) 2020-2021 Aloysius Indrayanto
#####
##### This file is part of the eCxx library, see LICENSE file for the license details.
#####


##### Check if the user gives the correct number of arguments
echo
if [[ ! -n "$1" ]] || [[ ! -n "$2" ]] || [[ $# > 2 ]]; then
    clear
    echo -e "\nCreate a shadow directory that points to the same source directory but with its own customized build options.\n"
    echo -e "Usage:\n\n$0 (avr|nodemcu|x86) <destination_directory_name>\n"
    exit -1
fi

##### Get and check the platform name
PLATFORM=$1

if [[ $PLATFORM != 'avr' ]] && [[ $PLATFORM != 'nodemcu' ]] && [[ $PLATFORM != 'x86' ]]; then
    clear
    echo -e "\nInvalid platform!\n"
    exit -1
fi

##### Get the source and destination directories
SRC_DIR=`dirname "$(readlink -f "$0")"`
DST_DIR=`realpath $2`

##### Check if the destination and the source directories are the same one
if [[ "$SRC_DIR" == "$DST_DIR"               ]] || \
   [[ -f "$DST_DIR"/shadow_init.sh           ]] || \
   [[ -f "$DST_DIR"/Makefile.shadow.template ]]; then
    echo -e "The destination directory cannot be the same with the real project tree!\n"
    exit -1
fi

for F in "$DST_DIR"/Makefile*; do
    if [[ -f "$F" ]] && [[ ! -L "$F" ]]; then
       if [[ "$(basename $F)" == "Makefile.shadow.config" ]]; then
            continue
       else
            echo -e "The destination directory cannot be the same with the real project tree!\n"
            exit -1
       fi
    fi
done

##### Check if the destination directory is a subdirectory of the source directory
CHECK="${DST_DIR#"$SRC_DIR"}"

if [[ "${CHECK:0:1}" == "/" ]]; then
    echo -e "The destination directory cannot be a subdirectory of the real project tree!\n"
    exit -1
fi

##### Check if the destination directory is empty
mkdir -p "$DST_DIR"

if [[ "$(ls -A "$DST_DIR")" ]]; then
    echo -e "The destination directory is not empty."
    echo -e "All files in the destination directory will be deleted!\n\n"
    read -p "Are you sure to continue? " YN
    case $YN in
        [Yy]*)
            echo -e "Deleting all files in the destination directory ...\n"
            rm -rf "$DST_DIR"/*
            ;;
        [Nn]*)
            echo -e "Aborted.\n"
            exit 0
            ;;
        *)
            echo -e "Please answer yes or no.\n"
            echo -e "Aborted.\n"
            exit -1
            ;;
    esac
fi

##### Make symbolic links from files and subdirectories within the source directory
echo -e "Creating symbolic links from source directory ...\n"
ln -s "$SRC_DIR"/* "$DST_DIR"

##### Remove some directories and files
echo -e "Preparing directories and files ...\n"
cd "$DST_DIR"

rm build
mkdir build

rm user_src
cp -R "$SRC_DIR"/user_src .

rm fs_root
cp -R "$SRC_DIR"/fs_root .

rm -f shadow_init.sh
rm -f Makefile.shadow.template

##### Create the shadow configuration makefile
echo -e "Preparing a shadow configuration file for customized build options ...\n"

MSC_FILE="$DST_DIR"/Makefile.shadow.config

cat "$SRC_DIR"/Makefile.shadow.template.$PLATFORM         \
    | sed '/^[[:space:]]*$/{:a;$d;N;/\n[[:space:]]*$/ba}' \
    | sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'               \
> $MSC_FILE
echo >> $MSC_FILE

if [[ $PLATFORM == "avr" ]]; then
    cat "$SRC_DIR"/Makefile.config                        \
        | \grep "\S"                                      \
        | sed -e '1,/##### <<< SHADOW UPCONF_TTY_C >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_TTY_N >>>/,$d' \
    >> $MSC_FILE
    echo >> $MSC_FILE

    cat "$SRC_DIR"/Makefile.config                         \
        | \grep "\S"                                       \
        | sed -e '1,/##### <<< SHADOW UPCONF_OTA_A1 >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_OTA_A2 >>>/,$d' \
    >> $MSC_FILE
    echo >> $MSC_FILE

    cat "$SRC_DIR"/Makefile.config                         \
        | \grep "\S"                                       \
        | sed -e '1,/##### <<< SHADOW UPCONF_OTA_A2 >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_OTA_N1 >>>/,$d' \
    >> $MSC_FILE
    echo >> $MSC_FILE
fi

if [[ $PLATFORM == "nodemcu" ]]; then
    cat "$SRC_DIR"/Makefile.config                        \
        | \grep "\S"                                      \
        | sed -e '1,/##### <<< SHADOW UPCONF_TTY_C >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_TTY_N >>>/,$d' \
    >> $MSC_FILE
    echo >> $MSC_FILE

    cat "$SRC_DIR"/Makefile.config                        \
        | \grep "\S"                                      \
        | sed -e '1,/##### <<< SHADOW UPCONF_TTY_N >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_OTA_A1 >>>/,$d'\
    >> $MSC_FILE
    echo >> $MSC_FILE

    cat "$SRC_DIR"/Makefile.config                         \
        | \grep "\S"                                       \
        | sed -e '1,/##### <<< SHADOW UPCONF_OTA_N1 >>>/d' \
        | sed -e '/##### <<< SHADOW UPCONF_OTA_N2 >>>/,$d' \
    >> $MSC_FILE
    echo >> $MSC_FILE

    cat "$SRC_DIR"/Makefile.config                         \
        | \grep "\S"                                       \
        | sed -e '1,/##### <<< SHADOW UPCONF_OTA_N2 >>>/d' \
    >> $MSC_FILE
    echo >> $MSC_FILE
fi

##### Show informational message
echo -e "\nDone!\n\nPlease edit $MSC_FILE to customize your build options.\n"
