#!/bin/bash

## This script can create external configuration files for all -tkg packages and tools supporting the feature.

# Path to create the external config files in - Default is ~/.config/frogminer
_EXT_CONFIG_PATH=~/.config/frogminer

_cfg_loop() {
    if [ -e "$_config_file_path"/"$_config_file_basename" ] && [ ! -e "$_EXT_CONFIG_PATH"/"$_config_file_targname" ]; then
        cp -v "$_config_file_path"/"$_config_file_basename" "$_EXT_CONFIG_PATH"/"$_config_file_targname"
    else
        echo "$_EXT_CONFIG_PATH/$_config_file_targname already exists. Skipping..."
    fi
}

_external_cfg() {
    # dxvk-tools
    if [ "$dxvk_tools" == "true" ]; then
        _config_file_path=dxvk-tools
        _config_file_basename=updxvk.cfg
        _config_file_targname=updxvk.cfg
        unset dxvk_tools
        _cfg_loop
    fi

    # gamescope
    if [ "$gamescope" == "true" ]; then
        _config_file_path=gamescope-git
        _config_file_basename=customization.cfg
        _config_file_targname=gamescope-git.cfg
        unset gamescope
        _cfg_loop
    fi

    # linux-tkg
    if [ "$linux_tkg" == "true" ]; then
        _config_file_path=linux-tkg
        _config_file_basename=customization.cfg
        _config_file_targname=linux-tkg.cfg
        unset linux_tkg
        _cfg_loop
    fi

    # mesa-git
    if [ "$mesa_git" == "true" ]; then
        _config_file_path=mesa-git
        _config_file_basename=customization.cfg
        _config_file_targname=mesa-git.cfg
        unset mesa_git
        _cfg_loop
    fi

    # mostlyportable-gcc
    if [ "$mostlyportable_gcc" == "true" ]; then
        _config_file_path=mostlyportable-gcc
        _config_file_basename=mostlyportable-gcc.cfg
        _config_file_targname=mostlyportable-gcc.cfg
        unset mostlyportable_gcc
        _cfg_loop
    fi

    # mostlyportable-mingw
    if [ "$mostlyportable_mingw" == "true" ]; then
        _config_file_path=mostlyportable-gcc
        _config_file_basename=mostlyportable-mingw.cfg
        _config_file_targname=mostlyportable-mingw.cfg
        unset mostlyportable_mingw
        _cfg_loop
    fi

    # nvidia-all
    if [ "$nvidia_all" == "true" ]; then
        _config_file_path=nvidia-all
        _config_file_basename=customization.cfg
        _config_file_targname=nvidia-all.cfg
        unset nvidia_all
        _cfg_loop
    fi

    # vkd3d-git
    if [ "$vkd3d_git" == "true" ]; then
        _config_file_path=vkd3d-git
        _config_file_basename=customization.cfg
        _config_file_targname=vkd3d-git.cfg
        unset vkd3d_git
        _cfg_loop
    fi

    # proton-tkg
    if [ "$proton_tkg" == "true" ]; then
        _config_file_path=wine-tkg-git/proton-tkg/proton-tkg-profiles
        _config_file_basename=sample-external-config.cfg
        _config_file_targname=proton-tkg.cfg
        unset proton_tkg
        _cfg_loop
    fi

    # wine-tkg
    if [ "$wine_tkg" == "true" ]; then
        _config_file_path=wine-tkg-git/wine-tkg-git/wine-tkg-profiles
        _config_file_basename=sample-external-config.cfg
        _config_file_targname=wine-tkg.cfg
        unset wine_tkg
        _cfg_loop
    fi
}

_main_loop() {
    echo -e "What do you want to do?"
    read -rp "`echo $'  > 1.Create missing external config files\n    2.Update submodules\n    3.Exit\nchoice[1-3?]: '`" _choice;
    if [ "$_choice" == "2" ]; then
        #git submodule update --remote
        for _tkg_tools in */; do
          if [ "$_tkg_tools" != ".git" ]; then
            ( cd "$_tkg_tools" && git config pull.rebase false && git checkout master && git pull origin master )
          fi
        done
    elif [ "$_choice" == "3" ]; then
        exit 0
    else
        mkdir -p "$_EXT_CONFIG_PATH"
        echo -e "\nPlease select which tool you want to create external config files for (or all of them with option #1) :"
        select tkg_tools in "all" "dxvk_tools" "gamescope" "linux_tkg" "mesa_git" "mostlyportable_gcc" "mostlyportable_mingw" "nvidia_all" "vkd3d_git" "proton_tkg" "wine_tkg"
        do
            case $tkg_tools in
                "all")
                    dxvk_tools="true"
                    gamescope="true"
                    linux_tkg="true"
                    mesa_git="true"
                    mostlyportable_gcc="true"
                    mostlyportable_mingw="true"
                    nvidia_all="true"
                    vkd3d_git="true"
                    proton_tkg="true"
                    wine_tkg="true"
                    ;;
                "dxvk_tools")
                    dxvk_tools="true"
                    ;;
                "gamescope")
                    gamescope="true"
                    ;;
                "linux_tkg")
                    linux_tkg="true"
                    ;;
                "mesa_git")
                    mesa_git="true"
                    ;;
                "mostlyportable_gcc")
                    mostlyportable_gcc="true"
                    ;;
                "mostlyportable_mingw")
                    mostlyportable_mingw="true"
                    ;;
                "nvidia_all")
                    nvidia_all="true"
                    ;;
                "vkd3d_git")
                    vkd3d_git="true"
                    ;;
                "proton_tkg")
                    proton_tkg="true"
                    ;;
                "wine_tkg")
                    wine_tkg="true"
                    ;;
                *)
            esac
            break
        done
        echo ""
        _external_cfg
        echo ""
    fi
    _main_loop
}

cat << 'EOM'
       .---.`               `.---.
    `/syhhhyso-           -osyhhhys/`
   .syNMdhNNhss/``.---.``/sshNNhdMNys.
   +sdMh.`+MNsssssssssssssssNM+`.hMds+
   :syNNdhNNhssssssssssssssshNNhdNNys:
    /ssyhhhysssssssssssssssssyhhhyss/
    .ossssssssssssssssssssssssssssso.
   :sssssssssssssssssssssssssssssssss:    TKG tools
  /sssssssssssssssssssssssssssssssssss/        Thingy
 :sssssssssssssoosssssssoosssssssssssss:
 osssssssssssssoosssssssoossssssssssssso
 osssssssssssyyyyhhhhhhhyyyyssssssssssso
 /yyyyyyhhdmmmmNNNNNNNNNNNmmmmdhhyyyyyy/
  smmmNNNNNNNNNNNNNNNNNNNNNNNNNNNNNmmms
   /dNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNd/
    `:sdNNNNNNNNNNNNNNNNNNNNNNNNNds:`
       `-+shdNNNNNNNNNNNNNNNdhs+-`
             `.-:///////:-.`

EOM

echo -e "This script can create external configuration files for all -tkg packages and tools supporting the feature.\nAlready existing config files will NOT be overwritten, so you'll want to remove any you'd want regenerated.\n\nIt can also update submodules if needed.\n"

echo -e "Current external config path is set to $_EXT_CONFIG_PATH \n"

_main_loop