#!/bin/sh

############# Packages to install ########################################

macos_homebrew_essentials="python3 vim-nox tmux nano zsh git autossh mosh sshuttle lynx npm tidy mcfly ack ncdu shfmt"
macos_homebrew_other_apps="calibre"
raspberrypi_essentials="python3 python3-pip vim-nox python3-venv tmux nano zsh git htop ncdu autossh mosh sshuttle fail2ban avahi-daemon ufw lynx npm ssmtp apticron tidy nmon software-properties-common ack restic unrar cmake"
raspberrypi_printer="cups printer-driver-gutenprint"
raspberrypi_camera="python3-opencv"

############# Packages install functions #################################

install_python_linters() {
    pip3 install requests bs4 prospector black isort --break-system-packages
}

install_npm_linters() {
    sudo npm install -g jsonlint
    sudo npm install -g fixjson
}

############# MacOS exclusive commands ###################################

# Install homebrew
system_type=$(uname -s)

if [ "$system_type" = "Darwin" ]; then

    # install homebrew if it's missing
    if ! command -v brew >/dev/null 2>&1; then
        echo "Install homebrew? (y/n, default=y)"
        read -r install_homebrew
        install_homebrew=${install_homebrew:-y}
        if echo "$install_homebrew" | grep -qi '^[yj]$'; then
            echo "Installing homebrew"
            /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
        fi
    fi
fi

############# Raspberrypi exclusive commands #############################

if [ "$system_type" = "Linux" ]; then
    install_this_now=""

    echo "Install the most important packages ($raspberrypi_essentials)? (y/n, default=y)"
    read -r install_raspberrypi_essentials
    install_raspberrypi_essentials=${install_raspberrypi_essentials:-y}
    if echo "$install_raspberrypi_essentials" | grep -qi '^[yj]$'; then
        install_this_now="$install_this_now $raspberrypi_essentials"
    fi

    echo "Install the packages for printing? (y/n, default=y)"
    read -r install_raspberrypi_printer
    install_raspberrypi_printer=${install_raspberrypi_printer:-y}
    if echo "$install_raspberrypi_printer" | grep -qi '^[yj]$'; then
        install_this_now="$install_this_now $raspberrypi_printer"
    fi

    echo "Install the packages for the camera? (y/n, default=y)"
    read -r install_raspberrypi_camera
    install_raspberrypi_camera=${install_raspberrypi_camera:-y}
    if echo "$install_raspberrypi_camera" | grep -qi '^[yj]$'; then
        install_this_now="$install_this_now $raspberrypi_camera"
    fi

    if [ -n "$install_this_now" ]; then
        echo "Packages will be installed now!"
        sudo apt update
        for i in $install_this_now; do
            sudo apt-get install -y $i
        done
    fi
fi

if command -v ufw >/dev/null 2>&1; then
    echo "Enable the firewall (ufw with ssh allowed)? (y/n, default=y)"
    read -r enable_ufw
    enable_ufw=${enable_ufw:-y}
    if echo "$enable_ufw" | grep -qi '^[yj]$'; then
        ufw allow ssh
        ufw enable
    fi
fi

############# Commands for both ##########################################

if command -v /bin/zsh >/dev/null 2>&1; then
    echo "Set zsh as default shell? (y/n, default=y)"
    read -r set_default_shell
    set_default_shell=${set_default_shell:-y}
    if echo "$set_default_shell" | grep -qi '^[yj]$'; then
        chsh -s /bin/zsh
    fi
fi

echo "Install all linters for vim? (y/n, default=y)"
read -r install_linters
install_linters=${install_linters:-y}
if echo "$install_linters" | grep -qi '^[yj]$'; then
    install_npm_linters
    install_python_linters
fi

echo "Install the vim plugins now? (y/n, default=y)"
read -r install_vim_plugins
install_vim_plugins=${install_vim_plugins:-y}
if echo "$install_vim_plugins" | grep -qi '^[yj]$'; then
    vim +'PlugInstall --sync' +qa
    echo "Done!"
fi
