#!/bin/bash

###
#  MRTG-XBOX installation script
#  Copyright (C) Thomas "ShALLaX" Pedley (gentoox@shallax.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###

if [ `whoami` != "root" ]; then
   echo "Sorry, you must be root to install this"
else
   clear
   echo "MRTG Installation script (c) ShALLaX"
   echo ""
   echo "Would you like to use:"
   echo ""
   echo "[c]elsius"
   echo "[f]arenheit"
   echo ""
   echo -n "Answer: "
   read -n 1 scaleuser
   clear
   echo "MRTG Installation script"
   echo ""
   echo "Would you like to use:"
   echo ""
   echo "[c]elsius"
   echo "[f]arenheit"
   echo ""
   echo -n "Answer: "
   if [ $scaleuser == "c" ]; then
      export scale="0"
      echo "celsius"
   else
      export scale="1"
      echo "farenheit"
   fi
   echo ""
   echo "Where would you like to output the file e.g. '/home/httpd/htdocs'"
   echo "You really should choose a location that your webserver has access to"
   echo "so that it is visible through a web browser."
   echo ""
   echo "(NOTE: The file will be called 'temp.html')"
   echo ""
   echo -n "Answer: "
   read output
   mkdir -p ${output} 1> /dev/null 2> /dev/null
   clear
   if [ ! -f /usr/bin/mrtg ]; then

      if [ ! -f /etc/make.profile/make.defaults ]; then
         clear
         echo "Forcing emerge sync..."
         sleep 2
         emerge sync
      fi

      clear
      echo "Emerging MRTG..."
      sleep 2
      emerge mrtg
      clear
   fi
   echo "Compiling mrtg-temp..."
   sleep 2
   cd ./src
   sed "s/@CHANGEME@/${scale}/" ./mrtg-temp.c > ./tmp.c
   rm -rf /bin/mrtg-temp
   gcc ./tmp.c -o /bin/mrtg-temp
   rm -rf ./tmp.c
   chmod 755 /bin/mrtg-temp
   chmod +s /bin/mrtg-temp
   cd ..
   clear
   echo "Installing config..."
   sleep 2
   cat ./config/header > ./xbox-temp.conf
   echo -n "WorkDir: " >> ./xbox-temp.conf
   echo "${output}" >> ./xbox-temp.conf
   cat ./config/footer >> ./xbox-temp.conf
   if [ $scale == "0" ]; then
      sed "s/@CHANGEME@/°C/g" ./xbox-temp.conf > ./tmp.conf
   else
      sed "s/@CHANGEME@/°F/g" ./xbox-temp.conf > ./tmp.conf
   fi
   rm -rf /etc/xbox-temp.conf
   mv ./tmp.conf /etc/xbox-temp.conf
   rm -rf ./xbox-temp.conf
   clear
   echo "Initialising MRTG..."
   mrtg /etc/xbox-temp.conf 1> /dev/null 2> /dev/null  
   mrtg /etc/xbox-temp.conf 1> /dev/null 2> /dev/null
   mrtg /etc/xbox-temp.conf 1> /dev/null 2> /dev/null
   clear
   echo "Adding MRTG to crontab..."
   sleep 2
   crontab -l | grep -v xbox-temp > /tmp/crontab.mrtg
   echo "*/5 * * * * /usr/bin/mrtg /etc/xbox-temp.conf 1> /dev/null 2> /dev/null" >> /tmp/crontab.mrtg   
   cat /tmp/crontab.mrtg | crontab -
   rm -rf /tmp/crontab.mrtg
   clear
   echo "All done..."
   echo "You can view the output in ${output}/temp.html"
fi
