
#!/bin/sh
# File	: upd_rcg-meter.sh
# By	: MaartenDeBoer.nl, 201231, 210920, 230220
# Subject	: Script to create the RCG-meter-page
# (from upd_zwerfvuil.sh)
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
BASEDIR="${HOME}/rcg-meter"
LOG="${BASEDIR}/log/${PGM}.log"
DATI=`date +%Y-%m-%d-%H-%M-%S`
RCFILE="${BASEDIR}/etc/${PGM}.rc"
ADDR="${BASEDIR}/etc/${PGM}.addr"
#IMG_FILE="zwerfvuilophaalthermometer.png"
IMG_FILE="rcg-meter.png"
HDR_IMG="https://rcg.agrarix.nl/wp-content/uploads/2022/11/cropped-RCG_221102b.png"

SITE="rcg-meter.agrarix.it"
PAGEDIR="/var/www/vhosts/${SITE}/html"
PREV_PAGE="${PAGEDIR}/index_${DATI}.html"
PAGE="${PAGEDIR}/index.html"
TABLE_BORDER=0

BODY_BGC=""
FFACE="Verdana"
#TITLE="TEST-page | By Agrarix-IT"
TITLE="RCG-meter"
#ICONFILE="Agrarix-Pingu_2017.jpg"
ICONFILE="https://radioclubgooi.nl/wp-content/uploads/2022/11/cropped-RCG-icon-1-192x192.png"
URL="https://www.radioclubgooi.nl"
#
MAX=100
STEPS=5
CUR=1
FROM=""

#H=25
H=20
#W=100
W=300
W1=`expr ${W} / 66`
W2=`expr ${W} / 33`

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options              :"
  echo "    -c | --cur         : Current # streets"
  echo "    -f | --from        : From-address"
  echo "    -h | --help        : this help"
  echo "    -V                 : Version"
  echo "    -x                 : set -x"
}
while [ ${#} -ge 1 ]
  do
  case ${1} in
    -c | --cur) CUR=${2}; shift ;;
    -f | --from) FROM=${2}; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
if [ ! -f ${PAGE} ]; then
  echo "  NO HTML-page (${PAGE}) found. Exiting ..."|tee -a ${LOG}
  exit 3
fi
if [ -f ${RCFILE} ]; then
  echo "  RCFILE (${RCFILE}) found. Using ..."|tee -a ${LOG}
  . ${RCFILE}
fi

echo "  CUR=${CUR}"|tee -a ${LOG}
echo "  FROM=${FROM}"|tee -a ${LOG}
sleep 1

if [ "${FROM}" != "" ]; then
# Check is email-addr is OK
ANSW=`grep ${FROM} ${ADDR}`
  if [ "${ANSW}" = "" ]; then
    echo "  FROMaddress (${FROM}) NOT known. Exiting..."|tee -a ${LOG}
    exit 4
  fi  # ANSW
fi  # ${FROM}" != ""

touch ${TMP}
# Web-page
echo "<!DOCTYPE HTML>" > ${TMP}
echo "<HTML>" >> ${TMP}
echo "  <HEAD>" >> ${TMP}
echo "    <meta http-equiv='Content-Language' content='en-us'>" >> ${TMP}
echo "    <meta http-equiv='Content-Type' content='text/html; charset=windows-1252'> " >> ${TMP}
echo "    <meta name='author' content=\"MaartenDeBoer.nl - Agrarix.IT\"> " >> ${TMP}
echo "    <meta name='generator' content=\"Agrarix-IT: ${PGM} v${VER}\"> " >> ${TMP}
echo "    <meta name='up-time' content=\"${DATI}\"> " >> ${TMP}
echo "    <TITLE>${TITLE}</TITLE>">> ${TMP}
echo "    <link rel=\"icon\" href=\"${ICONFILE}\" type=\"image/jpg\"> " >> ${TMP}
echo "  </HEAD>" >> ${TMP}

echo "  <BODY bgcolor=${BODY_BGC}>" >> ${TMP}

