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

176 lines
4.8 KiB
Text
Raw Normal View History

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:
# ash, dash, bash, zsh
VERBOSE="${VERBOSE:-1}"
2021-07-31 13:24:39 -07:00
NAME="$0"
2021-09-25 00:33:25 -07:00
[ -n "$ZSH_VERSION" -o -n "$BASH_VERSION" ] || VERBOSE=0 # no colors for you
2013-06-28 05:22:14 -07:00
note() {
local IFS=" "
printf "%s\n" "$*"
2013-06-28 05:22:14 -07:00
}
warn() {
note "$@" >&2
}
die() {
2021-07-31 13:24:39 -07:00
warn "$NAME:" "$@"
2013-06-28 05:22:14 -07:00
exit 1
}
2015-03-13 13:04:17 -07:00
backup() {
2021-07-31 14:12:05 -07:00
: "${1:?missing argument}"
2015-03-13 13:04:17 -07:00
note "backing up $1"
mkdir -p "${backup_dir:?backup_dir unset}/$(dirname "$1")"
2021-07-31 13:42:20 -07:00
[ ! -e "$backup_dir/$1" ] || die "backup already exists"
2015-03-13 13:04:17 -07:00
mv "$1" "$backup_dir/$1"
}
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
2021-07-31 13:42:20 -07:00
[ ! "$1" -ef "$2" ] || return 0
if [ -h "$1" ]; then
note "removing symbolic link $1"
rm "$1" || die 'failed to remove symbolic link'
fi
2015-03-13 13:04:17 -07:00
if [ -s "$1" ]; then
backup "$1" || die "$1 already exists"
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
2013-06-28 05:22:14 -07:00
note "removing symbolic link $1"
2021-07-31 13:42:20 -07:00
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}" -gt 0 ] || continue
printf "%s\n" "$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
local d1="$1/$f"
local d2="$2/$f"
if [ -d "$d2" ]; then
if [ "$d1" != ".vim/bundle" ]; then # buggy on Windows
[ "$VERBOSE" -lt 1 ] || warn $'\e[34m / \e[0m' "$d1 $d2"
2021-07-31 13:42:20 -07:00
softlink_pseudo "$d1" "$d2" || exit $?
fi
elif [ -f "$d2" ]; then
[ "$VERBOSE" -lt 1 ] || warn $'\e[34m * \e[0m' "$d1"
2021-07-31 13:42:20 -07:00
hardlink "$d1" "$d2" || exit $?
else
die "i don't know how to pseudo-symlink $d2"
fi
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
local d1="$1/$f"
local d2="$2/$f"
2021-07-31 13:42:20 -07:00
[ "$d1" != ".vim/.netrwhist" ] || continue
[ "$d1" != ".vim/backup" ] || continue
[ "$d1" != ".vim/bundle" ] || continue
[ "$d1" != ".vim/swp" ] || continue
[ "$d1" != ".vim/undo" ] || continue
if [ -d "$d2" ]; then
find_new_files "$d1" "$d2"
elif [ ! "$d1" -ef "$d2" ]; then
if [ "$VERBOSE" -lt 1 ]; then
warn " + $d1"
else
#warn "new destination file. consider manually moving it:"
warn $'\e[32m + \e[0m' "$d1"
fi
fi
done
}
softlink() {
if [ -n "$MSYSTEM" ]; then
# MSYS2 does not have nor emulate symbolic links.
softlink_pseudo "$@"
find_new_files "$@" # to make up for git status not seeing new files
else
softlink_nix "$@"
fi
}
2021-07-31 13:24:39 -07:00
which readlink >/dev/null || die 'failed sanity check (check your $PATH)'
2014-02-14 08:05:48 -08:00
2021-07-31 13:24:39 -07:00
rc="$(readlink -f "$(dirname "$NAME")" )"
[ -d "$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
2019-05-27 10:40:11 -07:00
backup_dir="$rc/backup-$(date -u '+%s')"
2021-07-31 13:24:39 -07:00
[ ! -d "$backup_dir" ] || die 'backup directory already exists'
2013-06-28 05:22:14 -07:00
2021-07-29 00:37:35 -07:00
for f in .bashrc .zshrc .-shrc .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 .mpv; 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
2019-06-05 13:16:01 -07:00
if [ ! -e .bash_profile ] || ! grep -qF .bashrc .bash_profile; then
echo >> .bash_profile
2021-07-31 13:42:20 -07:00
echo '[ ! -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
is_empty() {
local f="${1:?is_empty requires an argument}"
find "$f" -type f | while read -r f; do
return 1
done || return 1 # just in case pipes mess things up
return 0
}
# delete any directory structure that may have been included with the OS.
# note that i'm careful not to delete them if they contain even a single file,
# but you might still want to remove this if you're adapting my install script.
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
2021-01-06 11:53:34 -08:00
if is_empty "$d"; then
2021-07-31 13:42:20 -07:00
rm -r "$d" # doesn't really matter if it fails
2021-01-06 11:53:34 -08:00
else
note "not removing $d because it contains files"
fi
2013-06-28 05:22:14 -07:00
done
2019-06-05 18:24:02 -07:00
# create instead my preferred directory structure.
2013-06-28 05:22:14 -07:00
mkdir -p opt/local/bin src work play