#!/bin/bash

set -uo pipefail
CONFIG_FILE="config"

function generate_makefile {
    m4 -P Makefile.m4 > Makefile
    make Makefile
}

if ! command -v dialog; then
    echo '"dialog" not found, please install before continuing"
    exit 1
fi
if ! command -v m4; then
    echo 'm4" not found, please install before continuing"
    exit 1
fi
if ! command -v make; then
    echo 'make" not found, please install before continuing"
    exit 1
fi
if ! command -v awk; then
    echo 'awk" not found, please install before continuing"
    exit 1
fi

if [ -f "${CONFIG_FILE}" ]; then
    dialog \
        --keep-tite \
        --backtitle "Dotfile configuration" \
        --title "Overwrite settings?" \
        --defaultno \
        --yesno "A config file already exists, do you want to make that file over again? If you press No, the Makefile will be processed again based on the current config file." 0 0
    if [ "$?" -eq 1 ]; then
        generate_makefile
        exit 0
    fi
    source ${CONFIG_FILE}
fi

: "${DT_DOTFILES_DIR:=$(pwd)}"
: "${DT_HOME_DIRECTORY:=${HOME:-}}"
: "${DT_SHELL:=${SHELL:-}}"
: "${DT_EDITOR:=${EDITOR:-}}"
: "${DT_SYSID:=1}"
: "${DT_DISTRO:=}"
: "${DT_OS:=linux}"
: "${DT_DOTFILES_TYPE:=local}"
: "${DT_GIT_USER:=${DT_GIT_AUTHOR_NAME:-}}"
: "${DT_GIT_EMAIL:=${DT_GIT_AUTHOR_EMAIL:-}}"
: "${DT_TOOLS:=yes}"
: "${DT_OTHER_SYMLINKS:=yes}"
: "${DT_NEOVIM:=yes}"
: "${DT_ZSH:=no}"
: "${DT_TLP:=no}"
: "${DT_AUTORANDR:=no}"
: "${DT_GREETD_TUIGREET:=no}"
: "${DT_POWERLINE_P10K:=no}"
: "${DT_SWAY:=no}"
: "${DT_QTILE:=no}"
: "${DT_BASH:=yes}"
: "${DT_HOMEBIN:=no}"
: "${DT_GOTIFY_URL:=}"
: "${DT_GOTIFY_TOKEN:=}"
: "${DT_TMUX:=yes}"
: "${DT_MYCLI:=no}"
: "${DT_NEWSBOAT:=no}"
: "${DT_ALACRITTY:=yes}"
: "${DT_ROFI:=no}"
: "${DT_DEADD:=no}"
: "${DT_WAYBAR:=no}"
: "${DT_FOOT:=no}"
: "${DT_POWERLINE:=no}"
: "${DT_SXHKD:=no}"
: "${DT_BSPWM:=no}"
: "${DT_INPUTRC:=yes}"
: "${DT_VIM:=yes}"
: "${DT_I3:=yes}"

radiolist_on() {
    local value="$1"
    if [[ "$value" == "$2" ]]; then
        echo "on"
    else
        echo "off"
    fi
}

checklist_on() {
    local value="$1"
    if [[ "$value" == "yes" ]]; then
        echo "on"
    else
        echo "off"
    fi
}

DT_HOME_DIRECTORY=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --inputbox "Home directory" \
      0 0 "${DT_HOME_DIRECTORY}" \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

DT_SHELL=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --inputbox "Default shell" \
      0 0 "${DT_SHELL}" \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

DT_EDITOR=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --inputbox "Default editor" \
      0 0 "${DT_EDITOR}" \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

DT_SYSID=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
      --inputbox "System id (magical number)" \
      0 0 "${DT_SYSID}" \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

SYSTEM_TYPE=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --radiolist "System type" \
      0 0 0 \
      0 "Local"  $(radiolist_on "${DT_DOTFILES_TYPE}" "local") \
      1 "Remote" $(radiolist_on "${DT_DOTFILES_TYPE}" "remote") \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

