instalee/instalee
2022-07-05 12:53:53 +02:00

114 lines
2.6 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 "$1"; }
destress() { echo "$1"; }
# Get the content of a file or the output of its execution
getcontent() {
if test -x "$1"
then "$1"
else cat "$1"
fi
}
# Get available package entries for given package
getentries() {
cat "$home/handlers.available" | while read handler
do find "$home/packages/$1/" -type f -name "$handler*" 2>/dev/null
done
}
# Install a package by name
installpkg() {
test "$1" = "--quiet" && local quiet=true && shift
local pkgs=$(getentries "$1")
local pkgdir="$home/packages/$1/"
if test -z "$pkgs"; then
local handlers=$(find "$pkgdir" -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"
set -e
local name="$(basename $pkg)"
local base="${name%_*}"
for dependency in $(cat "${pkgdir}dependencies" "${pkgdir}${base}_dependencies" 2>/dev/null)
do runinstalee $dependency
done
local ext="${name#$base}"
local handler="$home/handlers/$base"
if test -d "$handler"
then
# This declaration needs to be ahead, otherwise it overrides failing exit codes
local args
local install
args="$(getcontent "$pkg")"
args="${args:-$1}"
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
runinstalee() {
groupfile="$home/groups/$1"
if test -f "$groupfile"
then for pkg in $(getcontent "$groupfile")
do runinstalee "$pkg" ||
"$home/handlers/$(head -1 handlers.available)/install" "$pkg"
done
else installpkg "$1"
fi
}
exitcode=0
while test $# -gt 0; do
runinstalee "$1"
exitcode=$(expr $exitcode \| $?)
shift
done
exit $exitcode