# -*-Shell-script-*-
#
# functions     This file contains functions to be used by Oreon install scripts
#

##
## COLOR FUNCTIONS
##


RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_INFO="echo -en \\033[1;38m"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"

OREON_CONF="/etc/oreon.conf"

function  echo_info() {
    echo -n "$1"
    $MOVE_TO_COL
    $SETCOLOR_INFO
    echo -n "$2"
    $SETCOLOR_NORMAL
    echo -e "\r"
}

function  echo_success() {
    echo -n "$1"
    $MOVE_TO_COL
    $SETCOLOR_SUCCESS
    echo -n "$2"
    $SETCOLOR_NORMAL
    echo -e "\r"
}

function echo_failure() {
    echo -n "$1"
    $MOVE_TO_COL
    $SETCOLOR_FAILURE
    echo -n "$2"
    $SETCOLOR_NORMAL
    echo -e "\r"
}

function echo_passed() {
    echo -n "$1"
    $MOVE_TO_COL
    $SETCOLOR_WARNING
    echo -n "$2"
    $SETCOLOR_NORMAL
    echo -e "\r"
}

function echo_warning() {
    echo -n "$1"
    $MOVE_TO_COL
    $SETCOLOR_WARNING
    echo -n "$2"
    $SETCOLOR_NORMAL
    echo -e "\r"
}

