#!/bin/sh
# chmod_random
#
################################################################
# Copyright (C) 2003 Robert Anderson and Tom Lord
#
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
#


#set -x

# u,g,o
range=3
mult=$(( 32767 / $range ))
who_num=$(( $RANDOM / $mult + 1 ))


case $who_num in

    1)
        who="u"
        ;;
    2)
        who="g"
        ;;
    3)
        who="o"
        ;;

esac

# +,-
range=2
mult=$(( 32767 / $range ))
sign_num=$(( $RANDOM / $mult + 1 ))

if [ $sign_num -eq 1 ]; then
    sign="+"
else
    sign="-"
fi

# r,w,x,s,X,t
range=3
mult=$(( 32767 / $range ))
what_num=$(( $RANDOM / $mult + 1 ))

case $what_num in

    1)
        what="r"
        ;;
    2)
        what="w"
        ;;
    3)
        what="x"
        ;;
    4)
        what="s"
        ;;
    5)
        what="X"
        ;;
    6)
        what="t"
        ;;
esac

# Constraints

if [ "$who$sign$what" == "u-r" ]; then
    exit 0;
fi

# Do it

chmod $who$sign$what $1

# tag: Robert Anderson Sun May 18 18:21:48 PDT 2003 (chmod_random)

