#!/bin/bash

git am --abort

PATCH_OLD_PATH=~/cromite/build/patches
PATCH_NEW_PATH=~/cromite/build/patches-new

DESTINATION=~/cromite/build/bromite_patches_list_ok.txt
DESTINATION_FAILED=~/cromite/build/bromite_patches_list_failed.txt

rm $DESTINATION
rm $DESTINATION_FAILED
mkdir $PATCH_NEW_PATH

IFS=$'\n'

PATCH_LIST=~/cromite/build/bromite_patches_list_new.txt
if [ ! -f $PATCH_LIST ]; then
        cp ~/cromite/build/bromite_patches_list.txt $PATCH_LIST
fi

echo "Phase 1: check clean"
for current_file in $(cat $PATCH_LIST); do

    if [[ $current_file =~ ^#.* ]]; then

        echo "Executing $current_file"
        eval "${current_file:1}"
        echo $current_file >>$DESTINATION

    elif [[ $current_file =~ ^-.* ]]; then

        echo "Skipping $current_file"

    elif [[ $current_file =~ ^=.* ]]; then

        echo "Adding $current_file"
        echo "Executing bash ~/create-from-patch.sh $PATCH_OLD_PATH/${current_file:1} $PATCH_NEW_PATH"

        bash ~/cromite/tools/create-from-patch.sh $PATCH_OLD_PATH/${current_file:1} $PATCH_NEW_PATH

       #echo "Press return"
       #read  -n 1


    elif [[ $current_file =~ ^1.* ]]; then

        echo "Using new path $current_file"

        bash ~/cromite/tools/apply-single-patch.sh $PATCH_NEW_PATH/${current_file:1} $PATCH_NEW_PATH

        echo ""
        LAST_COMMIT=$(git rev-parse HEAD)
        echo "Last Commit " $LAST_COMMIT
        bash ~/cromite/tools/export-single-patch.sh $LAST_COMMIT

    else

        if [ -n "$SKIPAUTOGENERATED" ]; then
           if [[ "$current_file" == *"Automated-domain-substitution"* ]]; then
                echo ""
                echo -e ${RED} " -> Excluding $current_file" ${NC}
                continue
           fi
        fi

        bash ~/cromite/tools/apply-single-patch.sh $PATCH_OLD_PATH/$current_file $PATCH_NEW_PATH || exit 1

        if [ -z "$SILENT" ]; then
           echo $current_file >>$DESTINATION
           echo $PATCH_FILE

           echo ""
           LAST_COMMIT=$(git rev-parse HEAD)
           echo "Last Commit " $LAST_COMMIT
           bash ~/cromite/tools/export-single-patch.sh $LAST_COMMIT || exit 1
        fi

    fi

done
