dotfiles/dot

90 lines
1.7 KiB
Text
Raw Normal View History

2023-10-14 22:38:12 +02:00
#!/bin/sh
ask=0
is_function() {
2024-06-04 14:43:05 +02:00
type "$1" 2>/dev/null | sed "s/$1//" | grep -qwi function
2023-10-14 22:38:12 +02:00
}
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
2023-10-14 22:38:13 +02:00
[ "${DO_SUDO:-yes}" = "yes" ] || return 0
2023-11-19 01:17:17 +01:00
ecmd=". $1 && super_$2"
[ "$(id -u)" = "0" ] && sh -c "$ecmd" || sudo sh -c "$ecmd"
unset ecmd
2023-10-14 22:38:12 +02:00
fi
2024-06-04 14:43:05 +02:00
is_function $2 && $2
2023-10-14 22:38:12 +02:00
}
# only run this *in crate dir*
run_crate() {
2023-10-14 22:38:14 +02:00
enabled=1
2023-10-14 22:38:12 +02:00
include ./crate.sh || exit 1
if [ -n "$describe" ]; then
echo "desc($(basename $PWD)): $describe"
fi
2023-10-14 22:38:14 +02:00
if [ $enabled -ne 1 ]; then
return
fi
2024-06-04 14:43:05 +02:00
cmd=$1
2023-10-14 22:38:12 +02:00
scripts=${scripts:-"@self @distro"}
2024-06-04 14:43:05 +02:00
for s in $scripts; do
2023-10-14 22:38:12 +02:00
echo "exec($(basename $PWD)): $s/$cmd"
case $s in
2024-06-04 14:43:05 +02:00
@self)
func ./crate.sh $cmd
2023-10-14 22:38:12 +02:00
;;
2024-06-04 14:43:05 +02:00
@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
2023-10-14 22:38:12 +02:00
;;
2024-06-04 14:43:05 +02:00
*)
sh $s
2023-10-14 22:38:12 +02:00
;;
esac
done
}
2023-10-14 22:38:15 +02:00
echo "# details:"
2023-11-16 15:58:35 +01:00
echo "# user: $USER (${UID:-$(id -u)})"
2023-10-14 22:38:15 +02:00
echo "# groups: $(groups)"
echo "# distro: $(curr_distro)"
2024-06-04 14:43:05 +02:00
cmd=$1
if [ -z "$cmd" ]; then
echo "usage: $0 <command> [...args]"
exit 1
else
shift
2023-10-14 22:38:12 +02:00
fi
2024-06-04 14:43:05 +02:00
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