
#!/bin/sh
# File	: mk_domain-page.sh
# By	: MaartenDeBoer.nl, 210616
# Subject	: Script to setup (MaKa) a domain-page
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
#TMP="/tmp/${PGM}"
LOG="${HOME}/log/${PGM}.log"

DOMAIN="domain-page.agrarix.it"
WEBCONFIG="/etc/apache2/sites-available/agrarix-it.conf"

echo "`date` ${PGM} ${VER} started."|tee -a ${LOG}
touch ${TMP}
if [ "${1}" != "" ]; then
  DOMAIN="${1}"
fi
DOMAINPART=`echo ${DOMAIN}|cut -d\. -f1`
WEBDIR="/var/www/vhosts/${DOMAIN}/html"
echo "  DOMAIN=${DOMAIN}"
echo "  DOMAINPART=${DOMAINPART}"
echo "  WEBDIR=${WEBDIR}"
echo "  WEBCONFIG=${WEBCONFIG}"
sleep 1

#if [ -d ${WEBDIR} ]; then
#  echo "  DOMAIN (${DOMAIN}) already found. Exiting ..."|tee -a ${LOG}
#  exit 3
#fi
if [ ! -f ${WEBCONFIG} ]; then
  echo "  WEBCONFIG (${WEBCONFIG}) NOT found. Exiting ..."|tee -a ${LOG}
  exit 4
fi

#WEBCONFIG="/tmp/webconfig"
ANSW=`grep ${DOMAIN} ${WEBCONFIG}`
if [ "${ANSW}" = "" ]; then
  echo "  Creating ${DOMAIN} ..."
  sudo mkdir -p /var/www/vhosts/${DOMAIN}/html
  echo "<VirtualHost *:80>" >> ${TMP}
  echo "    ServerAdmin webadmin@agrarix.it" >> ${TMP}
  echo "    ServerName ${DOMAIN}" >> ${TMP}
  echo "    ServerAlias ${DOMAIN}" >> ${TMP}
  echo "    DocumentRoot /var/www/vhosts/${DOMAIN}/html" >> ${TMP}
  echo "    ErrorLog \${APACHE_LOG_DIR}/${DOMAINPART}_error.log" >> ${TMP}
  echo "    CustomLog \${APACHE_LOG_DIR}/${DOMAINPART}_access.log combined" >> ${TMP}
  echo "</VirtualHost>" >> ${TMP}
#echo "TMP=${TMP}"
  /usr/bin/sudo cat ${TMP} >> ${WEBCONFIG}
  echo "<H1> ${DOMAINPART} </H1>" > /var/www/vhosts/${DOMAIN}/html/index.html
  sudo chown -R automation:automation /var/www/vhosts/${DOMAIN}

  echo "  Reloading Apache2"
  /usr/bin/sudo service apache2 reload

fi  # ANSW}" = ""

echo "  Config's:"
ls -l /var/www/vhosts/${DOMAIN}/html/
grep -B 2 -A 3 ${DOMAIN} ${WEBCONFIG}


# rm ${TMP}
echo "`date` ${PGM} ${VER} finished."|tee -a ${LOG}
exit 0

