2016-11-14 17:20:36 -08:00
|
|
|
#!/usr/bin/env bash
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
# My (idempotent) dotfile deployment script.
|
|
|
|
#
|
|
|
|
# You pass the directories of "topics" to it to install them. It will:
|
2017-01-08 21:44:01 -08:00
|
|
|
# 1. symlink $topic/.* => ~/ and $topic/.(config|local)/* => ~/.\1/
|
|
|
|
# 2. run $topic/install the first time
|
|
|
|
# 3. run $topic/update on consecutive runs
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
set -e
|
2016-12-16 15:33:59 -08:00
|
|
|
source "$(cd $(dirname $0) && pwd -P)/shell/common.sh"
|
2016-11-14 17:20:36 -08:00
|
|
|
shopt -s nullglob
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
#
|
|
|
|
blue="\033[34m"
|
|
|
|
magenta="\033[35m"
|
|
|
|
green="\033[32m"
|
|
|
|
orange="\033[33m"
|
|
|
|
grey="\033[36m"
|
|
|
|
red="\033[31m"
|
|
|
|
nc="\033[0m"
|
|
|
|
#
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
_run() {
|
|
|
|
local cmd="${@//$DOTFILES\//}"
|
|
|
|
cmd="${cmd//$HOME/~}"
|
|
|
|
local clr=""
|
|
|
|
case $cmd in
|
|
|
|
*~/.bin/) clr="${magenta}" ;;
|
|
|
|
ln*) clr="" ;;
|
|
|
|
mkdir*) clr="${blue}" ;;
|
|
|
|
*) clr="${orange}"
|
|
|
|
esac
|
|
|
|
echo -e " $PREFIX$clr$cmd$nc"
|
|
|
|
[[ $DRYRUN ]] || { $@ | sed 's/^/ => /'; }
|
2016-10-19 07:56:37 -07:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
_link() {
|
|
|
|
if [[ -f "$2" || ( -d "$2" && -f "$2/${1##*/}" ) ]]; then
|
|
|
|
if [[ $yes ]]; then
|
|
|
|
[[ $yes = 1 ]] && choice=y || choice=n
|
|
|
|
else
|
|
|
|
read -n 1 -p "? ${1##*/} exists in $2 -- overwrite? [y/n] " choice
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
case $choice in
|
2016-12-17 00:11:16 -08:00
|
|
|
y|Y) _run rm -rf "$2"
|
|
|
|
PREFIX="${red}OVERWRITE:${nc} " _run ln -svf "$1" "$2"
|
|
|
|
;;
|
2016-11-14 17:20:36 -08:00
|
|
|
*) PREFIX="${red}ABORTED:${nc} " DRYRUN=1 _run ln -svf "$1" "$2"
|
2016-12-17 00:11:16 -08:00
|
|
|
;;
|
2016-11-14 17:20:36 -08:00
|
|
|
esac
|
|
|
|
else
|
|
|
|
_run ln -svf "$1" "$2"
|
2016-10-19 07:56:37 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:23:55 -08:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
##
|
|
|
|
install=1
|
|
|
|
link=1
|
|
|
|
update=1
|
2016-12-17 00:11:16 -08:00
|
|
|
yes=0
|
2017-01-03 00:56:24 -08:00
|
|
|
while getopts iuldypLSUh opt; do
|
2016-11-14 17:20:36 -08:00
|
|
|
case $opt in
|
|
|
|
d) DRYRUN=1 ;;
|
|
|
|
i) install= ;;
|
|
|
|
l) link= ;;
|
2016-12-17 00:11:16 -08:00
|
|
|
p) yes= ;;
|
2016-11-14 17:20:36 -08:00
|
|
|
u) update= ;;
|
|
|
|
y) yes=1 ;;
|
2017-01-03 00:56:24 -08:00
|
|
|
S) SERVER=1 ;;
|
|
|
|
U) install=
|
|
|
|
yes=0
|
|
|
|
;;
|
|
|
|
L) update=
|
2016-12-23 00:15:47 -08:00
|
|
|
install=
|
2017-01-03 00:56:24 -08:00
|
|
|
yes=0
|
2016-12-23 00:15:47 -08:00
|
|
|
;;
|
2017-01-03 00:56:24 -08:00
|
|
|
h) cat <<EOL
|
|
|
|
Usage: $0 [-iuldypLSU] [topic1] [topic2] [...]
|
|
|
|
|
|
|
|
-i Don't run install scripts
|
|
|
|
-u Don't run update scripts
|
2017-01-08 21:44:01 -08:00
|
|
|
-l Don't symlink dotfiles
|
2017-01-03 00:56:24 -08:00
|
|
|
-d Do a dry run (only output commands, no changes)
|
|
|
|
-y Overwrite conflicts (no prompts)
|
|
|
|
-p Prompt on file conflict (to overwrite)
|
|
|
|
-L Refresh links; the same as -ui
|
|
|
|
-S Server mode; only install the bare minimum
|
|
|
|
-U Update mode; run update scripts only
|
|
|
|
EOL
|
|
|
|
exit
|
|
|
|
;;
|
2016-11-14 17:20:36 -08:00
|
|
|
*) >&2 echo "Aborted."
|
|
|
|
exit 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
#
|
2017-01-05 21:14:47 -08:00
|
|
|
CWD=$(cd $(dirname $0) && pwd -P)
|
2017-01-08 21:44:01 -08:00
|
|
|
ENABLED_DIR="$CWD/.enabled.d"
|
|
|
|
pushd $CWD >/dev/null
|
|
|
|
for topic in ${@:-$(ls -1 "$ENABLED_DIR" | sed 's%_%/%')}; do
|
2016-11-14 17:20:36 -08:00
|
|
|
[[ -d $topic ]] || continue
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2017-01-08 21:44:01 -08:00
|
|
|
echo -e "${green}${topic}${nc}"
|
2016-11-14 17:20:36 -08:00
|
|
|
|
2017-01-08 21:44:01 -08:00
|
|
|
tpath="$CWD/$topic"
|
2016-12-25 16:08:21 -08:00
|
|
|
if [[ $link ]]; then
|
2017-01-08 21:44:01 -08:00
|
|
|
# Symlink dotfiles from topics (2 levels deep) + parent (1 level deep)
|
|
|
|
for file in "$tpath"{,../}/.[a-zA-Z]*; do
|
|
|
|
if [[ -d "$file" && "${file##*/}" == .@(config|local) ]]; then
|
|
|
|
for subfile in "$file"/*; do
|
|
|
|
base="$HOME/${file##*/}/"
|
|
|
|
[[ -d $base ]] || _run mkdir -p "$base"
|
|
|
|
_link "$subfile" "$base"
|
|
|
|
done
|
|
|
|
elif [[ -e $file ]]; then
|
|
|
|
_link "$file" "$HOME/"
|
|
|
|
fi
|
2016-12-25 16:08:21 -08:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
# Link topic to .enabled.d
|
2017-01-08 21:44:01 -08:00
|
|
|
linkpath="${ENABLED_DIR}/${topic//\//_}"
|
2017-01-06 16:46:14 -08:00
|
|
|
if [[ -h "$linkpath" ]]; then
|
2017-01-08 21:44:01 -08:00
|
|
|
[[ "$update" && -x ${linkpath}/update ]] && _run "$tpath/update"
|
2016-11-14 17:20:36 -08:00
|
|
|
else
|
|
|
|
# Register topic as 'enabled'
|
2017-01-08 21:44:01 -08:00
|
|
|
_run ln -svf "$tpath" "$linkpath"
|
2016-11-14 17:20:36 -08:00
|
|
|
|
|
|
|
# Run install script
|
2017-01-08 21:44:01 -08:00
|
|
|
[[ "$install" && -x ${linkpath}/install ]] && _run "$tpath/install"
|
2016-11-14 17:20:36 -08:00
|
|
|
fi
|
|
|
|
done
|
2017-01-08 21:44:01 -08:00
|
|
|
popd >/dev/null
|
2016-10-19 07:56:37 -07:00
|
|
|
|
2016-11-14 17:20:36 -08:00
|
|
|
# vim:set ft=sh:
|