########################################################
#           SARGRAPH INSTALLER SCRIPT V3.0 	       #
# This script is inended to install the sargraph in    #
#  Linux and Solaris platforms                         #
#						       #
########################################################
INSTALL_DIR=/var/www/html
PKG=sargraphv3.tgz
KEY=`echo $HOME/.ssh/sargraph_dsa`

##VALIDATE OS##
OS=`uname`
if [ $OS = "Linux" || $OS = "SunOS" ];then
echo "OS type is $OS"
else
echo "ERROR:OS not supported exiting..."
exit
fi
##Extract TAR archive##
echo "Note:Ensure you have met prerequisites before installation, read the online documentaion"
echo "Enter WEB root [Default: /var/www/html]:"
read INTALL_DIR
if [ ! -d $INSTALL_DIR ];then
echo "ERROR: Directory $INSTALL_DIR not exist! Exiting"
exit
else
echo "Please wait...Extracting sargraph package..."
tar xzf $PKG -C $INSTALL_DIR
if [ ! $? -eq 0 ];then
echo "Error occured! Exiting..."
exit
fi
##Create DSA key###
echo "Creating DSA SSH KEY... "
ssh-keygen -q -t dsa -N "" -f $KEY
if [ ! $? -eq 0 ];then
echo "ERROR: key generation failed!
exit
fi
echo "Updating configuration..."
awk -v variable="ROOTDIR" -v path="$INSTALL_DIR/sargraph" 'BEGIN{FS=OFS="="}$1==variable{$2=path}1' sargraph.conf >> $INSTALL_DIR/sargraph/etc/sargraph.conf.tmp
awk -v variable="KEY" -v path="$KEY" 'BEGIN{FS=OFS="="}$1==variable{$2=path}1' sargraph.conf >> $INSTALL_DIR/sargraph/etc/sargraph.conf.tmp
mv $INSTALL_DIR/sargraph/etc/sargraph.conf $INSTALL_DIR/sargraph/etc/sargraph.conf.PRE
mv $INSTALL_DIR/sargraph/etc/sargraph.conf.tmp $INSTALL_DIR/sargraph/etc/sargraph.conf
cp $INSTALL_DIR/sargraph/etc/sargraph.conf /etc/sargraph.conf
if [ ! $? -eq 0 ];then
echo "Sargraph configuration Failed! Please ensure you are running the INSTALLER as root user"
exit
else
echo "Configuration success!"
echo "Scheduling polling job"
crontab -l > /tmp/crontab.root
echo "00,10,20,30,40,50 * * * * $INSTALL_DIR/sargraph/scripts/getsar.sh > /dev/null 2>&1" >> /tmp/crontab.root
crontab /tmp/crontab.root
if [ $? -eq 0 ];then
echo "Job scheduled"
else 
echo "ERROR: Scheduling cron job failed, please verify the cron access of root user"
exit
fi
fi
fi
