Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 84 lines (71 sloc) 1.75 KB
#!/usr/bin/env bash
usage() {
cat <<EOF
Install/Uninstall dotfiles
SYNOPSIS:
$PROGRAM [OPTION ...] PACKAGE ... PACKAGE
OPTIONS:
-c Use 'cp -nars' instead of GNU stow
-d DIR Install into DIR instead of $HOME
-a Install/uninstall all packages
-u Uninstall
EOF
}
install() {
local package=$1
if [ "$USECOPY" = true ]; then
shopt -s dotglob nullglob
cp -nars $(pwd)/$package/* $DESTDIR/
else
stow -t $DESTDIR $package
fi
}
uninstall() {
local package=$1
if [ "$USECOPY" = true ]; then
find $package -type f | while read f; do unlink $DESTDIR/$(expr $f : '[^/]*/\(.*\)') 2>/dev/null; done
find $package -type l | while read f; do unlink $DESTDIR/$(expr $f : '[^/]*/\(.*\)') 2>/dev/null; done
find $package -depth -prune -type d | while read d; do rmdir $DESTDIR/$(expr $d : '[^/]*/\(.*\)') 2>/dev/null; done
else
stow -t $DESTDIR -D $package
fi
}
# Defaults
PROGRAM="${0##*/}"
DESTDIR=$HOME
USECOPY=false
cmd=install
all=false
# Show usage and exit if no options given
if [ $# == 0 ]; then
usage
exit 1;
fi
# Parse options
while getopts haucd: opt; do
case $opt in
u) cmd=uninstall;;
a) all=true;;
c) USECOPY=true;;
d) DESTDIR="$OPTARG";;
h | ?) usage; exit 2;;
esac
done
shift $(($OPTIND-1))
# Create list of packages
declare -a pkgs
if [ "$all" = "true" ]; then
for p in $(ls -d */); do pkg=${p%%/}; pkgs+=($pkg); done
else
for pkg in $*; do pkgs+=($pkg); done
fi
# Perform actions
shift $((OPTIND-1))
for p in "${pkgs[@]}"; do
if [ "$cmd" = "install" ]; then
echo ${cmd}ing $p to $DESTDIR
else
echo ${cmd}ing $p from $DESTDIR
fi
$cmd $p
done