##
## FUNCTION
##
# Find the user and group nagios in nagios.cfg and ask if we don't find
function find_user_nagios()
{
    NAGIOS_USER=`more $NAGIOS_ETC/nagios.cfg | grep ^nagios_user | cut -d = -f 2`

    if [ -z "$NAGIOS_USER"  ] ; then
	for fichier in $NAGIOS_ETC/*.cfg
      do
	  if [ -f "$fichier" ];	then
	      NAGIOS_USER_TEMP=`more $fichier | grep ^nagios_user | cut -d = -f 2`
	      if [ -n "$NAGIOS_USER_TEMP" ]; then
		  NAGIOS_USER=$NAGIOS_USER_TEMP
    fi
	  fi
	done
    fi

    if [ -z "$NAGIOS_USER"  ] ; then
	#Nagios User
	NAGIOS_USER="nagios"
	echo_passed "Sorry, we don't found nagios user" "PASSED"
	echo "Which is your nagios user ?"
	echo -n "default to [$NAGIOS_USER]:"
	read temp
	test_answer NAGIOS_USER $temp
	echo ""
    else
	echo_info "Finding Nagios user :" "$NAGIOS_USER"
    fi

}

function find_group_nagios()
{
    NAGIOS_GROUP=`more $NAGIOS_ETC/nagios.cfg | grep ^nagios_group | cut -d = -f 2`

    if [ -z "$NAGIOS_GROUP"  ] ; then
	for fichier in $NAGIOS_ETC/*.cfg
      do
	  if [ -f "$fichier" ];	then
	      NAGIOS_GROUP_TEMP=`more $fichier | grep ^nagios_group | cut -d = -f 2`
	      if [ -n "$NAGIOS_GROUP_TEMP" ]; then
		  NAGIOS_GROUP=$NAGIOS_GROUP_TEMP
	      fi
	  fi
    done
    fi

    if [ -z "$NAGIOS_GROUP"  ] ; then
	#Nagios Group
	NAGIOS_GROUP="nagiocmd"
	echo_passed "Sorry, we don't found nagios group" "PASSED"
	echo "Which is your nagios group ?"
	echo -n "default to [$NAGIOS_GROUP]:"
	read temp
	test_answer NAGIOS_GROUP $temp
	echo ""
    else
	echo_info "Finding Nagios group :" "$NAGIOS_GROUP"
    fi

}

# Check the user and group nagios and create it if necessary

function check_user_nagios()
{
	# Try to find nagios user/group from nagios.cfg file.
    # If not, we ask...
    find_user_nagios


    nagios_user_grep=`grep $NAGIOS_USER /etc/passwd > /dev/null; echo $?`
    if [ $nagios_user_grep == 0 ]; then
    	echo_success "Finding nagios user '$NAGIOS_USER' in /etc/passwd" "YES"
    else
    	echo_warning "Finding nagios user '$NAGIOS_USER' in /etc/passwd" "NOT FOUND"
  		useradd -G $NAGIOS_GROUP $NAGIOS_USER
  		if [ $? == 0 ]; then
		    echo_success  "Creating nagios user '$NAGIOS_USER'" "OK"
		else
		    echo_failure  "error creating nagios user '$NAGIOS_USER' check you environnement" "ERROR"
		    exit
		fi
    fi
}

function check_group_nagios()
{
	# Try to find nagios user/group from nagios.cfg file.
	# If not, we ask...
    find_group_nagios

    nagios_group_grep=`grep $NAGIOS_GROUP /etc/group > /dev/null; echo $?`
    if [ $nagios_group_grep == 0 ]; then
    	echo_success "Finding nagios group '$NAGIOS_GROUP' in /etc/group" "YES"
    else
    	echo_warning "Finding nagios group '$NAGIOS_GROUP' in /etc/group" "NOT FOUND"
	groupadd $NAGIOS_GROUP
	if [ $? == 0 ]; then
      echo_success  "Creating nagios group '$NAGIOS_GROUP'" "OK"
	else
	    echo_failure  "error creating group $NAGIOS_GROUP check you environnement" "ERROR"
	    exit
	fi
    fi
}

function check_group_nagiocmd()
{
    nagios_group=`grep nagiocmd /etc/group > /dev/null; echo $?`
    if [ $nagios_group == 0 ]; then
	echo_success "Finding nagios group 'nagiocmd' in /etc/group" "YES"
    else
	echo_warning "Finding nagios group 'nagiocmd' in /etc/group" "NO"
	groupadd nagiocmd
	if [ $? == 0 ]; then
	    echo_passed  "Creating nagios group 'nagiocmd'" "OK"
	else
	    echo_failure  "error creating nagios group 'nagiocmd' check you environnement" "ERROR"
	    exit
	fi
    fi
}


function check_nagios_init_script()
{
    if [ -x "/etc/init.d/nagios" ]; then
		NAGIOS_INIT_SCRIPT="/etc/init.d/nagios"
    else if [ -x "/etc/init.d/nagios2" ] ; then
		NAGIOS_INIT_SCRIPT="/etc/init.d/nagios2"
    fi
    fi

    if [ -z "$NAGIOS_INIT_SCRIPT" ] ; then
		#Nagios init script
		NAGIOS_INIT_SCRIPT="/etc/init.d/nagios2"
		echo_passed "Sorry, we don't found nagios init script" "PASSED"
		echo "Which is your nagios init script ?"
		echo -n "default is [$NAGIOS_INIT_SCRIPT]:"
		read temp
		test_answer NAGIOS_INIT_SCRIPT $temp
		echo ""
    else
		echo_info "Finding nagios init script :" "$NAGIOS_INIT_SCRIPT"
    fi

}

function check_httpd_directory()
{
    if test -d /etc/apache/conf ; then
		DIR_APACHE="/etc/apache/conf"
		DIR_APACHE_CONF="/etc/apache/conf.d"
		HTPASSWD="htpasswd"
    else if test -d /usr/local/apache2/conf ; then
		DIR_APACHE="/usr/local/apache2/conf"
		DIR_APACHE_CONF="/usr/local/apache2/conf"
		HTPASSWD="htpasswd2"
    else if test -d /etc/apache2 ; then
		DIR_APACHE="/etc/apache2"
		DIR_APACHE_CONF="/etc/apache2/conf.d"
		HTPASSWD="htpasswd2"
    else if test -d /etc/httpd/conf ; then
		DIR_APACHE="/etc/httpd/conf"
		DIR_APACHE_CONF="/etc/httpd/conf.d"
		HTPASSWD="htpasswd"
    else if test -d /usr/local/etc/apache ; then
		DIR_APACHE="/usr/local/etc/apache"
		DIR_APACHE_CONF="/usr/local/etc/apache/Includes"
		HTPASSWD="htpasswd"
    else if test -d /usr/local/etc/apache2 ; then
		DIR_APACHE="/usr/local/etc/apache2"
		DIR_APACHE_CONF="/usr/local/etc/apache2/Includes"
		HTPASSWD="htpasswd"
    fi
    fi
    fi
    fi
    fi
    fi

    if test -e $DIR_APACHE/apache2.conf ; then
		APACHE_CONF="apache2.conf"
    else if test -e $DIR_APACHE/commondhttpd.conf ; then
		APACHE_CONF="commondhttpd.conf"
    else if test -e $DIR_APACHE/httpd.conf ; then
		APACHE_CONF="httpd.conf"
    fi
    fi
    fi
}

function check_user_apache()
{
    WEB_USER=`more $DIR_APACHE/$APACHE_CONF | grep -w ^User | cut -d " " -f 2`

    if [ -z "$WEB_USER"  ] ; then
	for fichier in $DIR_APACHE/*
	  do
	  if [ -f "$fichier" ];	then
	      WEB_USER_TEMP=`more $fichier | grep -w ^User | cut -d " " -f 2`
	      if [ -n "$WEB_USER_TEMP" ]; then
		  WEB_USER=$WEB_USER_TEMP
	      fi
	  fi
	done
    fi
    echo_info "Finding Apache user :" "$WEB_USER"
}

function check_group_apache()
{
    WEB_GROUP=`more $DIR_APACHE/$APACHE_CONF | grep -w ^Group | cut -d " " -f 2`

    if [ -z "$WEB_GROUP"  ] ; then
	for fichier in $DIR_APACHE/*
	  do
	  if [ -f "$fichier" ];	then
	      WEB_GROUP_TEMP=`more $fichier | grep -w ^Group | cut -d " " -f 2`
	      if [ -n "$WEB_GROUP_TEMP" ]; then
		  WEB_GROUP=$WEB_GROUP_TEMP
	      fi
	  fi
	done
    fi
    echo_info "Finding Apache group :" "$WEB_GROUP"
}

function install_oreon_plugins()
{
    echo ""
	echo "------------------------------------------------------------------------"
	echo "                     Start Plugins Oreon Installation"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""
    if test -d $NAGIOS_PLUGIN ; then
		echo_passed "$NAGIOS_PLUGIN already exists" "PASSED"
    else
		mkdir $NAGIOS_PLUGIN >> $LOG_FILE 2>> $LOG_FILE
		echo_success "Creating Nagios Plugin Directory '$NAGIOS_PLUGIN'" "OK"
    fi
    echo 	""
    echo "Preparing Oreon Plugins..."
    if [ -n "$PLUGINS_DIR" ]; then
		rm -f $PLUGINS_DIR/*.pl 2> /dev/null
		rm -f $PLUGINS_DIR/*.pm 2> /dev/null
		rm -f $PLUGINS_DIR/*.conf 2> /dev/null
    else
		rm -f *.pl 2> /dev/null
		rm -f *.pm 2> /dev/null
		rm -f *.conf 2> /dev/null
    fi

    if [ -n "$PLUGINS_DIR" ]; then
		for fichier in ${PLUGINS_DIR}src/*
		  do
		  if [ -d "$fichier" ]; then
		      echo ""
		  else
		      filename=`echo $fichier  | sed -e 's|.*\/\(.*\)|\1|'`
		      echo_success "  -> $filename" "OK"
		      `sed -e 's|@INSTALL_DIR_NAGIOS@|'"$INSTALL_DIR_NAGIOS"'|g' -e 's|@NAGIOS_ETC@|'"$NAGIOS_ETC"'|g' -e 's|@NAGIOS_PLUGINS@|'"$NAGIOS_PLUGIN"'|g' -e 's|@RRDTOOL_PERL_LIB@|'"$RRD_PERL"'|g' -e 's|@INSTALL_DIR_OREON@|'"$INSTALL_DIR_OREON"'|g'  "$fichier" > "$PLUGINS_DIR/$filename"`
		  fi
		done
		echo "Do you want to install Oreon Plugins ? "
		echo ""
		echo -n "[y/n], default to [y]:"
		read temp
		if [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ] ;then
                    while [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ]
                      do
                      echo "Do you want to install Oreon Plugins ? "
                      echo -n "[y/n], default to [y]:"
                      read temp
                    done
		fi
		if [ -z $temp ];then
		    temp=y
		fi
		if [ $temp = "y" ];then
			cp $PLUGINS_DIR/*.pl $NAGIOS_PLUGIN
			echo_success "Installing Oreon Plugins on '$NAGIOS_PLUGIN'" "OK"
		fi
    else
		for fichier in src/*
		  do
		  if [ -d "$fichier" ]; then
		      echo "$fichier is a directory"
		  else
		      filename=`echo $fichier  | sed -e 's|.*\/\(.*\)|\1|'`
		      echo_success "  -> $filename" "OK"
		      `sed -e 's|@INSTALL_DIR_NAGIOS@|'"$INSTALL_DIR_NAGIOS"'|g' -e 's|@NAGIOS_ETC@|'"$NAGIOS_ETC"'|g' -e 's|@NAGIOS_PLUGINS@|'"$NAGIOS_PLUGIN"'|g' -e 's|@RRDTOOL_PERL_LIB@|'"$RRD_PERL"'|g' -e 's|@INSTALL_DIR_OREON@|'"$INSTALL_DIR_OREON"'|g'  "$fichier" > "$filename"`
		  fi
		done
		echo_success "Installing Oreon Plugins on '$NAGIOS_PLUGIN'" "OK"
		cp *.pl $NAGIOS_PLUGIN
    fi

    chown -R $WEB_USER:$NAGIOS_GROUP $NAGIOS_PLUGIN >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $NAGIOS_PLUGIN >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $NAGIOS_PLUGIN/* >> $LOG_FILE 2>> $LOG_FILE

    echo ""
	echo "------------------------------------------------------------------------"
	echo "                     Start Traps Handler Installation"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""

    if [ -n "$PLUGINS_DIR/traps" ]; then
	if [ -e "$NAGIOS_PLUGIN/traps" ]; then
	    echo_passed "$NAGIOS_PLUGIN/traps already exists" "PASSED"
	else
		echo_passed "$NAGIOS_PLUGIN/traps created" "OK"
	    mkdir $NAGIOS_PLUGIN/traps
	fi
	echo 	""
    echo "Preparing Oreon Plugins..."
		for fichier in $PLUGINS_DIR/src/traps/*
		  do
		  filename=`echo $fichier  | sed -e 's|.*\/\(.*\)|\1|'`
		  echo "-> $filename"
		  `sed -e 's|@INSTALL_DIR_NAGIOS@|'"$INSTALL_DIR_NAGIOS"'|g' -e 's|@NAGIOS_ETC@|'"$NAGIOS_ETC"'|g' -e 's|@NAGIOS_PLUGINS@|'"$NAGIOS_PLUGIN"'|g' -e 's|@RRDTOOL_PERL_LIB@|'"$RRD_PERL"'|g' -e 's|@INSTALL_DIR_OREON@|'"$INSTALL_DIR_OREON"'|g'  "$fichier" > "$PLUGINS_DIR/$filename"`
		done
		echo ""
		echo "Do you want to install Oreon Traps Plugins ? "
		echo ""
		echo -n "[y/n], default to [y]:"
		read temp
		if [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ] ;then
                    while [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ]
                      do
                      echo "Do you want to install Oreon Traps Plugins ? "
                      echo -n "[y/n], default to [y]:"
                      read temp
                    done
                fi
		if [ -z $temp ];then
		    temp=y
		fi
		if [ $temp = "y" ];then
			echo "Installing Oreon Traps Plugins on "
			cp $PLUGINS_DIR/src/traps/* $NAGIOS_PLUGIN/traps
			chmod 775 $NAGIOS_PLUGIN/traps/* >> $LOG_FILE 2>> $LOG_FILE
			echo_success "$NAGIOS_PLUGIN/traps'" "OK"
		fi
    fi

    if test -a $NAGIOS_PLUGIN/oreon.conf ; then
		echo ""
		echo_success "Finding Oreon Plugins configuration file 'oreon.conf' :" "OK"
		echo "You already seem to have installed the plugins Oreon."
		echo "Do you want overwrite this file ? You must regenerate this one from Oreon interface."
		echo -n "[y/n], default to [n]:"
		read temp
		if [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ] ;then
                    while [ "$temp" != "y" ] && [ "$temp" != "n" ] && [ ! -z $temp ]
                      do
                      echo "Do you want overwrite this file ? You must regenerate this one from Oreon interface."
                      echo -n "[y/n], default to [n]:"
                      read temp
                    done
                fi
		if [ -z $temp ];then
		    temp=n
		fi
    else
		temp=y
    fi

    if [ $temp = "y" ];then
		cp ${PLUGINS_DIR}oreon.conf $NAGIOS_PLUGIN >> $LOG_FILE 2>> $LOG_FILE
		chmod 775 $NAGIOS_PLUGIN/oreon.conf >> $LOG_FILE 2>> $LOG_FILE
		chown $WEB_USER:$WEB_GROUP $NAGIOS_PLUGIN/oreon.conf >> $LOG_FILE 2>> $LOG_FILE
	fi

    cp ${PLUGINS_DIR}oreon.pm $NAGIOS_PLUGIN >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $NAGIOS_PLUGIN/oreon.pm >> $LOG_FILE 2>> $LOG_FILE
}

function install_ods(){
	
	echo ""
	echo "------------------------------------------------------------------------"
	echo "                       Start ODS Installation"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""
	
	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' $INSTALL_DIR_OREON/ODS/ods.pl > $INSTALL_DIR_OREON/ODS/ods_new.pl
	mv $INSTALL_DIR_OREON/ODS/ods_new.pl $INSTALL_DIR_OREON/ODS/ods.pl
 	chown $NAGIOS_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/ODS/ods.pl
	chmod 7755 $INSTALL_DIR_OREON/ODS/ods.pl
	echo_success "Replace ODS Macro " "OK"
    
	if test -d $INSTALL_DIR_OREON/OreonDataStorage ; then
		echo_passed "ODS data Directory already exists" "PASSED"
    else
		mkdir $INSTALL_DIR_OREON/OreonDataStorage >> $LOG_FILE 2>> $LOG_FILE
		echo_success "Creating Oreon Directory '$INSTALL_DIR_OREON/OreonDataStorage'" "OK"
    fi
	chown $NAGIOS_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/OreonDataStorage
	chmod 775 $INSTALL_DIR_OREON/OreonDataStorage
	
	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' -e 's|@NAGIOS_USER@|'"$NAGIOS_USER"'|g' -e 's|@NAGIOS_GROUP@|'"$NAGIOS_GROUP"'|g' $INSTALL_DIR_OREON/ODS_SRC_ETC/ods > /etc/init.d/ods
	chmod 755 /etc/init.d/ods
	
	chmod -R 775 $INSTALL_DIR_OREON/ODS/var/
	chown -R $NAGIOS_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/ODS/var
	rm -Rf $INSTALL_DIR_OREON/ODS_SRC_ETC
	
	echo ""
	echo "------------------------------------------------------------------------"
	echo "                    Replace Macro In ODS cron script"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""
	
	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' $INSTALL_DIR_OREON/cron/inventory_update.php > $INSTALL_DIR_OREON/cron/inventory_update_new.php	
	mv $INSTALL_DIR_OREON/cron/inventory_update_new.php $INSTALL_DIR_OREON/cron/inventory_update.php
	chown -R $WEB_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/cron/inventory_update.php >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $INSTALL_DIR_OREON/cron/reporting/inventory_update.php >> $LOG_FILE 2>> $LOG_FILE
	echo_success "in $INSTALL_DIR_OREON/cron/inventory_update.php" "OK"
	
	
	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php > $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB_new.php	
	mv $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB_new.php $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php
	chown -R $WEB_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php >> $LOG_FILE 2>> $LOG_FILE
	echo_success "in $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php" "OK"
	
	
	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' $INSTALL_DIR_OREON/cron/parsing_status.pl > $INSTALL_DIR_OREON/cron/parsing_status_new.pl
	mv $INSTALL_DIR_OREON/cron/parsing_status_new.pl $INSTALL_DIR_OREON/cron/parsing_status.pl
	chown -R $WEB_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/cron/parsing_status.pl >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $INSTALL_DIR_OREON/cron/parsing_status.pl >> $LOG_FILE 2>> $LOG_FILE
	echo_success "in $INSTALL_DIR_OREON/cron/parsing_status.pl" "OK"
	

	sed -e 's|@OREON_PATH@|'"$INSTALL_DIR_OREON"'|g' $INSTALL_DIR_OREON/cron/parsing_log.pl > $INSTALL_DIR_OREON/cron/parsing_log_new.pl
	mv $INSTALL_DIR_OREON/cron/parsing_log_new.pl $INSTALL_DIR_OREON/cron/parsing_log.pl
	chown -R $WEB_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/cron/parsing_log.pl >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $INSTALL_DIR_OREON/cron/parsing_log.pl >> $LOG_FILE 2>> $LOG_FILE
	echo_success "in $INSTALL_DIR_OREON/cron/parsing_log.pl" "OK"
	

}

function config_cron(){

    echo ""
	echo "------------------------------------------------------------------------"
	echo "                         Start Oreon Cron Configuration"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""
	
	PHP_FLG=`which php > /dev/null 2> /dev/null; echo $?`
	if [ "$PHP_FLG" = "0" ] ; then
	    PHP_BIN="php"
	else
	
	    PHP_FLG=`which php5 > /dev/null 2> /dev/null; echo $?`
	    if [ "$PHP_FLG" == '0' ] ; then
	        PHP_BIN="php5"
	    else
	        echo "PHP not found. Oreon take php by default"
	        PHP_BIN="php"
	    fi
	fi
	
	CRON=0
	if test -f /etc/cron.d/oreon ; then
	    CRON=`cat /etc/cron.d/oreon | grep inventory_cron_update.php | grep -v \# > /dev/null; echo $?`
	fi
 	if [ $CRON == '1' ] || [ ! -f /etc/cron.d/oreon ] ; then	
  		echo "0 0 * * * $NAGIOS_USER $PHP_BIN -q $INSTALL_DIR_OREON/cron/reporting/inventory_cron_update.php >> $INSTALL_DIR_OREON/log/ArchiveLogInDB_log 2>> $INSTALL_DIR_OREON/log/ods_parsing_log" >> /etc/cron.d/oreon 
		echo_success "Setup Cron for inventory_cron_update.php" "OK"
  	else
      	echo_passed "Setup Cron for inventory_cron_update.php" "PASSED"
  	fi
	
	CRON=0
	CRON=`cat /etc/cron.d/oreon | grep ArchiveLogInDB.php | grep -v \# > /dev/null; echo $?`
	

 	if [ $CRON == '1' ] || [ ! -f /etc/cron.d/oreon ] ; then	
  		echo "0 0 1-31 * * $NAGIOS_USER $PHP_BIN -q $INSTALL_DIR_OREON/cron/reporting/ArchiveLogInDB.php >> $INSTALL_DIR_OREON/log/ArchiveLogInDB_log 2>> $INSTALL_DIR_OREON/log/ods_parsing_log" >> /etc/cron.d/oreon 
		echo_success "Setup Cron for ArchiveLogInDB.php" "OK"
  	else
      	echo_passed "Setup Cron for ArchiveLogInDB.php" "PASSED"
  	fi

	CRON=0
	CRON=`cat /etc/cron.d/oreon | grep parsing_status.pl | grep -v \# > /dev/null; echo $?`
	
 	if [ $CRON == '1' ] || [ ! -f /etc/cron.d/oreon ] ; then	
  		echo "* * * * * $NAGIOS_USER $INSTALL_DIR_OREON/cron/parsing_status.pl >> $INSTALL_DIR_OREON/log/ods_parsing_status 2>> $INSTALL_DIR_OREON/log/ods_parsing_status" >> /etc/cron.d/oreon 
		echo_success "Setup Cron for parsing_status.pl" "OK"
  	else
      	echo_passed "Setup Cron for parsing_status.pl" "PASSED"
  	fi

	CRON=0
	CRON=`cat /etc/cron.d/oreon | grep parsing_log.pl | grep -v \# > /dev/null; echo $?`

 	if [ $CRON == '1' ] || [ ! -f /etc/cron.d/oreon ] ; then
  		echo "* * * * * $NAGIOS_USER $INSTALL_DIR_OREON/cron/parsing_log.pl >> $INSTALL_DIR_OREON/log/ods_parsing_log 2>> $INSTALL_DIR_OREON/log/ods_parsing_log" >> /etc/cron.d/oreon 
		echo_success "Setup Cron for parsing_log.pl" "OK"
  	else
      	echo_passed "Setup Cron for parsing_log.pl" "PASSED"
  	fi
  	
  	restart_cron
}

function restart_cron()
{
 
    echo ""
    echo "------------------------------------------------------------------------"
    echo "                         Restart Cron deamon"
    echo "------------------------------------------------------------------------"
  	echo ""
	echo ""
  	if test -x /etc/init.d/cron ; then
      /etc/init.d/cron restart
   	else
      echo_passed "We don't find Mysql server. OREON will not run." "PASSED"
   	fi
}



function install_oreon(){

    echo ""
	echo "------------------------------------------------------------------------"
	echo "                              Start Oreon Installation"
	echo "------------------------------------------------------------------------"
	echo ""
	echo ""

    if test -d $INSTALL_DIR_OREON ; then
		echo_passed "Oreon Directory already exists" "PASSED"
    else
		mkdir $INSTALL_DIR_OREON >> $LOG_FILE 2>> $LOG_FILE
		echo_success "Creating Oreon Directory '$INSTALL_DIR_OREON'" "OK"
    fi

    for directory in "filesGeneration" "filesUpload" "GPL_LIB" "log" "rrd" "www" "cron" "ODS" "ODS_SRC_ETC" "doc"
      do
      if test -d $directory ; then
		  cp -Rf $directory $INSTALL_DIR_OREON >> $LOG_FILE 2>> $LOG_FILE
		  echo_success "Copy '$directory'" "OK"
      fi
    done
    
    chown -R $WEB_USER:$WEB_GROUP $INSTALL_DIR_OREON/ >> $LOG_FILE 2>> $LOG_FILE
    chmod -R 775 $INSTALL_DIR_OREON >> $LOG_FILE 2>> $LOG_FILE
    chmod -R 775 $INSTALL_DIR_OREON/* >> $LOG_FILE 2>> $LOG_FILE
    chmod -R 775 $INSTALL_DIR_NAGIOS  >> $LOG_FILE 2>> $LOG_FILE
    chmod -R 775 $INSTALL_DIR_NAGIOS/*  >> $LOG_FILE 2>> $LOG_FILE
    if test -d /etc/nagios ; then
		chmod -R 775 /etc/nagios >> $LOG_FILE 2>> $LOG_FILE
    fi

    install_oreon_plugins

    if test -d $NAGIOS_ETC ; then
		echo_passed "$NAGIOS_ETC already exists" "PASSED"
    else
		mkdir  $NAGIOS_ETC  >> $LOG_FILE 2>> $LOG_FILE
		echo_success "Creating '$NAGIOS_ETC'" "OK"
    fi

    chown -R $WEB_USER:$NAGIOS_GROUP $NAGIOS_ETC >> $LOG_FILE 2>> $LOG_FILE
    chmod 775 $NAGIOS_ETC >> $LOG_FILE 2>> $LOG_FILE

    if test -d $INSTALL_DIR_OREON/filesGeneration/nagiosCFG ; then
	echo_passed "$INSTALL_DIR_OREON/filesGeneration/nagiosCFG already exists" "PASSED"
    else
	echo_success "Creating '$INSTALL_DIR_OREON/filesGeneration/nagiosCFG'" "OK"
	mkdir $INSTALL_DIR_OREON/filesGeneration/nagiosCFG
    fi

    chmod 775 $INSTALL_DIR_OREON/filesGeneration >> $LOG_FILE 2>> $LOG_FILE
    chown -R $WEB_USER:$WEB_GROUP $INSTALL_DIR_OREON/filesGeneration >> $LOG_FILE 2>> $LOG_FILE

    if test -d $INSTALL_DIR_OREON/filesUpload/nagiosCFG ; then
		echo_passed "$INSTALL_DIR_OREON/filesUpload/nagiosCFG already exists" "PASSED"
    else
		echo_success "Creating '$INSTALL_DIR_OREON/filesUpload/nagiosCFG'" "OK"
		mkdir $INSTALL_DIR_OREON/filesUpload/nagiosCFG
    fi

    chmod 775 $INSTALL_DIR_OREON/filesUpload/nagiosCFG >> $LOG_FILE 2>> $LOG_FILE
    chown -R $WEB_USER:$WEB_GROUP $INSTALL_DIR_OREON/filesUpload/nagiosCFG >> $LOG_FILE 2>> $LOG_FILE

    chmod 775 $INSTALL_DIR_OREON/rrd/*  >> $LOG_FILE 2>> $LOG_FILE
    chown -R $NAGIOS_USER:$NAGIOS_GROUP $INSTALL_DIR_OREON/rrd >> $LOG_FILE 2>> $LOG_FILE
}
