#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(realpath "${SCRIPT_DIR}/..")"
CONFIG_DIR="${PROJECT_ROOT}/configs"
DEVICE_CONFIG="${CONFIG_DIR}/${DEVICE_NAME}.conf"
A_TEAM_SETUP="${SCRIPT_DIR}/a-team_setup.sh"
A_TEAM_SETUP_CHECK="${ROM_PATH}/vendor/A-Team/A-Team-Setup.check"

if [ ! -f "$DEVICE_CONFIG" ]; then
    echo
    echo "Device Config Not Found: $DEVICE_CONFIG"
    exit 1
fi
# A-Team Setup
if [ -f "$A_TEAM_SETUP_CHECK" ]; then
    clear
    echo
    echo "A-Team Setup Already Run, Skipping Setup ..."
else
    clear
    echo
    echo "Syncing A-Team Addon Package ..."
    echo
    git clone https://ateam.gotadell.com/git/PizzaG/android_vendor_A-Team.git vendor/A-Team
    echo 
    echo
    echo "Running A-Team Setup ..."
    echo
    if bash "$A_TEAM_SETUP"; then
        mkdir -p "$(dirname "$A_TEAM_SETUP_CHECK")"
        touch "$A_TEAM_SETUP_CHECK"
    else
        echo "A-Team Setup Failed!"
        exit 1
    fi
fi

SYNC="${SCRIPT_DIR}/${DEVICE_SYNC_SCRIPT}"

cd "$ROM_PATH" || exit 1
if [ -f "$SYNC" ]; then
    bash "$SYNC"
else
    echo "No Device Sync Script Found For: $DEVICE_DISPLAY_CODENAME"
fi
