#!/bin/bash
boucleSondes(){

        echo "$nums/$rep" > /home/system/df$user.txt
        date >> /home/system/df$user.txt ; df -h >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; du -h -d 3 /home/$user/ >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; cat /etc/rsnapshot.conf | grep -P "^backup\t/|^exclude\t|^snapshot_root\t|^backup_exec\t" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; cat /etc/systemd/system/rsnapshot@.service | grep -P "^ExecStopPost" >> /home/system/df$user.txt

        echo " " >> /home/system/df$user.txt ; echo "sauvegarde : $varAPI" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; echo "uptime : $varAPIReboot" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; echo "spooler : $varAPISpooler" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; echo "vgec.gim : $varAPIVGEC" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; echo "IP : $varAPIIP" >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; echo "IP_PUB : $varIPPUB" >> /home/system/df$user.txt

        echo " " >> /home/system/df$user.txt ; dnf list sauvegarde gimini --installed >> /home/system/df$user.txt
        echo " " >> /home/system/df$user.txt ; cat /etc/systemd/system/synchroWatch.service | grep -P "^ExecStart" >> /home/system/df$user.txt

        echo "rsync -e \"ssh -p 50218 -i /home/system/.ssh/$(hostname)_rsa\" -av --progress /home/system/df$user.txt sauvegarde@nasus.timci.com:/home/sauvegarde/$nums/"
        rsync -e "ssh -p 50218 -i /home/system/.ssh/$(hostname)_rsa" -av --progress /home/system/df$user.txt sauvegarde@nasus.timci.com:/home/sauvegarde/$nums/

        ############### FULL LINUX uniquement
        echo "CONFIG fulllinux"
        if [ -f "/etc/sauvegarde-gimini.conf" ]; then

                echo " " >> /home/system/df$user.txt ; echo "FULLLINUX : OUI" >> /home/system/df$user.txt

                echo "curl -X GET https://api.timci.com/api/Serveurs_Events/$nums/$rep/sauvegarde/$varAPI -H accept: */*"
                curl -X GET "https://api.timci.com/api/Serveurs_Events/$nums/$rep/sauvegarde/$varAPI" -H "accept: */*" -H "Authorization: bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IkFETUlOIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1ODk3NiIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTg5NzYifQ.eRjvkY5L-LeoEKntbAPzmjlffkvPE9J7yJq68YHbZxU"

                echo "curl -X GET https://api.timci.com/api/Serveurs_Events/$nums/$rep/reboot/$varAPIReboot"
                curl -X GET "https://api.timci.com/api/Serveurs_Events/$nums/$rep/reboot/$varAPIReboot"

                echo "curl -X GET https://api.timci.com/api/Serveurs_Events/$nums/$rep/spooler/$varAPISpooler"
                curl -X GET "https://api.timci.com/api/Serveurs_Events/$nums/$rep/spooler/$varAPISpooler"

                echo "curl -X GET https://api.timci.com/api/Serveurs_Events/version/$nums/$varAPIVGEC"
                curl -X GET "https://api.timci.com/api/Serveurs_Events/version/$nums/$varAPIVGEC"

                ssh -o StrictHostKeyChecking=accept-new -t $user@localhost -i /home/system/.ssh/$(hostname)_rsa /home/system/executessh.sh psmlimit > /home/system/resPsmlimit_$user.txt
                ssh -t $user@localhost -i /home/system/.ssh/$(hostname)_rsa /home/system/executessh.sh putgimex

        else
                 echo " " >> /home/system/df$user.txt ; echo "FULLLINUX : NON" >> /home/system/df$user.txt
        fi
        ############### FIN FULL LINUX uniquement

}
bouclePSM(){

        limitelocreel=$(cat /home/system/resPsmlimit_$user.txt | grep limitelocreel)
        limitelocreel=$(echo ${limitelocreel##*/})
        num_limitelocreel=$(echo $num_limitelocreel $limitelocreel | awk '{print $1+$2}')
        limiteloc=$(cat /home/system/resPsmlimit_$user.txt | grep limiteloc/)
        limiteloc=$(echo ${limiteloc##*/})
        num_limiteloc=$(echo $num_limiteloc $limiteloc | awk '{print $1+$2}')

        limitelotreel=$(cat /home/system/resPsmlimit_$user.txt | grep limitelotreel)
        limitelotreel=$(echo ${limitelotreel##*/})
        num_limitelotreel=$(echo $num_limitelotreel $limitelotreel | awk '{print $1+$2}')


        limitelot=$(cat /home/system/resPsmlimit_$user.txt | grep limitelot/)
        limitelot=$(echo ${limitelot##*/})
        num_limitelot=$(echo $num_limitelot $limitelot | awk '{print $1+$2}')

        echo "num_limitelocreel = $num_limitelocreel"
        echo "num_limiteloc = $num_limiteloc"
        echo "num_limitelotreel = $num_limitelotreel"
        echo "num_limitelot = $num_limitelot"

}

num_limitelocreel="0"
num_limiteloc="0"
num_limitelotreel="0"
num_limitelot="0"

nums=$(su - system /home/system/disks/s/theox/command/txserial | sed 's/[0-9]*-//g')
nums=$(sed 's/^0*//g' <<< ${nums})
nums=$(printf "%05d" $nums)

echo " "
#if [[ -n $1 ]]; then
#        echo "Nom de compte precise"
#        nom=$1
#fi
#if [[ -n $2 ]]; then
#        echo "Serial en parametre = $2"
#        nums=$2
#fi

if [[ -z "$nums" ]]
then
        echo "Error: txserial vide"
        exit
fi

varAPI=$(grep -HnP "alpha: completed successfull" /var/log/rsnapshot | tail -n 1 | sed -e 's/[^[]*\[\([^]]*\)]/\1/' | sed -e 's/ .*//g')

varAPIReboot=$(uptime -s | sed 's/ /T/g')

varAPISpooler=$(ls -l /home/system/disks/s/theos/spooler/system.spooler/ -I 'queue.*' | wc -l)
((varAPISpooler--))

varAPIVGEC=$(cat /home/system/disks/s/vgec.gim)
varAPIVGEC=$(echo ${varAPIVGEC%,*}| sed 's/,/ /g')
varAPIVGEC=$(date -d "$varAPIVGEC" +"%Y-%m-%dT%H:%M")

varAPIIP=`hostname -I`
varAPIIP=$(echo ${varAPIIP%% *})

varIPPUB=`curl https://ipinfo.io/ip`
echo "curl -X GET https://api.timci.com/api/Serveurs_Events/ip/$nums/$varIPPUB"
curl -X GET "https://api.timci.com/api/Serveurs_Events/ip/$nums/$varIPPUB"

#if [[ -n $1 ]]; then
#        echo "Nom de compte precise = $1"
#       NOMUSER=$1
#        NOMREP=$(echo $NOMUSER | /usr/bin/tr '[:lower:]' '[:upper:]')
#        rep=$NOMREP
#        user=$NOMUSER
#        boucleSondes
#fi

if [[ -n $2 ]]; then
        if [[ -n $1 ]]; then
                echo "Nom de compte precise = $1"
                NOMUSER_DONGLE2=$1
        fi
fi

rm -f /home/system/df*.txt

#for i in `/bin/find /home/system/ -maxdepth 1 -name "df*.txt" 2> /dev/null | /bin/sort`
for i in `cat /home/system/disks/s/theos/config/account.ini | grep -v "\[__uid__\]" | grep -v "\[system\]" |  grep "\[" | sed -e "s/\[//g; s/\]//g;"`
do
        if [[ $i == $NOMUSER_DONGLE2 ]]
        then
                echo "user $NOMUSER sur autre dongle. Pas de synchro sur $nums"
        else

                NOMUSER=$i
                NOMREP=$(echo $NOMUSER | /usr/bin/tr '[:lower:]' '[:upper:]')

                rep=$NOMREP
                user=$NOMUSER

                boucleSondes&
        fi
done
wait

if [[ -n $2 ]]; then
        echo "Serial en parametre = $2"
        nums=$2

        if [[ -z "$nums" ]]
        then
                echo "Error: txserial vide"
                exit
        fi

        if [[ -n $1 ]]; then
                echo "Nom de compte precise = $1"

                echo "curl -X GET https://api.timci.com/api/Serveurs_Events/ip/$nums/$varIPPUB"
                curl -X GET "https://api.timci.com/api/Serveurs_Events/ip/$nums/$varIPPUB"

                NOMUSER=$1
                NOMREP=$(echo $NOMUSER | /usr/bin/tr '[:lower:]' '[:upper:]')

                rep=$NOMREP
                user=$NOMUSER

                boucleSondes
        fi

fi
if [ -f "/etc/sauvegarde-gimini.conf" ]; then
        for i in `cat /home/system/disks/s/theos/config/account.ini | grep -v "\[__uid__\]" | grep -v "\[system\]" |  grep "\[" | sed -e "s/\[//g; s/\]//g;"`
        do
                if [[ $i == $NOMUSER_DONGLE2 ]]
                then
                        echo "user $NOMUSER sur autre dongle. Pas de synchro sur $nums"
                else

                        NOMUSER=$i
                        NOMREP=$(echo $NOMUSER | /usr/bin/tr '[:lower:]' '[:upper:]')

                        rep=$NOMREP
                        user=$NOMUSER

                        bouclePSM
                fi
        done

        echo "curl -X GET \"https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOCREEL/$num_limitelocreel/\""
        echo "num_limitelocreel = $num_limitelocreel"
        curl -X GET "https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOCREEL/$num_limitelocreel/"

        echo "num_limitelotreel = $num_limitelotreel"
        echo "curl -X GET \"https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOTREEL/$num_limitelotreel/\""
        curl -X GET "https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOTREEL/$num_limitelotreel/"

        echo "curl -X GET \"https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOCCONT/$num_limiteloc/\""
        echo "num_limiteloc = $num_limiteloc"
        curl -X GET "https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOCCONT/$num_limiteloc/"

        echo "curl -X GET \"https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOTCONT/$num_limitelot/\""
        echo "num_limitelot = $num_limitelot"
        curl -X GET "https://api.timci.com/api/Serveurs_Comptes_Params/$nums/SYSTEM/LIMITLOTCONT/$num_limitelot/"

fi

