2019-06-03 08:49:32 -07:00
|
|
|
#!/usr/bin/env sh
|
2019-06-05 13:16:01 -07:00
|
|
|
# this script is compatible with following shells:
|
|
|
|
# dash, ash, bash, zsh
|
2016-11-01 15:22:38 -07:00
|
|
|
|
2013-06-28 05:22:14 -07:00
|
|
|
note() {
|
2019-06-05 13:16:01 -07:00
|
|
|
printf "%s\n" "$@"
|
2013-06-28 05:22:14 -07:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:22:38 -07:00
|
|
|
warn() {
|
2019-06-05 13:16:01 -07:00
|
|
|
printf "%s\n" "$@" >&2
|
2016-11-01 15:22:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
|
|
|
warn "$@"
|
2013-06-28 05:22:14 -07:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
dotless() {
|
2019-06-05 13:16:01 -07:00
|
|
|
local f="$1"
|
|
|
|
local ind="$(expr index "$f" .)"
|
|
|
|
if [ "$ind" -gt 0 ]; then
|
|
|
|
local len="$(expr length "$f")"
|
|
|
|
REPLY="$(expr substr "$f" 2 "$len")"
|
|
|
|
else
|
|
|
|
REPLY="$1"
|
|
|
|
fi
|
|
|
|
#warn "$f -> $REPLY"
|
2013-06-28 05:22:14 -07:00
|
|
|
}
|
|
|
|
|
2015-03-13 13:04:17 -07:00
|
|
|
backup() {
|
|
|
|
note "backing up $1"
|
|
|
|
mkdir -p "${backup_dir:?backup_dir unset}/$(dirname "$1")"
|
|
|
|
[ -e "$backup_dir/$1" ] && die "backup already exists"
|
|
|
|
mv "$1" "$backup_dir/$1"
|
|
|
|
}
|
|
|
|
|
2013-06-28 05:22:14 -07:00
|
|
|
hardlink() {
|
2015-03-13 13:04:17 -07:00
|
|
|
if [ -e "$1" ]; then
|
2013-06-28 05:22:14 -07:00
|
|
|
[ "$1" -ef "$2" ] && return
|
2013-11-01 20:36:36 -07:00
|
|
|
[ -h "$1" ] && note "removing symbolic link $1" && rm "$1"
|
2015-03-13 13:04:17 -07:00
|
|
|
if [ -s "$1" ]; then
|
2017-11-04 06:45:26 -07:00
|
|
|
backup "$1" || die "$1 already exists"
|
2015-03-13 13:04:17 -07:00
|
|
|
fi
|
|
|
|
fi
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2013-06-29 09:52:14 -07:00
|
|
|
ln "$2" "$1" || die "couldn't hardlink $1"
|
2013-06-28 05:22:14 -07:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:22:38 -07:00
|
|
|
softlink_nix() {
|
2015-03-13 13:04:17 -07:00
|
|
|
if [ -e "$1" ]; then
|
|
|
|
if [ -h "$1" ]; then
|
2019-06-05 13:16:01 -07:00
|
|
|
[ "$(readlink "$1")" = "$2" ] && return
|
2013-06-28 05:22:14 -07:00
|
|
|
note "removing symbolic link $1"
|
2013-11-01 20:36:36 -07:00
|
|
|
rm "$1"
|
2015-03-13 13:04:17 -07:00
|
|
|
else
|
|
|
|
die "$1 already exists and is not a symbolic link"
|
|
|
|
fi
|
|
|
|
fi
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2013-06-29 09:52:14 -07:00
|
|
|
ln -s "$2" "$1" || die "couldn't symlink $1"
|
2013-06-28 05:22:14 -07:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:22:38 -07:00
|
|
|
softlink_pseudo() {
|
|
|
|
[ -d "$2" ] || die "$1 is not a directory to softlink"
|
|
|
|
|
|
|
|
if [ ! -d "$1" ]; then
|
|
|
|
mkdir "$1" || die "couldn't mkdir $1"
|
|
|
|
fi
|
|
|
|
|
2019-06-05 13:16:01 -07:00
|
|
|
ls -A "$2" | while read -r REPLY; do
|
2016-11-01 15:22:38 -07:00
|
|
|
local d1="$1/$REPLY"
|
|
|
|
local d2="$2/$REPLY"
|
|
|
|
if [ -d "$d2" ]; then
|
2019-05-27 10:40:36 -07:00
|
|
|
if [ "$d1" != ".vim/bundle" ]; then # buggy on Windows
|
|
|
|
#warn $'\e[34m / \e[0m' "$d1 $d2"
|
|
|
|
softlink_pseudo "$d1" "$d2" || exit 1
|
|
|
|
fi
|
2016-11-01 15:22:38 -07:00
|
|
|
elif [ -f "$d2" ]; then
|
2019-05-27 10:40:36 -07:00
|
|
|
#warn $'\e[34m * \e[0m' "$d1"
|
2016-11-01 15:22:38 -07:00
|
|
|
hardlink "$d1" "$d2"
|
|
|
|
else
|
|
|
|
die "i don't know how to pseudo-symlink $d2"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2019-06-05 13:16:01 -07:00
|
|
|
ls -A "$1" | while read -r REPLY; do
|
2016-11-01 15:22:38 -07:00
|
|
|
local d1="$1/$REPLY"
|
|
|
|
local d2="$2/$REPLY"
|
|
|
|
if [ -d "$d2" ]; then
|
|
|
|
:
|
|
|
|
elif [ ! "$d1" -ef "$d2" ]; then
|
2019-06-05 13:16:01 -07:00
|
|
|
[ "$d1" = ".vim/.netrwhist" ] && continue
|
|
|
|
[ "$d1" = ".vim/backup" ] && continue
|
|
|
|
[ "$d1" = ".vim/bundle" ] && continue
|
|
|
|
[ "$d1" = ".vim/swp" ] && continue
|
|
|
|
[ "$d1" = ".vim/undo" ] && continue
|
2019-05-27 10:40:36 -07:00
|
|
|
#warn "new destination file. consider manually moving it:"
|
|
|
|
#warn $'\e[32m + \e[0m' "$d1"
|
2019-06-05 17:11:16 -07:00
|
|
|
warn " + $d1"
|
2016-11-01 15:22:38 -07:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
softlink() {
|
|
|
|
if [ -n "$MSYSTEM" ]; then
|
|
|
|
# MSYS2 does not have nor emulate symbolic links.
|
2019-05-27 10:40:36 -07:00
|
|
|
softlink_pseudo "$@" || exit 1
|
2016-11-01 15:22:38 -07:00
|
|
|
else
|
|
|
|
softlink_nix "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-05-27 10:40:36 -07:00
|
|
|
which readlink >/dev/null || exit 1 # sanity check
|
2014-02-14 08:05:48 -08:00
|
|
|
|
2013-06-28 05:22:14 -07:00
|
|
|
rc="$(readlink -f "$(dirname "$0")" )"
|
2013-11-01 20:36:36 -07:00
|
|
|
cd "$HOME"
|
2013-07-09 15:10:54 -07:00
|
|
|
PATH="${PATH:?No existing PATH}:$rc/sh"
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2019-05-27 10:40:11 -07:00
|
|
|
backup_dir="$rc/backup-$(date -u '+%s')"
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2019-06-03 09:27:20 -07:00
|
|
|
for f in .bashrc .zshrc .-shrc .arrays .streamcrap .ea .vimrc \
|
2019-06-05 16:03:15 -07:00
|
|
|
.inputrc .Xresources .screenrc .tmux.conf; do
|
2013-07-09 15:10:54 -07:00
|
|
|
dotless "$f"
|
|
|
|
r="$rc/home/$REPLY"
|
2013-06-28 05:22:14 -07:00
|
|
|
hardlink "$f" "$r"
|
|
|
|
done
|
|
|
|
|
2014-03-06 22:23:57 -08:00
|
|
|
for d in sh .vim .mpv; do
|
2013-07-09 15:10:54 -07:00
|
|
|
dotless "$d"
|
|
|
|
r="$rc/$REPLY"
|
2013-06-28 05:22:14 -07:00
|
|
|
softlink "$d" "$r"
|
|
|
|
done
|
|
|
|
|
2019-06-05 13:16:01 -07:00
|
|
|
if [ ! -e .bash_profile ] || ! grep -qF .bashrc .bash_profile; then
|
|
|
|
echo >> .bash_profile
|
|
|
|
echo '[[ -f ~/.bashrc ]] && . ~/.bashrc' >> .bash_profile
|
|
|
|
fi
|
2013-06-28 05:22:14 -07:00
|
|
|
|
2015-04-01 22:15:46 -07:00
|
|
|
for d in Desktop Documents Downloads Music Pictures Public Templates Video Videos; do
|
2013-06-28 05:22:14 -07:00
|
|
|
[ -d "$d" ] || continue
|
2019-06-05 17:11:16 -07:00
|
|
|
is_empty "$d" && rm -r "$d" || note "not removing $d because it contains files"
|
2013-06-28 05:22:14 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
mkdir -p opt/local/bin src work play
|