#!/bin/sh
#
# chmod-info - utility to calculate values for chmod

L1HD="CHMOD:  "
L2HD="Rights  -  User Group Other"
L3HD="READ         "
L4HD="WRITE        "
L5HD="EXEC         "
L6HD="Misc - Set UID:    "
L7HD="       Set GID:    "
L8HD="       Set Sticky: "
SPC="     "

UVAL=0
GVAL=0
OVAL=0
SVAL=0

UR="_"
UW="_"
UX="_"
GR="_"
GW="_"
GX="_"
OR="_"
OW="_"
OX="_"
MU="_"
MG="_"
MS="_"

URx=0
UWx=0
UXx=0
GRx=0
GWx=0
GXx=0
ORx=0
OWx=0
OXx=0
MUx=0
MGx=0
MSx=0
INPUT=" "

# These control the input keys
URKey="7"
UWKey="4"
UXKey="1"
GRKey="8"
GWKey="5"
GXKey="2"
ORKey="9"
OWKey="6"
OXKey="3"
MUKey="u"
MGKey="g"
MSKey="s"

# Display help
help() {
clear
cat << EOF
Enter the letters / number indicated in the grid
to toggle the value of that property.

${L1HD}_${SPC}_${SPC}_${SPC}_

${L2HD}
${L3HD}${URKey}${SPC}${GRKey}${SPC}${ORKey}
${L4HD}${UWKey}${SPC}${GWKey}${SPC}${OWKey}
${L5HD}${UXKey}${SPC}${GXKey}${SPC}${OXKey}

${L6HD}${MUKey}
${L7HD}${MGKey}
${L8HD}${MSKey}

Enter to continue...
EOF
}

# Refresh screen
ref_screen() {
clear
cat << EOF
${L1HD}${SVAL}${SPC}${UVAL}${SPC}${GVAL}${SPC}${OVAL}

${L2HD}
${L3HD}${UR}${SPC}${GR}${SPC}${OR}
${L4HD}${UW}${SPC}${GW}${SPC}${OW}
${L5HD}${UX}${SPC}${GX}${SPC}${OX}

${L6HD}${MU}
${L7HD}${MG}
${L8HD}${MS}

Enter 'h' for help
Enter 'x' to Exit
EOF
}

ref_screen
read INPUT
while ! [ $INPUT = "x" ]; do
	case $INPUT in
		$URKey) if [ $UR = "_" ]; then UR="X"; URx=4; else UR="_"; URx=0; fi ;;
		$UWKey) if [ $UW = "_" ]; then UW="X"; UWx=2; else UW="_"; UWx=0; fi ;;
		$UXKey) if [ $UX = "_" ]; then UX="X"; UXx=1; else UX="_"; UXx=0; fi ;;
		$GRKey) if [ $GR = "_" ]; then GR="X"; GRx=4; else GR="_"; GRx=0; fi ;;
		$GWKey) if [ $GW = "_" ]; then GW="X"; GWx=2; else GW="_"; GWx=0; fi ;;
		$GXKey) if [ $GX = "_" ]; then GX="X"; GXx=1; else GX="_"; GXx=0; fi ;;
		$ORKey) if [ $OR = "_" ]; then OR="X"; ORx=4; else OR="_"; ORx=0; fi ;;
		$OWKey) if [ $OW = "_" ]; then OW="X"; OWx=2; else OW="_"; OWx=0; fi ;;
		$OXKey) if [ $OX = "_" ]; then OX="X"; OXx=1; else OX="_"; OXx=0; fi ;;
		$MUKey) if [ $MU = "_" ]; then MU="X"; MUx=4; else MU="_"; MUx=0; fi ;;
		$MGKey) if [ $MG = "_" ]; then MG="X"; MGx=2; else MG="_"; MGx=0; fi ;;
		$MSKey) if [ $MS = "_" ]; then MS="X"; MSx=1; else MS="_"; MSx=0; fi ;;
		"h" | "H") help; read INPUT ;;
		*) echo "Invalid selection."
	esac

	UVAL=$((${URx}+${UWx}+${UXx}))
	GVAL=$((${GRx}+${GWx}+${GXx}))
	OVAL=$((${ORx}+${OWx}+${OXx}))
	SVAL=$((${MUx}+${MGx}+${MSx}))
	
ref_screen
read INPUT
done

ref_screen
