#!/usr/bin/env sh
# Automatically deploys my rice onto a new system

git_url="https://github.com/agryphus/archrice.git"
location="$HOME/repos/dotfiles"

if [ ! -d "$location" ]; then
    mkdir -p "$location"
fi

cd "$location"

if [ "$(git remote get-url origin 2> /dev/null)" != "$git_url" ]; then
    git clone --bare "$git_url" .
fi

alias config="git --git-dir $location --work-tree=$HOME"
config config status.showUntrackedFiles no

# Checkout branch, but move collisions to ~/.config-backup
if [ ! -e "$location/checked" ]; then
    mkdir -p "$HOME/.config-backup"
    config checkout 2>&1 \
        | egrep "\s+\." | awk {'print $1'} \
        | xargs -I {} mv {} "$HOME/.config-backup/"{}

    # Fix unsafe permissions on gnupg directory
    chmod 700 "$HOME/.local/share/gnupg/"

    # Empty file, indicating the repo has been checked out once
    touch "$location/checked"
fi

config submodule init
config submodule update --force --recursive --init --remote
config submodule foreach "git checkout master"

cd $HOME

exec zsh

