#!/bin/bash

flatpakBin=$(/usr/bin/flatpak)
checkApt=$(/usr/bin/apt)
checkXbps=$(/usr/bin/xbps-install)
installFlatpak(){
    if [ ! -f ${flatpakBin} ];then
        if [ -f ${checkXbps} ];then
            sudo xbps-install -S flatpak

        elif [ -f ${checkApt} ];then
            printf "\nChecking for PPA\n"
            if [ ! -f /etc/apt/sources.list.d/alexlarsson-ubuntu-flatpak-xenial.list ];then
                printf "\nInstalling PPA\n"
                sudo add-apt-repository ppa:alexlarsson/flatpak
                sudo apt update
                sudo apt install flatpak
            else
                sudo apt install flatpak
            fi
        else
            printf "\nUnsupported Operating System"
        fi
    else
        printf "\nFlatpak is already installed\n"
    fi
}

addFlatpakRepo(){

    if [ -f ${flatpakBin} ];then 
        printf "\nAdding Flathub Repository..\n"
        flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
    else
        echo "Flatpak is not installed"
    fi
}

getPackages(){
    echo -e  "\nInstalling Shortwave..\n"
    flatpak install flathub shortwave -y

    echo -e "\nInstalling Adapta Theme...\n"
    flatpak install flathub org.gtk.Gtk3theme.Adapta-Nokto-Eta

    echo -e "Installing Bitwarden...\n"
    flatpak install flathub bitwarden -y
}

helpFile(){
    echo -e "\n\nusage: ${0} [all] [init]\n\n"
    echo -e "init         only run initial flatpak setup\n"
    echo -e "\nall          Init setup and packages for jitty\n\n"
}
case ${1} in

    init|-i)
        installFlatpak
        addFlatpakRepo
    ;;

    all|-a)
        installFlatpak
        addFlatpakRepo
        getPackages
    ;;

    -r)
        addFlatpakRepo
    ;;
esac