#!/bin/bash
. /usr/lib/eole/ihm.sh

if [ $(CreoleGet eole_module) == "scribe" ]; then
    . /usr/lib/eole/eolead.sh
    NETLOGON="$CONTAINER_ROOTFS/home/sysvol/$(CreoleGet ad_domain)/scripts"

elif [ $(CreoleGet eole_module) == "seth" ]; then
    CONTAINER_ROOTFS="/"
    CONTAINER_NAME="$(CreoleGet ad_server_netbios_name)"
    NETLOGON="$CONTAINER_ROOTFS/home/sysvol/$(CreoleGet ad_realm)/scripts"
elif [ $(CreoleGet eole_module) == "amonecole" ]; then
    CONTAINER_NAME=addc
    CONTAINER_ROOTFS=/var/lib/lxc/$CONTAINER_NAME/rootfs
    NETLOGON="$CONTAINER_ROOTFS/home/sysvol/$(CreoleGet ad_realm)/scripts"
fi


if [ ! -d "$CONTAINER_ROOTFS" ]; then
    EchoOrange "Le conteneur $CONTAINER_NAME est absent"
    exit 0
fi


INFOSQUOTA="$NETLOGON/infosquota"
DOMAINUSERS="$NETLOGON/groups/Domain Users.txt"

if [ ! -d "$INFOSQUOTA" ];then
    echo "Installation d'infosquota dans le conteneur $CONTAINER_NAME"
    cp -rf /home/netlogon/infosquota "$INFOSQUOTA"
    cp "/home/netlogon/infosquota/infosquota.ini.sample" "$INFOSQUOTA/infosquota.ini"
fi

grep -q infosquota.exe "$DOMAINUSERS" 2>/dev/null
if [ $? -ne 0 ];then
    echo "Mise en place de la commande infosquota dans Domain Users.txt"
    echo "cmd,\\\\$CONTAINER_NAME\\netlogon\\infosquota\\infosquota.exe,NOWAIT" >> "$DOMAINUSERS"
fi

if [ ! -f /var/log/infosquota/recherche-fich-users.log ];then
    echo  "Première exécution de infosquota2"
    EchoOrange "Lancement de la recherche, "
    EchoOrange "veuillez patienter quelques minutes."
    EchoOrange "Merci ..."
    /etc/cron.weekly/findfic
    if [ $? -ne 0 ] ; then
        EchoRouge "Problème lors de l'exécution de /etc/cron.weekly/findfic"
    fi
fi

exit 0
