96 lines
2.3 KiB
Bash
Executable file
96 lines
2.3 KiB
Bash
Executable file
#!/bin/sh
|
||
|
||
## DEFINITIONS
|
||
name="$(basename "$0")"
|
||
home="$(find "$INSTALEE_HOME" "${XDG_CONFIG_HOME:-$HOME/.config}/$name" "$HOME/.$name" "$PWD" -maxdepth 0 2>/dev/null | head -1)"
|
||
|
||
highlight() { echo "[1m$1[0m"; }
|
||
destress() { echo "[2m$1[0m"; }
|
||
|
||
# Get the content of a file or the output of its execution
|
||
getcontent() { test -x "$1" && "$1" || cat "$1"; }
|
||
|
||
# Get available package entries for given package
|
||
getentries() {
|
||
cat "$home/handlers.available" | while read handler
|
||
do find "$home/packages/$1" -name "$handler*" 2>/dev/null
|
||
done
|
||
}
|
||
|
||
# Install a package by name
|
||
installpkg() {
|
||
test "$1" = "--quiet" && local quiet=true && shift
|
||
local pkgs=$(getentries "$1")
|
||
if test -z "$pkgs"; then
|
||
local handlers=$(find "$home/packages/$1" -type f -printf "%f\n" 2>/dev/null)
|
||
for handler in $handlers
|
||
do installpkg --quiet "$handler" &&
|
||
echo "$handler" >> "$home/handlers.available" &&
|
||
installpkg "$1" &&
|
||
return $?
|
||
done
|
||
test "$quiet" ||
|
||
echo "No handler available for package '$1'" >&2
|
||
return 2
|
||
fi
|
||
for pkg in $pkgs
|
||
do highlight "$pkg"
|
||
local name="$(basename $pkg)"
|
||
local base="${name%_*}"
|
||
|
||
local ext="${name#$base}"
|
||
local handler="$home/handlers/$base"
|
||
if test -d "$handler"
|
||
then
|
||
local args="$(getcontent "$pkg" | grep . || echo "$1")"
|
||
local install="$(find "$handler" -name "install$ext" | head -1)" &&
|
||
destress " $install $args" &&
|
||
$install $args
|
||
else
|
||
destress " running unhandled $pkg"
|
||
$pkg
|
||
fi
|
||
break
|
||
done
|
||
}
|
||
|
||
## EXECUTION
|
||
chmod +rx "$home/handlers" -R
|
||
|
||
case "$1" in
|
||
(-V|--version|"")
|
||
printf "$0 r%s.%s\n" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
|
||
echo "Try '$0 --help' or 'man $name' for usage info"
|
||
# TODO license https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion
|
||
exit 0
|
||
;;
|
||
(-h|--help)
|
||
man $name 2>/dev/null || man "$home/$name.1"
|
||
exit $?
|
||
;;
|
||
(-a|--add)
|
||
dir="$home/packages/$2"
|
||
mkdir -p "$dir"
|
||
test $# -gt 3 && echo "$4">"$dir/$3" || $EDITOR "$dir/$3"
|
||
exit $?
|
||
;;
|
||
(-v|--verbose)
|
||
set -x
|
||
shift
|
||
;;
|
||
esac
|
||
|
||
exitcode=0
|
||
while test $# -gt 0; do
|
||
groupfile="$home/groups/$1"
|
||
if test -f "$groupfile"
|
||
then for pkg in $(getcontent "$groupfile")
|
||
do installpkg "$pkg" ||
|
||
"$home/handlers/$(head -1 handlers.available)/install" "$pkg"
|
||
done
|
||
else installpkg "$1"
|
||
exitcode=$(expr $exitcode \| $?)
|
||
fi
|
||
shift
|
||
done
|
||
exit $exitcode
|