Fix a bug that was exposed by the SXEmacs configure script.
[pkgusr] / usr / lib / pkgusr / chgrp
1 #!/bin/bash
2 # Original...
3 # Copyright (c) 2000,2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
6
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 #  Handle chgrp options and chgrp'ing outside the user's group list.
9
10 DAISY_CHAIN=""
11
12 for p in $(type -ap chgrp) ; do
13     if [ ! $p -ef $0 ]; then
14         DAISY_CHAIN=$p
15         break
16     fi
17 done
18
19 if [ ! -n "$DAISY_CHAIN" ]; then
20     echo 1>&2 '***' Cannot find real ${0##*/} command 
21     exit 1
22 fi
23
24 if [ $(id -u) == 0 ]; then
25     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
26     echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly
27     exit 1
28 fi
29
30 # Preserve the command line because we're about to mess with it.
31 cmdline=$@
32
33 # Remove any options so $1 becomes the group name.
34 while [ -n "$1" ]; do
35     case $1 in
36         (-*) shift ;;
37         (*) break ;;
38     esac
39 done
40
41 # If you're not root you can only chgrp to groups you are in, so lets
42 # find out.
43 GRP_CHAIN=""
44 # name or GID?
45 printf '%d' "$1" &>/dev/null
46 if [ $? -eq 0 ]; then
47     GRP_LIST=$(id -G)
48 else
49     GRP_LIST=$(id -Gn)
50 fi
51 for g in ${GRP_LIST}; do
52     if [ "$1" == "$g" ]; then
53         GRP_CHAIN=$g
54         break
55     fi
56 done
57
58 if [ -z "$GRP_CHAIN" ]; then
59     echo 1>&2 '***' chgrp ${cmdline}
60 else
61     exec $DAISY_CHAIN ${cmdline} || exit $?
62 fi
63
64 exit 0
65
66 # Local variables:
67 # sh-basic-offset: 4
68 # End: