#!/bin/zsh

#echo 'This installer needs a rewrite! It is disabled for now. Exiting...'
#exit 1

while [[ "$HOME" == '' ]]
do
    read -p "please enter your homedir: " -i "/home/$(logname)" -e HOME
    [[ -d "$HOME" ]] || { HOME=""; echo "invalid. please enter an existing directory." }
done
echo "HOME:$HOME"
cd "$HOME" || { echo "can't get to your home! please report this issue!"; exit 1 }

if [[ -f .zshrc ]]
then echo -ne "do you want to back up your existing .zshrc?\n[y(backup)/n(remove)]> "
    read in && echo ''
    case "$in" in
        n|no|remove) rm -rf .zshrc;;
        y|yes|backup)   cp .zshrc .zshrc.old
                        rm -rf .zshrc;;
        * ) echo "invalid."; exit 1
    esac
fi

if [[ -v ROOTCMD ]]
then rootcmd="$ROOTCMD"
elif command -v doas
then rootcmd=doas
elif command -v sudo
then rootcmd=sudo
else
echo "can't find doas or sudo."
echo "please enter the name of your permission elevation command (like sudo or doas). please make sure the command accepts another command as arguments."
echo "(so `su -c` won't work, that only accepts one word as command.)"
read -q "? > " rootcmd
fi


if ! command -v git >/dev/null; then
    read -q "?Git not found, do you want ot install it using apt? [y/n]> "
    "$rootcmd" apt install git curl -y
    if ! command -v git >/dev/null; then
        echo "installing git failed. please install manually and rerun me."
        exit 2
    fi
fi

echo "cloning git repo..."
if [[ -d ".zsh.plugins.d" ]]; then
    echo "We store the contents of the repo in `~/.zsh.plugins.d`, but that directory already exists!"
    echo "We can copy this directory for you, to make place for this repo. It will then be located at"
    echo "`~/.zsh.plugins.d.bak` for you to give it a better name. If your scripts are in there and"
    echo "you want to source them later, you can use the user config directory ~/.zsh.d and all files"
    echo "in there will be sourced automaticaly."
    read -q "?do you want to back up your existing .zsh.plugins.d?\n[y(backup)/n(remove)]> " in
    case "$in" in
        n|no|remove) rm -rf .zsh.plugins.d;;
        y|yes|backup)   cp -r .zsh.plugins.d .zsh.plugins.d.bak
                        rm -rf .zsh.plugins.d;;
        * ) echo "invalid."; exit 1
    esac
fi
git clone 'https://github.com/Vosjedev/foxprompt.git' .zsh.plugins.d
echo "fixing subrepos:"
cd ~/.zsh.plugins.d
git clone https://github.com/lincheney/fzf-tab-completion
git clone https://github.com/zsh-users/zsh-autosuggestions


mkdir .zsh.d

cd "$HOME"
echo "downloading ~/.zshrc..."
url=https://vosjedev.pii.at/configs/zshrc/latest
file=.zshrc
if command -v axel>/dev/null
then axel -o "$file" "$url"
elif command -v curl>/dev/null
then curl -# -o "$file" "$url"
elif command -v wget>/dev/null
then wget "$url" -O "$file"
else echo "no downloader. install curl or wget"; exit 2
fi

echo -e "\n\n"


echo "This setup has some aliases that are interesting."
echo 'One of them is `d`, which lets you switch directory or open a file using'
echo 'a nice-looking interface.'
echo 'Do you want to install the program `lf` using `apt`?'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall lf"; else echo "not installing lf"; fi

echo -e "\n\n"

echo 'Another one is micro. This is a text editor with nice integration for your'
echo 'system clipboard and waaayyy more. I really recommend this one.'
echo 'Do you want to install the program `micro` using `apt`?'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall micro"; else echo "not installing micro"; fi

echo -e "\n\n"

echo "Then there is bat. There are 2 aliases for bat."
echo 'One is `page`, for a better paging experience. (linenrs, syntaxhighighting)'
echo 'The other one is `bat`, which makes bat without any args just cat with'
echo 'line numbers and syntax highlighting'
echo 'Do you want to install the program `bat` using `apt`?'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall bat"; else echo "not installing bat"; fi

