#!/usr/bin/env bash
CASTUSER=centovacast

IAM=`whoami`
[ "$IAM" != "root" ] && echo "Must be run as root" && exit 1

echo "Centova Cast Permissions Fixer"
echo "==============================================================================="

echo "Please enter the path to your Centova Cast web interface files.  Examples:"
echo ""
echo "- Under CPanel, this might be something like /home/user/public_html/ or"
echo "  /home/user/public_html/cast/"
echo "- Under Plesk, this might be something like /var/www/vhosts/domain.com/httpdocs/"
echo "  or /var/www/vhosts/domain.com/httpdocs/cast/"
echo "- Under DirectAdmin, it might be /home/user/domains/domain.com/public_html or"
echo "  /home/user/domains/domain.com/public_html/cast"
echo "- With no control panel, this might be something like /var/www/html or"
echo "  /var/www/html/cast"
echo ""
echo -n "Enter path to web interface files: "
read WEBROOTPATH
[ -z "$WEBROOTPATH" ] && echo "Aborted" && exit 1

[ ! -d "$WEBROOTPATH" ] && echo "The path $WEBROOTPATH does not exist" && exit 1
[ ! -f "$WEBROOTPATH/class_APIClient.php" ] && echo "The path $WEBROOTPATH does not contain the Centova Cast web interface files" && exit 1
[ ! -f "$WEBROOTPATH/config.php" ] && echo "The path $WEBROOTPATH does not contain a configured copy of Centova Cast" && exit 1

SYSPATH=`cat $WEBROOTPATH/config.php | grep '^define(.SYSTEM_PATH' | awk -F "'" '{print $4}'`

if [ ! -d "$SYSPATH" ]; then
	cat <<EOF
The web interface in $WEBROOTPATH indicates that Centova Cast's system
files are installed in $SYSPATH, however that path is not found.
EOF
	exit 1
fi

if [ ! -f "${SYSPATH}ccmanage.php" ]; then
	cat <<EOF
The web interface in $WEBROOTPATH indicates that Centova Cast's system
files are installed in $SYSPATH, however that path does not appear to
contain the Centova Cast system files.
EOF
	exit 1
fi

echo ""

echo "Working, please wait (this could take a few minutes, especially if you have a"
echo "lot of accounts and/or a lot of media files) ..."

COREPATH="${SYSPATH}../"

chmod -R 0755 $SYSPATH
chmod -R 0755 ${COREPATH}ioncube
chmod 0711 ${COREPATH}vhosts/*
chmod 0700 ${COREPATH}vhosts/*/etc
chmod 0711 ${COREPATH}vhosts/*/var/log
chmod 0700 ${COREPATH}vhosts/*/var/run
chmod 0600 ${COREPATH}vhosts/*/etc/*
chmod 0600 ${COREPATH}vhosts/*/var/log/*
chmod 0755 ${COREPATH}vhosts/*/var/log/reports >/dev/null 2>&1
chmod 0644 ${COREPATH}vhosts/*/var/log/reports/* >/dev/null 2>&1
chmod 0600 ${COREPATH}vhosts/*/var/run/*
chmod -R 0755 $WEBROOTPATH
chown -R ${CASTUSER}.${CASTUSER} $SYSPATH
chown -R ${CASTUSER}.${CASTUSER} ${COREPATH}vhosts

cd "${SYSPATH}runascc"
rm -f runascc spawn castd
gcc -o runascc runascc.c
[ $? -gt 0 ] && echo "runascc compilation failed -- do you have gcc installed?" && exit 1

chown ${CASTUSER}.${CASTUSER} runascc
chmod u+s runascc
gcc -o spawn spawn.c
[ $? -gt 0 ] && echo "spawn compilation failed" && exit 1

gcc -o castd castd.c
[ $? -gt 0 ] && echo "castd compilation failed" && exit 1

chown ${CASTUSER}.${CASTUSER} castd
chmod u+s castd

echo "Complete"