case $SYSTEM_TYPE in
  0) DT_DOTFILES_TYPE=local;;
  1) DT_DOTFILES_TYPE=remote;;
esac

DISTRO=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --radiolist "Distro" \
      0 0 0 \
      0 "Debian" $(radiolist_on "${DT_DISTRO}" "debian") \
      1 "Fedora" $(radiolist_on "${DT_DISTRO}" "fedora") \
3>&1 1>&2 2>&3 3>&-)
if [ "$?" -eq 1 ]; then exit 0; fi

case $DISTRO in
  0) DT_DISTRO=debian;;
  1) DT_DISTRO=fedora;;
esac

GIT_VALUES=$(dialog \
      --keep-tite \
	  --backtitle "Dotfile configuration" \
	  --title "Configure" \
	  --form "Git settings" \
0 50 0 \
	"Username:" 1 1	"${GIT_AUTHOR_NAME:-}" 	    1 10 255 0 \
    "Email:"    2 1	"${GIT_AUTHOR_EMAIL:-}"  	2 10 255 0 \
3>&1 1>&2 2>&3 3>&-)
GIT_VALUES=($GIT_VALUES)
if [ "$?" -eq 1 ]; then exit 0; fi

DT_GIT_USER=${GIT_VALUES[0]}
DT_GIT_EMAIL=${GIT_VALUES[1]}

FEATURES="$(dialog --stdout \
    --keep-tite \
    --backtitle "Dotfile configuration" \
    --title "Configure" \
    --checklist "Change dotfile features (be aware that turning them off is not really possible):" 0 0 0 \
    1 "General tools (highligt, mediainfo, git, ripgrep...)" $(checklist_on "${DT_TOOLS}") \
    2 "Other dotfile symlinks (Not for specific features below)" $(checklist_on "${DT_OTHER_SYMLINKS}") \
    3 "neovim" $(checklist_on "${DT_NEOVIM}") \
    4 "ZSH and Oh-My-Zsh" $(checklist_on "${DT_ZSH}") \
    5 "tlp" $(checklist_on "${DT_TLP}") \
    6 "autorandr" $(checklist_on "${DT_AUTORANDR}") \
    7 "greetd and tuigreet" $(checklist_on "${DT_GREETD_TUIGREET}") \
    8 "Powerline and P10K" $(checklist_on "${DT_POWERLINE_P10K}") \
    9 "Sway" $(checklist_on "${DT_SWAY}") \
    10 "QTile" $(checklist_on "${DT_QTILE}") \
    11 "Bash" $(checklist_on "${DT_BASH}") \
    12 "home folder bin dir" $(checklist_on "${DT_HOMEBIN}") \
    13 "tmux" $(checklist_on "${DT_NEOVIM}") \
    15 "MyCLI" $(checklist_on "${DT_MYCLI}") \
    16 "Newsboat" $(checklist_on "${DT_NEWSBOAT}") \
    17 "Alacritty" $(checklist_on "${DT_ALACRITTY}") \
    18 "Rofi" $(checklist_on "${DT_ROFI}") \
    19 "Deadd" $(checklist_on "${DT_DEADD}") \
    20 "Waybar (needed for sway)" $(checklist_on "${DT_WAYBAR}") \
    21 "Foot" $(checklist_on "${DT_FOOT}") \
    22 "Powerline" $(checklist_on "${DT_POWERLINE}") \
    23 "Sxhkd" $(checklist_on "${DT_SXHKD}") \
    24 "Bspwm" $(checklist_on "${DT_BSPWM}") \
    25 "Inputrc (usefull for bash, less, read etc)" $(checklist_on "${DT_INPUTRC}") \
    26 "Vim" $(checklist_on "${DT_VIM}") \
    27 "i3wm" $(checklist_on "${DT_I3}"))"
if [ "$?" -eq 1 ]; then exit 0; fi