echo -e "\n\n"

echo "For better autocomplete you will need fzf: fuzzy finder. It is a finder,"
echo "but also a component of our shell."
echo "If you do not install it, fzf-autocomplete is disabled until fzf is found."
echo 'Do you want to install the program `fzf` using `apt`?'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall fzf"; else echo "not installing fzf"; fi

echo -e "\n\n"

echo 'I also recommend eza: a better ls, with things like icons and more.'
echo 'Note: this will add a new apt repo.'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall eza"; else echo "not installing eza"; fi

echo -e "\n\n"

echo 'And last but not least I recommend opendoas.'
echo 'This is the sudo from bds, and is a lot smaller than sudo.'
echo 'Do you want to install the program `opendoas` using `apt`?'
read -q '?[y/n] ' yn
if [[ "$yn" == y ]]; then whattoinstall="$whattoinstall opendoas"; else echo "not installing opendoas"; fi

echo -e "\n\n"

whattoinstall=" $whattoinstall "

if ! command -v apt; then
read '?apt is not installed. This means a lot of the following commands will fail, but configuration of programs will still take place if needed. Make sure to install the packages afterward, a list will be printed later. Press enter...'
fi

if [[ "$whattoinstall" == *" eza "* ]]; then
    echo "Adding repo for eza..."
    echo "|- making sure gpg and wget are installed..."
    "$rootcmd" apt install -y gpg wget

    echo "|- getting key..."
    "$rootcmd" mkdir -p /etc/apt/keyrings
    wget -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc | "$rootcmd" gpg --dearmor -o /etc/apt/keyrings/gierens.gpg
    echo "|- adding list..."
    echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" | "$rootcmd" tee /etc/apt/sources.list.d/gierens.list
    "$rootcmd" chmod 644 /etc/apt/keyrings/gierens.gpg /etc/apt/sources.list.d/gierens.list
    echo "|- updating..."
    "$rootcmd" apt update
    echo "Done."
fi


echo "installing the following packages:"
echo "$whattoinstall"
eval "\"$rootcmd\" apt install $whattoinstall -y"

if [[ "$whattoinstall" == *" micro "* ]]
then
echo 'Do you also want to download my micro config?'
read -q '? > ' yn
if [[ "$yn" == 'y' ]]
then

if command -v axel>/dev/null
then axel -o "micro.tar.gz" "https://vosjedev.pii.at/configs/micro.tar.gz"
elif command -v curl>/dev/null
then curl -# -o "micro.tar.gz" "https://vosjedev.pii.at/configs/micro.tar.gz"
elif command -v wget>/dev/null
then wget "https://vosjedev.pii.at/configs/micro.tar.gz" -O micro.tar.gz
else echo "no downloader. install curl or wget"; exit 2
fi

tar -xvzf micro.tar.gz

fi
fi

if [[ "$whattoinstall" == *" opendoas "* ]]
then echo -e "\n\nconfiguring doas"

echo "I will now drop you into an editor to configure opendoas."
echo 'Please change add `permit persist :groupname` to allow'
echo 'a specific group to use doas. On my system, this was sudo, making this line'
echo 'permit persist :sudo'
echo 'but on most systems this is wheel. I recommend checking if you'
echo 'are in a group like this, to ensure access to doas. to list all groups you are in, use'
echo 'groups your-username'
echo 'If you happened to be in a group called wheel, add'
echo 'permit persist :wheel'
echo 'in the editor. The same for a group called sudo, but replace `:wheel`'
echo 'with `:sudo`.'
echo 'You can also use'
echo 'permit persist your-username'
echo 'to only allow this username.'
echo 'If the above is all unclear to you, I recommend watching'
echo 'https://www.youtube.com/watch?v=brXd12LstgA'
echo 'by Brodie Robertson. He explains this more clearly than I can do'
echo 'in this environment.'
echo ''
echo 'please type the name of your favorite text editor (maybe `micro`?)'
read '? > ' editor
"$rootcmd" "$editor" "/etc/doas.conf"
fi

echo "done"

if ! command -v apt; then
echo 'Please install the following packages using your own package manager:'
echo "$whattoinstall"
fi

