#!/bin/sh

mkdir -p "$HOME/.cache/phyos/"{dwm,dots}
cd "$HOME/.cache/phyos/$1"
dots="/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME"

if [ "$1" == "dwm" ]; then
    wget "https://raw.githubusercontent.com/FT-Labs/phyOS-PKGBUILD/master/CORE/phyOS-dwm/PKGBUILD"
    makepkg -si --noconfirm
    kill -HUP $(pidof -s dwm) && xdotool key Super_L
    rm -rf "$1-phyOS" src pkg *.zst PKGBUILD
elif [ "$1" == "dots" ] && [ "$2" == "--force" ]; then
        yes | rm -r ~/.cfg
        git clone "https://github.com/FT-Labs/dotfiles" && mv dotfiles/.git "$HOME/.cfg" && cp -r dotfiles/.* "$HOME/" || rm -rf dotfiles
        $dots config --local status.showUntrackedFiles no
        $dots checkout -b local
elif [ "$1" == "dots" ]; then
    if [[ -d "$HOME/.cfg" ]]; then
        $dots pull origin master --rebase
        $dots checkout local
        $dots add -u
        $dots commit -m "local"
        $dots merge master
    else
        git clone "https://github.com/FT-Labs/dotfiles" && mv dotfiles/.git "$HOME/.cfg" && cp -r dotfiles/.* "$HOME/" || rm -rf dotfiles
        $dots config --local status.showUntrackedFiles no
        $dots checkout -b local
    fi
fi
