#!/bin/bash

cd ~/conky/calendrier

function exist_mail_conf
{
if [[ ! -f "mail.conf" ]] && [[ ! -f "mail.conf.cpt" ]]
	then
		popconf=$(yad --form \
    			--title="Decryption" \
				--image "dialog-password" --on-top \
    			--text="<b>Entrez les paramètres de votre boîte mail\net la clé d\'Encryptage</b>" --text-align="center" \
				--field="Nom du Serveur POP ": \
				--field="Nom d\'Utilisateur ": \
				--field="Mot de Passe : ":H \
				--field="Clé d\'encryptage: ":H \
    			--field="Confirmer la clé: ":H \
				--buttons-layout=spread \
    			--separator="|")
		ret=$?
		[[ $ret -eq 1 ]] && exit 0

		ServerName=$(echo "$popconf" | cut -d "|" -f1)
		UserName=$(echo "$popconf" | cut -d "|" -f2)
		Password=$(echo "$popconf" | cut -d "|" -f3)
		Encrypt1=$(echo "$popconf" | cut -d "|" -f4)
		Encrypt2=$(echo "$popconf" | cut -d "|" -f5)

		echo "host=$ServerName" > ~/conky/calendrier/mail.conf
		echo "user=$UserName" >> ~/conky/calendrier/mail.conf
		echo "pass=$Password" >> ~/conky/calendrier/mail.conf

		if [ $Encrypt1 = $Encrypt2 ]
			then
				ccrypt -e -K $Encrypt1 mail.conf > /dev/null
			else
				yad --text-align="center" --text="<span font_desc=\"Arial bold italic 14\">Les clés de saisie\nne correspondent pas</span>" --timeout=3 --title="Encryptage" --image=dialog-error --no-buttons --undecorated --fixed --borders=5
				fmail_crypt
		fi
fi
}

function fmail_crypt
{
for i in mail.*
	do
		name=`basename "$i"`
		ext=`echo "$name" |  awk -F. '{print $NF}'`
		name_alone=`echo "$name" |  awk -F. '{print $1}'`
done
if [ $ext = cpt ]
	then
## Decrypt mail.conf
		cfgpass=`yad --form \
    		--title="Décryptage mail.conf" \
			--image "dialog-password" --on-top \
    		--text="<b>Entrez la clé de Décryptage</b>" \
			--field="Clé de décryptage: ":H \
			--buttons-layout=spread \
    		--separator="|"`

		p=`echo "$cfgpass" | cut -d "|" -f1`
		ccrypt -d -K $p $name > /dev/null
#		echo $?
		var=$? 
		if [ $var == 4 ]
			then
				yad --text-align="center" --text="<span font_desc=\"Arial bold italic 14\">La clé de saisie\nne correspond pas</span>" --timeout=3 --title="Décryptage" --image=dialog-error --no-buttons --undecorated --fixed --borders=5
				fmail_crypt
		fi				
	else
## Encrypt mail.conf
		cfgpass=`yad --form \
    		--title="Encryptage mail.conf" \
			--image "dialog-password" --on-top \
    		--text="<b>Entrez la clé d\'encryptage</b>" \
			--field="Clé d\'encryptage: ":H \
    		--field="Confirmer la clé: ":H \
			--buttons-layout=spread \
    		--separator="|"`

		p=`echo "$cfgpass" | cut -d "|" -f1`
		pr=`echo "$cfgpass" | cut -d "|" -f2`

		if [ $p = $pr ]
			then
				ccrypt -e -K $p $name > /dev/null
			else
				yad --text-align="center" --text="<span font_desc=\"Arial bold italic 14\">Les clés de saisie\nne correspondent pas</span>" --timeout=3 --title="Encryptage" --image=dialog-error --no-buttons --undecorated --fixed --borders=5
				fmail_crypt
		fi
    for i in mail.*
      do
        name=`basename "$i"`
        ext=`echo "$name" |  awk -F. '{print $NF}'`
        name_alone=`echo "$name" |  awk -F. '{print $1}'`
    done
    if [ $ext = cpt ]
      then
				yad --text-align="center" --text="<span font_desc=\"Arial bold italic 14\">Le fichier mail.conf\na bien été crypté</span>" --timeout=3 --title="Décryptage" --image=dialog-information --no-buttons --undecorated --fixed --borders=5 &
      else
        exist_mail_conf
    fi
fi
}

exist_mail_conf
fmail_crypt

exit 0
