if [ $UID == 0 ]; then
echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
- echo 1>&2 '***' call "$DAISY_CHAIN $@" directly.
+ echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
exit 1
fi
# Save the original cmdline as we're gonna mess with it
cmdline="$@"
-reportmsg="*** chmod $cmdline"
-report=0
-
# Remove any options so $1 becomes the perm arg, however save the
# options for later
opts=""
# $1 should now be the perm arg
# Octal or symbolic?
-if [ $1 -ge 0 2>/dev/null ]; then
+printf '%d' $1 &>/dev/null
+if [ $? -eq 0 ]; then
perm=$1
else
touch /tmp/hack-o-matic-4500
- chmod $1 /tmp/hack-o-matic-4500
+ $DAISY_CHAIN $1 /tmp/hack-o-matic-4500
perm=$(stat --printf "%a" /tmp/hack-o-matic-4500)
rm -f /tmp/hack-o-matic-4500
fi
if [ $perm -gt 999 ]; then
# Chop off the 1st digit (the set{uid,gid,sticky} bit)
perm=${perm/?/}
- echo 1>&2 "$reportmsg"
+ echo 1>&2 '***' chmod ${cmdline}
fi
# kill off $1 and replace it with our maybe sanitised $perm
shift 1; set -- $perm "$@"
-exec $DAISY_CHAIN "$opts $@" || exit $?
+exec $DAISY_CHAIN ${opts} $@ || exit $?
exit 0
# Local variables:
fi
## root has no business installing things here!!
-if [ $UID == 0 ]; then
+if [ $UID -eq 0 ]; then
echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
- echo 1>&2 '***' call '"'$DAISY_CHAIN ${pristinecmd[*]'"' directly
+ echo 1>&2 '***' call '"'$DAISY_CHAIN ${pristinecmd[*]}'"' directly
exit 1
fi
# Only allow the creation of directories that don't already exist.
_dirs()
{
- local myarray=($@)
- local dir=${myarray[-1]}
+ local dir=${pristinecmd[-1]}
if [ -d ${dir} ]; then
report
+ exit 0
else
cmdopts="$cmdopts -d"
fi
return
}
-## Leading directories (install option -D)
-# The same as for -d, only create if it doesn't exist. But we need
-# to do a little more this time because if we decide to NOT create
-# the directory we will need to alter the command line further...
-#
-# install -Dm755 myprog /usr/bin/myprog
-# To
-# install -m755 myprog /usr/bin
-_leading_dirs()
-{
- local myarray=($@)
- local dir=$(dirname ${myarray[-1]})
- local last=$((${#myarray[*]}-1))
-
- if [ -d ${dir} ]; then
- report
- myarray[${last}]=${dir}
- set --
- set -- "$@" "${myarray[*]}"
- else
- cmdopts="$cmdopts -D"
- fi
- return
-}
-
## Group
# If $group is one of the groups we belong to, use it, otherwise set
# -g to our currently active group and report it.
local GRP_LIST
# GID or name?
- if [ $group -ge 0 2>/dev/null ]; then
+ printf '%d' $group &>/dev/null
+ if [ $? -eq 0 ]; then
GRP_LIST=$(id -G)
else
GRP_LIST=$(id -Gn)
}
## Owner
-# Set -u to our username and report if it wasn't already our name.
+# Set -o to our username and report if it wasn't already our name.
_owner()
{
local MYNAME
# UID or name?
- if [ $owner -ge 0 2>/dev/null ]; then
+ printf '%d' $owner &>/dev/null
+ if [ $? -eq 0 ]; then
MYNAME=$(id -u)
else
MYNAME=$(id -un)
if [ $owner != "$MYNAME" ]; then
report
fi
- cmdopts="$cmdopts -u$(id -un)"
+ cmdopts="$cmdopts -o$(id -un)"
return
}
{
local p
### HACK-O-MATIC:
- # Convert symbolic permissions to numerical. This works via
- # side-effect because if $perm is text it causes the if to error
- # and you come out on the else branch.
- if [ $perm -ge 0 2>/dev/null ]; then
+ # Convert symbolic permissions to numerical.
+ printf '%d' $perm &>/dev/null
+ if [ $? -eq 0 ]; then
p=$perm
else
touch /tmp/hack-o-matic-9000
- chmod $perm /tmp/hack-o-matic-9000
+ # A tiny risk hard-coding /bin/chmod here, but I don't won't
+ # the chmod wrapper in play for this.
+ /bin/chmod $perm /tmp/hack-o-matic-9000
p=$(stat --printf "%a" /tmp/hack-o-matic-9000)
rm -f /tmp/hack-o-matic-9000
fi
}
## Parse the command line.
-# All we really care about here is -d, -D, -o, -g, and -m. -c is
+# All we really care about here is -d, -o, -g, and -m. -c is
# silently dropped. Unrecognised options cause the script to exit
# with a non-zero return code.
#
while getopts $args opts; do
case $opts in
(-)
- # long opts
+ ## Long options
case $OPTARG in
- (backup*) cmdopts="$cmdopts --${OPTARG}" ;;
- (compare) cmdopts="$cmdopts -C" ;;
+ # Passed through unaltered.
+ (backup*|compare|strip|suffix*|verbose|context*|version|help)
+ cmdopts="$cmdopts --${OPTARG}" ;;
+ # The options we care about.
(directory) _dirs ;;
(group*) group=${OPTARG/group[=[:space:]]/}; _group ;;
(mode*) perm=${OPTARG/mode[=[:space:]]/}; _perms ;;
(owner*) owner=${OPTARG/owner[=[:space:]]/}; _owner ;;
- (strip) cmdopts="$cmdopts -s" ;;
- (suffix*) cmdopts="$cmdopts -S${OPTARG/suffix[=[:space:]]/}" ;;
- (verbose) cmdopts="$cmdopts -v" ;;
- (context*) cmdopts="$cmdopts -Z${OPTARG/context[=[:space:]]/}" ;;
- (help) cmdopts="$cmdopts --help" ;;
- (version) cmdopts="$cmdopts --version" ;;
+ # Anything else errors out.
(*) echo 1>&2 '***' Illegal option -- ${OPTARG}
exit 1
;;
esac
;;
- # short opts
- (b) cmdopts="$cmdopts -b" ;;
- (C) cmdopts="$cmdopts -C" ;;
- (c) ;; # no-op
+ ## Short options
+ # Passed through unaltered.
+ (b|C|D|p|s|T|v|S|t|Z) cmdopts="$cmdopts -${opts}${OPTARG}" ;;
+ # Dropped.
+ (c) ;;
+ # The options we care about.
(d) _dirs ;;
- (D) _leading_dirs ;;
(g) group=${OPTARG}; _group ;;
(m) perm=${OPTARG}; _perms ;;
(o) owner=${OPTARG}; _owner ;;
- (p) cmdopts="$cmdopts -p" ;;
- (s) cmdopts="$cmdopts -s" ;;
- (S) cmdopts="$cmdopts -S${OPTARG}" ;;
- (t) cmdopts="$cmdopts -t${OPTARG}" ;;
- (T) cmdopts="$cmdopts -T" ;;
- (v) cmdopts="$cmdopts -v" ;;
- (Z) cmdopts="$cmdopts -Z${OPTARG}" ;;
+ # Illegal options.
(*) exit 1 ;;
esac
done
shift $(( $OPTIND - 1 ))
# We've done all we can, now lets run install
-$DAISY_CHAIN "$cmdopts $@" || exit $?
+$DAISY_CHAIN ${cmdopts} $@ || exit $?
exit 0
### End install