DT_TOOLS=no
DT_OTHER_SYMLINKS=no
DT_NEOVIM=no
DT_ZSH=no
DT_TLP=no
DT_AUTORANDR=no
DT_GREETD_TUIGREET=no
DT_POWERLINE_P10K=no
DT_SWAY=no
DT_QTILE=no
DT_BASH=no
DT_HOMEBIN=no
DT_TMUX=no
DT_MYCLI=no
DT_NEWSBOAT=no
DT_ALACRITTY=no
DT_ROFI=no
DT_DEADD=no
DT_WAYBAR=no
DT_FOOT=no
DT_POWERLINE=no
DT_SXHKD=no
DT_BSPWM=no
DT_INPUTRC=no
DT_VIM=no
DT_I3=no

for choice in ${FEATURES}; do
    case $choice in
      1)   DT_TOOLS=yes;;
      2)   DT_OTHER_SYMLINKS=yes;;
      3)   DT_NEOVIM=yes;;
      4)   DT_ZSH=yes;;
      5)   DT_TLP=yes;;
      6)   DT_AUTORANDR=yes;;
      7)   DT_GREETD_TUIGREET=yes;;
      8)   DT_POWERLINE_P10K=yes;;
      9)   DT_SWAY=yes;;
      10)  DT_QTILE=yes;;
      11)  DT_BASH=yes;;
      12)  DT_HOMEBIN=yes;;
      13)  DT_TMUX=yes;;
      15)  DT_MYCLI=yes;;
      16)  DT_NEWSBOAT=yes;;
      17)  DT_ALACRITTY=yes;;
      18) DT_ROFI=yes;;
      19) DT_DEADD=yes;;
      20) DT_WAYBAR=yes;;
      21) DT_FOOT=yes;;
      22) DT_POWERLINE=yes;;
      23) DT_SXHKD=yes;;
      24) DT_BSPWM=yes;;
      25) DT_INPUTRC=yes;;
      26) DT_VIM=yes;;
      27) DT_I3=yes;;
    esac
done

if [ "${DT_NEWSBOAT}" = "yes" ]; then
    NEWSBOAT_VALUES=$(dialog \
          --keep-tite \
          --backtitle "Dotfile configuration" \
          --title "Configure" \
          --form "Newsboat settings" \
    0 50 0 \
        "Url:"      1 1	"${DT_NEWSBOAT_SOURCE:-}" 	    1 10 255 0 \
        "Username:" 2 1	"${DT_NEWSBOAT_USER:-}"      	2 10 255 0 \
    3>&1 1>&2 2>&3 3>&-)
    if [ "$?" -eq 1 ]; then exit 0; fi
    NEWSBOAT_VALUES=($NEWSBOAT_VALUES)
    DT_NEWSBOAT_SOURCE=${NEWSBOAT_VALUES[0]}
    DT_NEWSBOAT_USER=${NEWSBOAT_VALUES[1]}
fi

if [ "${DT_HOMEBIN}" = "yes" ]; then
    GOTIFY_VALUES=$(dialog \
          --keep-tite \
          --backtitle "Dotfile configuration" \
          --title "Configure" \
          --form "Gotify (push) settings" \
    0 50 0 \
        "Url:"          1 1	"${DT_GOTIFY_URL:-}" 	    1 10 255 0 \
        "Push token:"   2 1	"${DT_GOTIFY_TOKEN:-}"     	2 10 255 0 \
    3>&1 1>&2 2>&3 3>&-)
    if [ "$?" -eq 1 ]; then exit 0; fi
    GOTIFY_VALUES=($GOTIFY_VALUES)
    DT_GOTIFY_URL=${GOTIFY_VALUES[0]}
    DT_GOTIFY_TOKEN=${GOTIFY_VALUES[1]}
fi

# write config to file
printf "" > "${CONFIG_FILE}"
set | grep '^DT_' | awk -F '=' '{print $1"="$2""}' >> "${CONFIG_FILE}"

# generate makefile
generate_makefile