echo "    <TABLE class='table1-class' border=${TABLE_BORDER} id='table1' cellspacing='0'>" >> ${TMP}

BGC1=lightgrey
BGC2=lightgrey
echo "      <TR>" >> ${TMP}
echo "        <TD BGCOLOR=${BGC1} WIDTH=${W} colspan=2>" >> ${TMP}
echo "          <A HREF=\"${URL}\"> " >> ${TMP}
echo "            <IMG SRC=${HDR_IMG} WIDTH=100%>" >> ${TMP}
echo "          </A> " >> ${TMP}
echo "          <CENTER> <FONT face=verdana size=4>DONATIE meter. <BR> Staat op <B>${CUR}</B> %</FONT> </CENTER>" >> ${TMP}
echo "        </TD>" >> ${TMP}
echo "      </TR>" >> ${TMP}


PCT=100
# From 100% downto 0%
while [ ${PCT} -gt 0 ]
do
  echo "      <TR>" >> ${TMP}
#  BGC1=blue
  CUR_PCT=`expr ${CUR} \* 100 / ${MAX}`
  NUM=`expr ${PCT} \* ${MAX} / 100`
  if [ ${PCT} -ge 100 ]; then
    BGC1="green"
  fi
  if [ ${PCT} -le 95 ]; then
    BGC1="lightgreen"
  fi
  if [ ${PCT} -le 90 ]; then
    BGC1="yellow"
  fi
  if [ ${PCT} -le 70 ]; then
    BGC1="orange"
  fi
  if [ ${PCT} -le 35 ]; then
    BGC1="red"
  fi
  if [ ${PCT} -le ${CUR_PCT} ]; then
    BGC2="blue"
  fi
echo "  PCT=${PCT} CUR_PCT=${CUR_PCT} NUM=${NUM} BGC1=${BGC1} BGC2=${BGC2}"

  echo "        <TD BGCOLOR=${BGC1} HEIGHT=${H} WIDTH=${W1} >" >> ${TMP}
#  echo "          <CENTER> <FONT face=verdana size=3> ${NUM} = ${PCT}% </FONT> </CENTER>" >> ${TMP}
  echo "          <CENTER> <FONT face=verdana size=5> ${PCT}% </FONT> </CENTER>" >> ${TMP}
  echo "        </TD>" >> ${TMP}

  echo "        <TD BGCOLOR=${BGC2} HEIGHT=${H} WIDTH=${W2} >" >> ${TMP}
  echo "        </TD>" >> ${TMP}

  echo "      </TR>" >> ${TMP}
  PCT=`expr ${PCT} - ${STEPS}`
done  # PCT

echo "      </TR>" >> ${TMP}

echo "    </TABLE>" >> ${TMP}

#echo "    <HR>" >> ${TMP}
#echo "    <div align='center'> <font size=1 color=grey face=${FFACE}>`date` by ${PGM} v${VER} </font>" >> ${TMP}

echo "  </BODY>" >> ${TMP}
echo "</HTML>" >> ${TMP}


cp ${PAGE} ${PREV_PAGE}
echo "  Copied ${PAGE} to ${PREV_PAGE}"|tee -a ${LOG}
cp ${TMP} ${PAGE}
echo "  Copied ${TMP} to ${PAGE}"|tee -a ${LOG}

if [ "${IMG_FILE}" != "" ]; then
  /usr/bin/xvfb-run --server-args="-screen 0, 1280x1280x24" /usr/bin/cutycapt --url=http://${SITE} --out=${PAGEDIR}/${IMG_FILE} --min-width=${W}
  echo "  cutycapt ${SITE} to ${IMG_FILE}"|tee -a ${LOG}
fi

if [ "${FROM}" != "" ]; then
  echo "`date` updated ${SITE} and file ${IMG_FILE} (http://${SITE}/${IMG_FILE}) "|mailx -s "${PGM} v${VER}" ${FROM}
  echo "  Mailed ${FROM}"|tee -a ${LOG}
fi

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

