#!/bin/sh
#
#
OPTION=""
PLAT=`uname`

if [ ${1} ]; then
    OPTION=${1}
fi

if [ "$OPTION" = "-h" ]; then
    OPTION=""
fi

if [ ${OPTION} ]; then
    OK=YES
else
    echo ""
    echo "usage: createuserstreamingdir user"
    echo ""
    echo "This tool will create the directory ~user/Sites/Streaming/."
    echo "The created directory gives the QuickTimeStreamingServer access to user managed content."
    echo ""
    exit 0
fi
echo ""

CALLER=`whoami`

if [ "$1" = "$CALLER" ] ; then
    OK=YES
else
    if [ `id -u` != 0 ]
    then
        echo "You must be root, ${1}, or use the sudo command to proceed. "
        echo "Cannot continue."
        echo ""
        echo "usage: createuserstreamingdir user"
        echo ""
        exit 1
    fi
fi


#
# Home dir
#
NEWPATH=~"${1}"
HOMEDIR=`eval "echo $NEWPATH"`

echo "examining the home directory for ${NEWPATH}"
echo "home directory path = ${HOMEDIR}"
if [ -e "${HOMEDIR}" ] ; then
    OK=YES
else
    echo "The path \"${HOMEDIR}\" is not found."
    echo "Cannot continue."
    echo ""
    echo "usage: createuserstreamingdir user"
    echo ""
    exit 1
fi

if [ -d "${HOMEDIR}" ] ; then
    OK=YES
else
    echo "${HOMEDIR} is not a directory."
    echo "Cannot continue."
    echo ""
    exit 1
fi

if [ "Darwin" != "$PLAT" ]; then
    chmod 755 "${HOMEDIR}"	
    echo "Set privileges for ${HOMEDIR} to 755 "
fi

#
# /Sites
#

if [ -e "${HOMEDIR}/Sites" ]; then
    OK=YES
else
    if [ -w "${HOMEDIR}/" ]; then
        mkdir "${HOMEDIR}/Sites"
        chmod 755 "${HOMEDIR}/Sites"
        chown ${1} "${HOMEDIR}/Sites"
    fi
fi

if [ -e "${HOMEDIR}/Sites" ]; then
    OK=YES
else
    echo "You do not have privileges to create ${HOMEDIR}/Sites."
    echo "Cannot continue."
    echo ""
    exit 1
fi

if [ -d "${HOMEDIR}/Sites" ]; then
    OK=YES
else
    echo "${HOMEDIR}/Sites is not a directory."
    echo "Cannot continue."
    echo ""
    exit 1
fi

#
# /Sites/Streaming
#

if [ -e "${HOMEDIR}/Sites/Streaming" ]; then
    OK=YES
else
    if [ -w "${HOMEDIR}/Sites" ]; then
        mkdir -m 755 "${HOMEDIR}/Sites/Streaming"
        chown ${1}:qtss "${HOMEDIR}/Sites/Streaming"
    fi
fi

if [ -e "${HOMEDIR}/Sites/Streaming" ]; then
    OK=YES
else
    echo "You do not have privileges to create ${HOMEDIR}/Sites/Streaming."
    echo "Cannot continue."
    echo ""
    exit 1
fi

if [ -d "${HOMEDIR}/Sites/Streaming" ]; then
    OK=YES
else
    echo "${HOMEDIR}/Sites/Streaming is not a directory."
    echo "Cannot continue."
    echo ""
    exit 1
fi

#
# Test access
#

if [ -w "${HOMEDIR}/Sites/Streaming/" ]; then
    OK=YES
else
    echo "You do not have privileges to modify ${HOMEDIR}/Sites/Streaming."
    echo "Cannot continue."
    echo ""
    exit 1
fi

chown ${1}:qtss "${HOMEDIR}/Sites/Streaming"  > /dev/null 2>&1
if [ $? = 0 ]; then
    OK=YES
else
    echo "You are not the owner."
    echo "You may need to run this tool again as root or use the sudo command."
    echo ""
    exit 1
fi

chmod 755 "${HOMEDIR}/Sites/Streaming" > /dev/null 2>&1
if [ $? = 0 ]; then
    OK=YES
else
    echo "The permissions are not correct."
    echo "You may need to run this tool again as root or use the sudo command."
    echo ""
    exit 1
fi


echo "${HOMEDIR}/Sites/Streaming is ready for streaming."
echo ""

exit 0