dotfiles/bin/dotfiles

92 lines
2.3 KiB
Bash
Executable file

#!/bin/zsh -e
# Install via curl -o- -L https://raw.githubusercontent.com/Klowner/dotfiles/bearmode/bin/dotfiles | zsh
DOTFILES_HOME="${DOTFILES_HOME:-$HOME}"
DOTFILES_ROOT="${DOTFILES_ROOT:-$DOTFILES_HOME/.dotfiles}"
DOTFILES_REMOTE="${DOTFILES_REMOTE:-git@github.com:Klowner/dotfiles}"
DOTFILES_BRANCH="${DOTFILES_BRANCH:-main}"
DOTFILES_WORK_TREE="${DOTFILES_WORK_TREE:-$HOME}"
DOTFILES_ALIAS="/usr/bin/git --git-dir=${DOTFILES_ROOT} --work-tree=${DOTFILES_WORK_TREE}"
DEPENDENCIES=(git zsh ssh)
function has() {
return $(which $1 &> /dev/null)
}
function die() {
echo $*
exit 1
}
function info() {
echo "DOTFILES_ROOT: ${DOTFILES_ROOT}"
echo "DOTFILES_REMOTE: ${DOTFILES_REMOTE}"
echo "DOTFILES_WORK_TREE: ${DOTFILES_WORK_TREE}"
echo "DOTFILES_ALIAS: ${DOTFILES_ALIAS}"
}
function smart_clone() {
if [ -d $DOTFILES_ROOT ]; then
EXISTING_REMOTE=$(git --git-dir=${DOTFILES_ROOT} remote get-url origin)
if [ "$EXISTING_REMOTE" = "$DOTFILES_REMOTE" ]; then
dotfiles fetch
else
die "$DOTFILES_ROOT already exists, aborting."
fi
else
git clone --bare $DOTFILES_REMOTE $DOTFILES_ROOT --branch $DOTFILES_BRANCH
fi
}
function check_requirements() {
for DEP in ${DEPENDENCIES[@]}; do
has $DEP || die "${DEP} not found, please install and try again."
done
}
function dotfiles() {
eval $DOTFILES_ALIAS $*
}
function backup_existing() {
dotfiles checkout
DST="${DOTFILES_ROOT}-backup"
SRC="${DOTFILES_WORK_TREE}"
mkdir -p $DST && \
dotfiles checkout 2>&1 | grep -E "\s+\." | awk {'print $1'} | \
xargs -d $'\n' sh -c "for arg do mkdir -p ${DST}/\${arg%/*} && mv -v ${SRC}/\$arg ${DST}/\$arg; done" _
}
function install() {
check_requirements
smart_clone
dotfiles checkout || backup_existing && dotfiles checkout
dotfiles config --local status.showUntrackedFiles no
}
function echo_alias {
echo $DOTFILES_ALIAS
exit 0
}
# If this script is being piped from curl
if ! tty >/dev/null; then
install
exit 0
fi
# Command line mode
case "${1:-help}" in
info) info;;
install) install;;
alias) echo_alias;;
help)
echo "dotfiles commands: "
echo " - install"
echo " - info"
echo " - alias"
echo "all other commands will be forwarded to git"
;;
*) eval $DOTFILES_ALIAS $*;;
esac
# vim:ts=2:sw=2:et: