instalee/instalee

101 lines
2.4 KiB
Text
Raw Normal View History

2020-06-12 21:33:58 +02:00
#!/bin/sh
## DEFINITIONS
2022-07-04 13:51:24 +02:00
name="$(basename "$0")"
home="$(find "$INSTALEE_HOME" "${XDG_CONFIG_HOME:-$HOME/.config}/$name" "$HOME/.$name" "$PWD" -maxdepth 0 2>/dev/null | head -1)"
2020-06-12 21:33:58 +02:00
highlight() { echo "$1"; }
destress() { echo "$1"; }
2020-06-12 21:33:58 +02:00
# 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
2022-07-04 21:39:34 +02:00
getentries() {
2022-07-04 13:51:24 +02:00
cat "$home/handlers.available" | while read handler
do find "$home/packages/$1/" -type f -name "$handler*" 2>/dev/null
2022-07-04 13:51:24 +02:00
done
2020-06-12 21:33:58 +02:00
}
2022-07-04 19:13:34 +02:00
# Install a package by name
installpkg() {
2022-07-04 21:39:34 +02:00
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)
2022-07-04 21:39:34 +02:00
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
2022-07-04 19:13:34 +02:00
return 2
fi
2022-07-04 21:39:34 +02:00
for pkg in $pkgs
do highlight "$pkg"
local name="$(basename $pkg)"
local base="${name%_*}"
2022-07-04 19:13:34 +02:00
2022-07-04 21:39:34 +02:00
local ext="${name#$base}"
local handler="$home/handlers/$base"
2022-07-04 19:13:34 +02:00
if test -d "$handler"
then
2022-07-04 21:39:34 +02:00
local args="$(getcontent "$pkg" | grep . || echo "$1")"
local install="$(find "$handler" -name "install$ext" | head -1)" &&
2022-07-04 19:13:34 +02:00
destress " $install $args" &&
$install $args
else
destress " running unhandled $pkg"
$pkg
fi
break
done
}
## EXECUTION
chmod +rx "$home/handlers" -R
case "$1" in
2022-07-04 19:13:34 +02:00
(-V|--version|"")
2022-07-04 13:51:24 +02:00
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
;;
2022-07-04 19:13:34 +02:00
(-h|--help)
man $name 2>/dev/null || man "$home/$name.1"
exit $?
;;
(-a|--add)
2020-06-12 21:33:58 +02:00
dir="$home/packages/$2"
mkdir -p "$dir"
2020-06-21 16:19:54 +02:00
test $# -gt 3 && echo "$4">"$dir/$3" || $EDITOR "$dir/$3"
exit $?
2020-06-21 16:19:54 +02:00
;;
(-v|--verbose)
set -x
shift
2020-06-21 16:19:54 +02:00
;;
2020-06-12 21:33:58 +02:00
esac
2022-07-04 23:09:58 +02:00
runinstalee() {
2022-07-04 19:13:34 +02:00
groupfile="$home/groups/$1"
if test -f "$groupfile"
then for pkg in $(getcontent "$groupfile")
2022-07-04 23:09:58 +02:00
do runinstalee "$pkg" ||
"$home/handlers/$(head -1 handlers.available)/install" "$pkg"
done
2022-07-04 19:13:34 +02:00
else installpkg "$1"
fi
2022-07-04 23:09:58 +02:00
}
exitcode=0
while test $# -gt 0; do
runinstalee "$1"
exitcode=$(expr $exitcode \| $?)
shift
done
2022-07-04 21:39:34 +02:00
exit $exitcode