#!/bin/bash
#Wrapper script to manage USERNAME accounts + web security
# myth_USERNAME_all -c add -u USERNAME
# myth_USERNAME_all -c delete -u USERNAME
# myth_USERNAME_all -c pass  -u USERNAME -p pass
# myth_USERNAME_all -c web  -u USERNAME -p pass

INIT_CHECK=TRUE
FULL_CALL="$@"
function CHROOT_CHECK {
    INIT=`ps -p 1 -o comm=`
    if [ x$INIT = xrunit ]
    then
       CHROOT_NEEDED=FALSE
    else
       CHROOT_NEEDED=TRUE
    fi
}

function store_commands () {
    echo "$FULL_CALL" >> /root/myth_user_call.out
    chmod 600 /root/myth_user_call.out
}


function add_user() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to add user"
        store_commands
    else
        echo "adding user $USERNAME"
	useradd -m -s /bin/bash $USERNAME -G audio,video,optical,storage,users
    usermod -a -G mythtv $USERNAME

    fi

}


function del_user() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to delete user"
        store_commands
    else
        echo "removing  user $USERNAME"
        userdel $USERNAME
    fi

}


function pass_change() {
    if [ $CHROOT_NEEDED = TRUE ]
    then
        echo "calling myth_call_user in chroot to change password"
        store_commands
    else
        echo "changing password for $USERNAME"
        echo $USERNAME:$PASSWORD | chpasswd
    fi

}


function web_security {

    grep -q ${USERNAME}: /etc/lighttpd/lighttpd.user
    if [ $? = 0 ]
    then
            #delete user
            sed -i "/${USERNAME}\:/d"  /etc/lighttpd/lighttpd.user
    fi
    echo "${USERNAME}:${PASSWORD}" >> /etc/lighttpd/lighttpd.user

}

function ARG_ERR()  {
        if [  x$OPTARG = "x" ]
        then
            echo "$SWITCH NEEDS AND ARG"
            exit 11
        fi
}


function print_help {

    echo "Valid options are:"
    echo "  -c (add|delete|pass|web)"
    echo "  -u  USERNAMEname"
    echo "  -p  password"
    exit 1
}

if [ $# -eq 0 ]
then
    print_help
fi

declare -r OPTSTRING="c:u:p:i"
while getopts "$OPTSTRING"  SWITCH
do
    case $SWITCH in

            c)  ARG_ERR
                OPERATION=$OPTARG
                ;;
            u)  ARG_ERR
                USERNAME=$OPTARG
                ;;
            p)  ARG_ERR
                PASSWORD=$OPTARG
                ;;
            i)  INIT_CHECK=FALSE

    esac
done

if [ $INIT_CHECK = TRUE ]
then
    CHROOT_CHECK
else
    CHROOT_NEEDED=FALSE
fi

case $OPERATION in
    add)
            if [ x$USERNAME = x ]
            then
                print_help
            fi
            add_user
        ;;
    delete)
            if [ x$USERNAME = x ]
            then
                print_help
            fi
            del_user
        ;;
    pass)
            if [ x$USERNAME = x ]
            then
                print_help
            fi

            if [ x$PASSWORD = x ]
            then
                print_help
            fi
            pass_change
        ;;

    web)
            if [ x$USERNAME = x ]
            then
                print_help
            fi


            if [ x$PASSWORD = x ]
            then
                print_help
            fi
            echo "adding webUSERNAME $USERNAME with pass $PASSWORD"
	    web_security
            ;;
    *)  print_help
        ;;
esac