bsdnix/roles/pub/bin/group.sh

78 lines
1.4 KiB
Bash
Raw Normal View History

2024-12-09 19:03:48 +01:00
#!/bin/sh
rootdir=""
group=""
2024-12-16 14:23:45 +01:00
id=""
2024-12-09 19:03:48 +01:00
action=""
usage() {
echo "Usage: $0 -g group -a action"
echo "Valid actions: present, absent"
exit 1
}
run() {
echo $*
$*
}
OPTIND=1
2024-12-16 14:23:45 +01:00
while getopts d:g:a:i: opt ; do
2024-12-09 19:03:48 +01:00
case $opt in
d)
rootdir="$OPTARG"
;;
g)
group="$OPTARG"
;;
2024-12-16 14:23:45 +01:00
i)
id="$OPTARG"
;;
2024-12-09 19:03:48 +01:00
a)
action="$OPTARG"
;;
*)
usage
;;
esac
done
shift $(($OPTIND - 1))
if test -z "$group" -o -z "$action"; then
usage
fi
2024-12-16 14:23:45 +01:00
# we do it once for $rootdir and once on the host to have synchronous groups
for root in "$rootdir" ""; do
args=""
2024-12-09 19:03:48 +01:00
2024-12-16 14:23:45 +01:00
if test -n "$root"; then
root="-R $root"
fi
if test -n "$id"; then
args="-g $id"
fi
2024-12-09 19:03:48 +01:00
2024-12-16 14:23:45 +01:00
case "$action" in
present)
if pw $root group show "$group" > /dev/null 2>&1; then
if pw $root group show "$group" | grep -q LOCKED; then
echo "$group exists."
fi
else
run pw $root group add "$group" $args
2024-12-09 19:03:48 +01:00
fi
2024-12-16 14:23:45 +01:00
;;
absent)
if pw $root group show "$group" > /dev/null 2>&1; then
run pw $root group del "$group"
fi
;;
*)
usage
;;
esac
done