2019-12-30 08:13:23 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# autostow.sh
|
|
|
|
#
|
|
|
|
# Stow automatic symlinking script
|
|
|
|
#
|
|
|
|
# Invokes stow for every folder within this repository, and that's it.
|
|
|
|
|
2019-12-30 10:00:00 +00:00
|
|
|
ignore="${AUTOSTOW_IGNORED_DIRS:-"bootstrap:assets"}"
|
|
|
|
|
|
|
|
main() {
|
|
|
|
case "$1" in
|
|
|
|
-s | --stow)
|
|
|
|
have_stow
|
|
|
|
echo "Creating dotfile symlinks ................................................."
|
|
|
|
stow_dirs
|
|
|
|
echo "Done creating symlinks ...................................................."
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-d | --delete)
|
|
|
|
have_stow
|
|
|
|
echo "Removing dotfile symlinks ................................................."
|
|
|
|
unstow_dirs
|
|
|
|
echo "Done removing symlinks ...................................................."
|
|
|
|
exit 0
|
|
|
|
;;
|
2020-01-30 11:50:37 +00:00
|
|
|
-n | --dry-run)
|
|
|
|
have_stow
|
|
|
|
echo "Printing processed directories ............................................"
|
|
|
|
dryrun
|
|
|
|
echo "Done printing directories ................................................."
|
|
|
|
exit 0
|
|
|
|
;;
|
2019-12-30 10:00:00 +00:00
|
|
|
-v | --version)
|
|
|
|
printf "System bootstrap script.\n\n©Marty Oehme\n\nVersion: 0.2\n"
|
|
|
|
;;
|
|
|
|
-h | --help | *)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
}
|
|
|
|
|
2019-12-30 08:13:23 +00:00
|
|
|
stow_dirs() {
|
|
|
|
for d in */; do
|
2019-12-30 10:00:00 +00:00
|
|
|
|
|
|
|
if is_ignored "$d"; then
|
|
|
|
printf "ignoring %s\n" "$d"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2019-12-30 08:13:23 +00:00
|
|
|
printf "stowing %s\n" "$d"
|
|
|
|
stow -S -t ~ "$d"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
unstow_dirs() {
|
|
|
|
for d in */; do
|
2019-12-30 10:00:00 +00:00
|
|
|
|
|
|
|
if is_ignored "$d"; then
|
|
|
|
printf "ignoring %s\n" "$d"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2019-12-30 08:13:23 +00:00
|
|
|
printf "unstowing %s\n" "$d"
|
|
|
|
stow -D -t ~ "$d"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-01-30 11:50:37 +00:00
|
|
|
dryrun() {
|
|
|
|
for d in */; do
|
|
|
|
|
|
|
|
if is_ignored "$d"; then
|
|
|
|
printf "ignoring %s\n" "$d"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
printf "processing %s\n" "$d"
|
|
|
|
done
|
2020-01-30 12:21:01 +00:00
|
|
|
echo "Done printing directories ................................................."
|
2020-01-30 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:00:00 +00:00
|
|
|
is_ignored() {
|
|
|
|
IFS=":"
|
|
|
|
for ign in $ignore; do
|
2020-01-30 11:51:48 +00:00
|
|
|
# it is either passed in through our environment variable
|
2020-01-30 12:21:01 +00:00
|
|
|
if [ "$ign" = "$1" ] || [ "$ign/" = "$1" ]; then
|
2019-12-30 10:00:00 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2020-01-30 12:21:01 +00:00
|
|
|
# or it starts with a _ which is ignored by default (that's the regex).
|
|
|
|
# (using herestring to avoid cat>grep)
|
|
|
|
if grep -q -e '^_[[:alnum:]]\{1,\}' <<<"$1"; then return 0; fi
|
2019-12-30 10:00:00 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-12-30 08:13:23 +00:00
|
|
|
have_stow() {
|
|
|
|
if ! type stow >/dev/null 2>&1; then
|
|
|
|
printf "GNU stow needs to be installed for this script to function. Please install stow through your package manager.\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
printf "%s\n" \
|
|
|
|
"" \
|
|
|
|
" autostow.sh - Automatically stow your dotfiles." \
|
|
|
|
" Uses GNU stow to set up automatic links to any dotfiles in subdirectories of this directory." \
|
|
|
|
"" \
|
2020-01-30 11:50:37 +00:00
|
|
|
" Usage: stow.sh -dhsn" \
|
2019-12-30 08:13:23 +00:00
|
|
|
"" \
|
|
|
|
" Options:" \
|
|
|
|
"" \
|
2020-01-30 11:50:37 +00:00
|
|
|
" -h | --help Print out this help." \
|
|
|
|
"" \
|
|
|
|
" -s | --stow Install dotfiles, by symlinking any directory found next to stow.sh using GNU stow." \
|
|
|
|
"" \
|
|
|
|
" -d | --delete Remove dotfiles, by unlinking any directory found next to stow.sh using GNU stow." \
|
2019-12-30 08:13:23 +00:00
|
|
|
"" \
|
2020-01-30 11:50:37 +00:00
|
|
|
" -n | --dry-run Do not invoke any operation but print out directories affected, simulating a dry-run." \
|
2019-12-30 08:13:23 +00:00
|
|
|
"" \
|
2020-01-30 11:51:48 +00:00
|
|
|
" Note, by default any directory starting with an underscore _directoryname will be ignored." \
|
|
|
|
" Additional folders to ignore can be set through the environment variable AUTOSTOW_IGNORED_DIRS " \
|
|
|
|
" using a colon-separated string: AUTOSTOW_IGNORED_DIRS=\"directories:to:ignore\" " \
|
2019-12-30 08:13:23 +00:00
|
|
|
"" \
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|