#!/bin/sh -

# xgrp - run a recursive, logging, backgrounded, nohupped group owner switch
# Steve Kinzler, steve@kinzler.com, Jul 24
# https://kinzler.com/me/home.html#unix

sudo=
case "$1" in
-s)	sudo=sudo; shift;;
esac

case "$#" in
4)	;;
*)	cat 1>&2 <<EOF
usage: $0 [ -s ] name oldgroup newgroup path
	-s	run chown under sudo
name is used just for part of the output filenames.
Output files are placed in \$XGRP_DIR else \$HOME,
and a work subdirectory within if it exists.
EOF
	exit;;
esac

dir="${XGRP_DIR:-$HOME}"; test -d "$dir/work" && dir="$dir/work"

set -x
nohup stdbuf -oL $sudo chown -v -h -R --from=:"$2" :"$3" "$4" \
	> "$dir/xgrp_$1_$$.out" 2> "$dir/xgrp_$1_$$.err" &
