#!/bin/bash
if [ "$1" != "yes" ]; then
        echo "You must confirm this reset with option 'yes'."
        echo "Example : $0 yes"
        exit 0
fi

# ECATP
cp /usr/share/doc/gesa-ecatp-client/ecatp.conf.example /etc/gesa/ecatp.conf

# RCM
dpkg -l esia-rcm|grep ii > /dev/null
if [ "$?" -eq "0" ]; then
    cp /usr/local/esia/rcm/conf.example.ini	/etc/esia/rcm.ini
    sed -i "s/VMKEY/$(sha1sum /var/log/daemon.log|cut -c 1-20)/g" /etc/esia/rcm.ini
fi

dpkg -l gesa-web-interface|grep ii > /dev/null
if [ "$?" -eq "0" ]; then
    # Configurations dans le répertoire home
    rm /home/esia/*.conf
    cp -n /usr/local/gesa/public_html/dev/default_conf_files/* /home/esia/
    chown -R esia:esia /home/esia/

    # Modification du mot de passe
    echo "esia:gesa" | chpasswd


    # visudo
    touch /etc/sudoers.tmp
    ## Edition de /tmp/sudoers.new
    grep -v www-data /etc/sudoers >/tmp/sudoers.new
    cat /usr/local/gesa/setup/visudo.conf >> /tmp/sudoers.new

    visudo -c -f /tmp/sudoers.new
    if [ "$?" -eq "0" ]; then
        cp /tmp/sudoers.new /etc/sudoers
    fi
    rm /etc/sudoers.tmp

    GESA_DIR=/usr/local/gesa
    HTML_DATA=$GESA_DIR/public_html/data
    for file in $(echo "$HTML_DATA/user.conf.json $HTML_DATA/network.conf.json")
    do
        [ -f "$file" ] && rm $file
    done

    IFACE=$(grep 2 /sys/class/net/*/ifindex |cut -d '/' -f5)
    cat << EOF > /etc/network/interfaces
auto lo
iface lo inet loopback

auto $IFACE
allow-hotplug $IFACE
iface $IFACE inet dhcp
EOF
    fi


    rm /root/.ssh/*
    rm /usr/local/gesa/public_html/data/ssh_keys/*

# Fichiers logs
rm /var/log/esia/*.log
rm /var/log/gesa*.log
rm /home/esia/gesa.log

# PluginList
find -L /usr/local/esia/plugins -maxdepth 1 -type f -executable|sort|xargs -n 1 basename > /etc/gesa/plugins-list.conf
