Skip to content

Instantly share code, notes, and snippets.

@webb
Created January 11, 2017 14:17
Show Gist options
  • Save webb/412aee140f286ec199df6023ea9d7074 to your computer and use it in GitHub Desktop.
Save webb/412aee140f286ec199df6023ea9d7074 to your computer and use it in GitHub Desktop.
Script for stowing all the packages in the ~/stow dir
#!/usr/bin/env bash
set -o nounset -o errexit
#HELP:COMMAND_NAME: put packages in their place
#HELP:Options:
command="$0"
#HELP: --help | -h: print this help
opt_help () {
grep -e "^#HELP:" "$0" | sed -e 's/^#HELP://' | m4 -D COMMAND_NAME="$command"
exit 0
}
#HELP: --delete | -d: delete packages instead of restowing them
stow_mode_flag=--restow
opt_delete () {
stow_mode_flag=--delete
}
#HELP: --verbose | -v: be wordy, loquacious, talkative
verbose_mode_flag=""
opt_verbose () {
verbose_mode_flag=--verbose
}
is_verbose () {
[[ $verbose_mode_flag = --verbose ]]
}
verbose_run () {
if is_verbose
then printf "#"
printf " %q" "$@"
printf "\n"
fi
"$@"
}
OPTIND=1
while getopts :hdv-: option
do
case "$option" in
h ) opt_help;;
d ) opt_delete;;
v ) opt_verbose;;
- ) case "$OPTARG" in
help ) opt_help;;
delete ) opt_delete;;
verbose ) opt_verbose;;
help=* | delete=* | verbose=* )
printf "No argument expected for long option \"%s\"\n" "${OPTARG%%=*}"
exit 1;;
* ) printf "Unknown long option \"%s\"\n" "$OPTARG"
exit 1;;
esac;;
'?' ) printf "Unknown short option \"%s\"\n" "$OPTARG"
exit 1;;
: ) printf "Short option \"%s\" missing argument" "$OPTARG"
exit 1;;
* ) printf "Bad state in getopts (OPTARG=\"%s\")\n" "$OPTARG"
exit 1;;
esac
done
shift $((OPTIND-1))
cd "$(dirname "$0")"
if (( $# == 0 ))
then packages=()
for path in *
do case "$path" in
r ) true;;
* ) if [[ -d $path ]]
then packages+=( "$path" )
fi;;
esac
done
else packages=( "$@" )
fi
for path in "${packages[@]}"
do verbose_run stow $verbose_mode_flag $stow_mode_flag --no-folding "$path"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment