#!/bin/sh
set -e

#
# This script should be run via curl:
#   sh -c "$(curl -fsSL https://gitlab.hedenstroem.com/-/snippets/22/raw/main/install-dotfiles.sh)"
# or via wget:
#   sh -c "$(wget -qO- https://gitlab.hedenstroem.com/-/snippets/22/raw/main/install-dotfiles.sh)"
# or via fetch:
#   sh -c "$(fetch -o - https://gitlab.hedenstroem.com/-/snippets/22/raw/main/install-dotfiles.sh)"
#
# As an alternative, you can first download the install script and run it afterwards:
#   wget https://gitlab.hedenstroem.com/-/snippets/22/raw/main/install-dotfiles.sh
#   sh install.sh

USER=${USER:-$(id -u -n)}
HOME="${HOME:-$(getent passwd $USER 2>/dev/null | cut -d: -f6)}"
HOME="${HOME:-$(eval echo ~$USER)}"
TIMESTAMP=$(date +%s)

command_exists() {
    command -v "$@" >/dev/null 2>&1
}

setup_dotfiles() {
    if [ ! -d "${HOME}/.dotfiles" ]; then
        git clone --depth=1 git@gitlab.hedenstroem.com:erik/dotfiles.git "${HOME}/.dotfiles"
    fi
}

main() {

    command_exists git || {
        echo "git is not installed"
        exit 1
    }

    setup_dotfiles

    sh "${HOME}/.dotfiles/bootstrap.sh" 

}

main "$@"
