#!/bin/bash
#
# Copyright (C) 2019-Present A-Team Digital Solutions
#

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEVICE_MODEL="$DEVICE_MODEL_NUMBER"

clear
bash "$SCRIPT_DIR/clean.sh"
clear

# Used For Testing variable additions
#env | sort | grep -E 'DEVICE|ROM|ANDROID|JOB|CCACHE'

if [[ "$USE_CCACHE" == "1" ]]; then
    CCACHE=Yes
else
    CCACHE=No
fi

echo 
echo "================================"
echo "ROM Build Settings ..."
echo 
echo "Device: $DEVICE_DISPLAY_NAME"
echo "Device Codename: $DEVICE_DISPLAY_CODENAME"
echo "Device Model: $DEVICE_MODEL"
echo "Rom: $ROM_DISPLAY_NAME"
echo "Android: $ANDROID_VERSION"
echo "Build Jobs: $BUILD_JOBS"
echo "Ccache: $CCACHE"
echo "Ccache Size: ${CCACHE_SIZE}GB"
echo "Build Variant: ${BUILD_VARIANT^}"
echo "Gapps Variant: ${GAPPS_VARIANT^}"
echo "Installer Variant: ${INSTALLER_VARIANT^}"
echo "Pre-Build Clean Variant: ${CLEAN_VARIANT^}"
echo "================================"
echo 
echo 
sleep 5
echo "ROM Build Starting ..."
echo 
echo
# Source Build Env
source build/envsetup.sh
# Export Build Variables
export LC_ALL=C
export SELINUX_IGNORE_NEVERALLOWS=true
# Export user selected ccache toggle
if [[ "$USE_CCACHE" == "1" ]]; then
    export USE_CCACHE=1
    export CCACHE_EXEC="$(command -v ccache)"
    export CCACHE_DIR="$HOME/.ccache"

    echo "CCache: Enabled"
    echo "  Binary : $CCACHE_EXEC"
    echo "  Cache  : $CCACHE_DIR"
    echo

    ccache -M ${CCACHE_SIZE}G
    ccache -s
    echo
    echo
else
    export USE_CCACHE=0
    echo "CCache: Disabled"
    echo
    echo
fi
if [[ $INSTALLER_VARIANT == "default" ]]; then
   # Device Auto select since fogos spans 2 devices
   if [[ "$DEVICE_DISPLAY_NAME" == "Moto G34 5G" ]]; then
       sed -i 's/PRODUCT_MODEL := moto g34 5G/PRODUCT_MODEL := Moto G34 5G/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto G45 5G" ]]; then
       sed -i 's/PRODUCT_MODEL := moto g34 5G/PRODUCT_MODEL := Moto G45 5G/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   # Device Auto select since rtwo spans 3 devices
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto Edge 40 Pro" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto Edge 40 Pro/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto X40" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto X40/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto Edge+ 2023" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto Edge+ - 2023/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   fi

   # Lunch
   lunch ${ROM_BUILD_NAME}_${DEVICE_NAME}-${ROM_TARGET_RELEASE}-${BUILD_VARIANT}
   # Build
   "$ROM_BUILD_COMMAND" "$DEVICE_CODENAME"
elif [[ $INSTALLER_VARIANT == "custom" ]]; then
   # Device Auto select since fogos spans 2 devices
   if [[ "$DEVICE_DISPLAY_NAME" == "Moto G34 5G" ]]; then
       sed -i 's/PRODUCT_MODEL := moto g34 5G/PRODUCT_MODEL := Moto G34 5G/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto G45 5G" ]]; then
       sed -i 's/PRODUCT_MODEL := moto g34 5G/PRODUCT_MODEL := Moto G45 5G/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   # Device Auto select since rtwo spans 3 devices
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto Edge 40 Pro" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto Edge 40 Pro/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto X40" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto X40/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   elif [[ "$DEVICE_DISPLAY_NAME" == "Moto Edge+ 2023" ]]; then
       sed -i 's/PRODUCT_MODEL := motorola edge 40 pro/PRODUCT_MODEL := Moto Edge+ - 2023/g' $ROM_PATH/device/motorola/${DEVICE_CODENAME}/lineage_${DEVICE_CODENAME}.mk
       echo "3/3 ..."
       echo 
       sleep 3
   fi
   # Lunch
   lunch ${ROM_BUILD_NAME}_${DEVICE_NAME}-${ROM_TARGET_RELEASE}-${BUILD_VARIANT}
   # Build
   "$ROM_BUILD_COMMAND" "$DEVICE_CODENAME"
fi
#m bacon -j$JOBS


