1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-13 20:29:02 -08:00
rc/install

168 lines
4.6 KiB
Text
Raw Normal View History

2019-06-03 08:49:32 -07:00
#!/usr/bin/env sh
2024-02-24 09:02:36 -08:00
# for busybox ash, dash, bash, and zsh.
VERBOSE="${VERBOSE-1}"
2021-07-31 13:24:39 -07:00
NAME="$0"
die() {
printf '%s:' "$NAME"
printf ' %s' "$@"
printf '\n'
2013-06-28 05:22:14 -07:00
exit 1
} >&2
2013-06-28 05:22:14 -07:00
pl() { # print lines
printf '%s\n' "$@"
}
2015-03-13 13:04:17 -07:00
backup() {
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
pl "backing up $1"
set -- "$1" "${backup_dir:?backup_dir unset}" # shorthand for my own sanity
[ -d "$2" ] || mkdir "$2" || die "failed to create backup directory"
if [ ${1%/*} != "$1" ]; then
mkdir -p "$2/${1%/*}" || die "failed to create backup directory"
fi
! [ -e "$2/$1" ] || die "backup already exists: $2/$1"
mv "$1" "$2/$1" || die "failed to backup $1"
2015-03-13 13:04:17 -07:00
}
2013-06-28 05:22:14 -07:00
hardlink() {
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
: "${2:?missing argument}"
2015-03-13 13:04:17 -07:00
if [ -e "$1" ]; then
! [ "$1" -ef "$2" ] || return 0
2021-07-31 13:42:20 -07:00
if [ -h "$1" ]; then
pl "removing symbolic link $1"
rm "$1" || die "failed to remove symbolic link"
2021-07-31 13:42:20 -07:00
fi
2015-03-13 13:04:17 -07:00
if [ -s "$1" ]; then
#diff -U3 "$1" "$2" >>/tmp/installed.patch
backup "$1"
2015-03-13 13:04:17 -07:00
fi
fi
2013-06-28 05:22:14 -07:00
2021-07-31 13:42:20 -07:00
ln "$2" "$1" || die "failed to hardlink $1"
2013-06-28 05:22:14 -07:00
}
softlink_nix() {
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
: "${2:?missing argument}"
2015-03-13 13:04:17 -07:00
if [ -e "$1" ]; then
if [ -h "$1" ]; then
2021-07-31 13:42:20 -07:00
[ "$(readlink "$1")" != "$2" ] || return 0
pl "removing symbolic link $1"
rm "$1" || die "failed to remove symbolic link"
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
2021-07-31 13:42:20 -07:00
ln -s "$2" "$1" || die "failed to symlink $1"
2013-06-28 05:22:14 -07:00
}
list_files() {
find "${1:-.}" -maxdepth 1 -printf "%P\n" | while read -r f; do
[ "${#f}" != 0 ] || continue
pl "$f"
done
}
softlink_pseudo() (
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
: "${2:?missing argument}"
[ -d "$2" ] || die "$1 is not a directory to softlink"
2021-07-31 13:42:20 -07:00
[ -d "$1" ] || mkdir "$1" || die "failed to mkdir $1"
list_files "$2" | while read -r f; do
d1="$1/$f"
d2="$2/$f"
if [ -d "$d2" ]; then
if [ "$d1" != ".vim/bundle" ]; then # buggy on Windows
[ "$VERBOSE" -lt 2 ] || printf '\033[34m / \033[0m %s %s\n' "$d1" "$d2" >&2
2021-10-05 10:59:05 -07:00
softlink_pseudo "$d1" "$d2" || exit
fi
elif [ -f "$d2" ]; then
[ "$VERBOSE" -lt 2 ] || printf '\033[34m * \033[0m %s\n' "$d1" >&2
2021-10-05 10:59:05 -07:00
hardlink "$d1" "$d2" || exit
else
die "i don't know how to pseudo-symlink $d2"
fi
2021-10-05 10:59:05 -07:00
done || exit
)
find_new_files() (
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
2021-07-31 16:15:07 -07:00
: "${2:?missing argument}"
list_files "$1" | while read -r f; do
d1="$1/$f"
d2="$2/$f"
case "$d1" in
(.vim/.netrwhist) continue;;
(.vim/backup) continue;;
(.vim/bundle) continue;;
(.vim/swp) continue;;
(.vim/undo) continue;;
esac
if [ -d "$d2" ]; then
find_new_files "$d1" "$d2" || exit
elif ! [ "$d1" -ef "$d2" ]; then
[ -d "$d1" ] && ind=/ || ind=
if [ "$VERBOSE" -lt 1 ]; then
printf ' + %s%s\n' "$d1" "$ind" >&2
else
#printf 'new destination file. consider manually moving it:\n' >&2
printf '\033[32m + \033[0m %s%s\n' "$d1" "$ind" >&2
fi
fi
done
)
softlink() {
if [ -n "$MSYSTEM" ]; then
# MSYS2 does not have nor emulate symbolic links.
softlink_pseudo "$@" || exit
# to make up for git status not seeing new files:
find_new_files "$@" || exit
else
softlink_nix "$@"
fi
}
readlink -f / >/dev/null || die 'failed sanity check (check your $PATH)'
2014-02-14 08:05:48 -08:00
unset CDPATH
rc="$(readlink -f "$0")" && rc="${rc%/*}" && cd "$rc" || die "failed to determine rc directory"
cd "${HOME:?HOME variable empty or unset}" || die "failed to change directory"
2013-06-28 05:22:14 -07:00
backup_dir="$rc/backup-$(date -u +%s)" || die "failed to determine date"
! [ -d "$backup_dir" ] || die "backup directory already exists"
2013-06-28 05:22:14 -07:00
for f in \
.shrc .bashrc .zshrc .prep .prezto-compinit .ls_colors \
.vimrc .inputrc .Xresources .screenrc .tmux.conf \
; do
hardlink "$f" "$rc/home/${f#.}"
2013-06-28 05:22:14 -07:00
done
for d in sh .vim; do
softlink "$d" "$rc/${d#.}"
2013-06-28 05:22:14 -07:00
done
2019-06-05 19:09:37 -07:00
# ensure that .bashrc gets executed
if ! [ -e .bash_profile ] || ! grep -qF .bashrc .bash_profile; then
pl '' '! [ -f ~/.bashrc ] || . ~/.bashrc' >>.bash_profile
2019-06-05 13:16:01 -07:00
fi
2013-06-28 05:22:14 -07:00
2019-06-05 18:24:02 -07:00
# delete any directory structure that may have been included with the OS.
for d in bin Desktop Documents Downloads Music Pictures Public Templates Video Videos; do
2024-03-25 16:11:17 -07:00
! [ -d "$d" ] || rmdir "$d"
2013-06-28 05:22:14 -07:00
done
2019-06-05 18:24:02 -07:00
# create instead my preferred directory structure.
mkdir -p .local/bin src work play || die "failed to create directories"