#!/bin/bash

if [ "$(CreoleGet activer_miroir)" == "oui" ]; then

    miroir_log=$(CreoleGet miroir_log)
    niveau_miroir=$(CreoleGet niveau_miroir)
    nom_domaine_local=$(CreoleGet nom_domaine_local)
    adresse_ip_eth2=$(CreoleGet adresse_ip_eth2 '')
    miroir_homedir=$(CreoleGet miroir_homedir)
    path_ministere=$(CreoleGet path_ministere)
    path_ddt=$(CreoleGet path_ddt)

    if [ "$(CreoleGet heure_maj)" == "aleatoire" ]; then
        if [ "$niveau_miroir" == "3" ]; then
            let "m=($RANDOM % 59)"
            echo "# telechargement des mises à jour de MacAfee" > /etc/cron.d/eole-antivir2
            echo $m "6 * * *	root	/usr/share/eole-antivir2/bin/eole-antivir2.j"' >> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2
            echo $m "12 * * *	root	/usr/share/eole-antivir2/bin/eole-antivir2.j"' >> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2

        else
            let "h=($RANDOM % 2)+ 4"
            let "m=($RANDOM % 59)"
            echo "# telechargement des mises à jour de MacAfee" > /etc/cron.d/eole-antivir2
            echo $m $h "* * *	root	/usr/share/eole-antivir2/bin/eole-antivir2.j "'>> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2
            let "h=($h+8)"
            echo $m $h "* * *	root	/usr/share/eole-antivir2/bin/eole-antivir2.j "'>> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2

        fi
    else
        heure_maj_hh=$(CreoleGet heure_maj_hh)
        heure_maj_mm=$(CreoleGet heure_maj_mm)
        echo "# telechargement des mises à jour de MacAfee " > /etc/cron.d/eole-antivir2
        echo $heure_maj_mm $heure_maj_hh "* * *   root    /usr/share/eole-antivir2/bin/eole-antivir2.j "'>> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2
        if [ "$heure_maj_hh" -ge "12" ]; then
			let "h=($heure_maj_hh - 8)"
			echo $heure_maj_mm $h "* * *   root    /usr/share/eole-antivir2/bin/eole-antivir2.j "'>> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2
		else
			let "h=($heure_maj_hh + 8)"
			echo $heure_maj_mm $h "* * *   root    /usr/share/eole-antivir2/bin/eole-antivir2.j "'>> '"$miroir_log/miroir-mcafee.log 2>&1" >> /etc/cron.d/eole-antivir2
        fi
    fi

    [ ! -d $miroir_log ] && mkdir -p $miroir_log


    [ ! -d /etc/proftpd/conf.d/ ] && mkdir -p /etc/proftpd/conf.d/


    if [ "$(CreoleGet module_type)" == "amon" ] ; then

        case "$(CreoleGet nombre_interfaces)" in
        1)
            echo "DefaultAddress $(CreoleGet adresse_ip_eth0)" >> /etc/proftpd/proftpd.conf
            ;;
        2)
            echo "DefaultAddress $(CreoleGet adresse_ip_eth1)" >> /etc/proftpd/proftpd.conf
            ;;
        3)
            echo "DefaultAddress $adresse_ip_eth2" >> /etc/proftpd/proftpd.conf
            ;;
        4)
            echo "DefaultAddress $adresse_ip_eth2" >> /etc/proftpd/proftpd.conf
            ;;
        5)
            echo "DefaultAddress $adresse_ip_eth2" >> /etc/proftpd/proftpd.conf
            ;;
        esac
    fi

    if [ "$(CreoleGet type_miroir)" == "miroir McAfee DDT" ]; then

		if [ -d $miroir_homedir/$path_ministere ]; then

			rm -rf $miroir_homedir/$path_ministere

			echo $(date) "Dossier des signatures Mac Afee Ministere sur un miroir DDT supprimé" >> $miroir_log/miroir-mcafee.log

		fi
		if [ ! -d $miroir_homedir/$path_ddt ]; then

			echo "signatures McAfee en cours de telechargement, merci de patienter..."

			/usr/share/eole-antivir2/bin/eole-antivir2.j

		fi


	fi

	if [ "$(CreoleGet type_miroir)" == "miroir McAfee Ministeriel" ]; then

		if [ -d $miroir_homedir/$path_ddt ]; then

			rm -rf $miroir_homedir/$path_ddt

			echo $(date) "Dossier des signatures Mac Afee DDT sur un miroir Ministere supprimé" >> $miroir_log/miroir-mcafee.log

		fi
		if [ ! -d $miroir_homedir/$path_ministere ]; then

			echo "signatures McAfee en cours de telechargement, merci de patienter..."

			/usr/share/eole-antivir2/bin/eole-antivir2.j

		fi


	fi


fi
exit 0
