dotfiles/dot
2024-06-04 14:43:05 +02:00

89 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
ask=0
is_function() {
type "$1" 2>/dev/null | sed "s/$1//" | grep -qwi function
}
curr_distro() {
cat /etc/os-release | grep -G "^ID=" | sed 's/ID=//'
}
include() {
[ -f "$1" ] || return 1
. $1
return 0
}
func() {
if is_function $(echo "super_$2"); then
[ "${DO_SUDO:-yes}" = "yes" ] || return 0
ecmd=". $1 && super_$2"
[ "$(id -u)" = "0" ] && sh -c "$ecmd" || sudo sh -c "$ecmd"
unset ecmd
fi
is_function $2 && $2
}
# only run this *in crate dir*
run_crate() {
enabled=1
include ./crate.sh || exit 1
if [ -n "$describe" ]; then
echo "desc($(basename $PWD)): $describe"
fi
if [ $enabled -ne 1 ]; then
return
fi
cmd=$1
scripts=${scripts:-"@self @distro"}
for s in $scripts; do
echo "exec($(basename $PWD)): $s/$cmd"
case $s in
@self)
func ./crate.sh $cmd
;;
@distro)
unset -f super_$cmd
unset -f $cmd
include ./crate.$(curr_distro).sh
func ./crate.$(curr_distro).sh $cmd
unset -f super_$cmd
unset -f $cmd
include ./crate.sh
;;
*)
sh $s
;;
esac
done
}
echo "# details:"
echo "# user: $USER (${UID:-$(id -u)})"
echo "# groups: $(groups)"
echo "# distro: $(curr_distro)"
cmd=$1
if [ -z "$cmd" ]; then
echo "usage: $0 <command> [...args]"
exit 1
else
shift
fi
for c in ${@:-$(ls crates)}; do
[ -d crates/*$c ] || exit 1
cd crates/*$c
case $cmd in
a | apply)
run_crate apply
;;
u | undo)
run_crate undo
;;
*) exit 1 ;;
esac
cd ../..
done