#!/bin/bash

cd ~/conky/calendrier

function exist_mail_conf
{
if [[ ! -f "mail.conf" ]] && [[ ! -f "mail.conf.cpt" ]]
	then
		popconf=$(yad --form \
    			--title="Mailbox Settings" \
				--image "dialog-password" --on-top \
    			--text="<b>Enter the settings for your mailbox\nand the Encryption Key</b>" --text-align="center" \
				--field="POP Server Name ": \
				--field="User Name ": \
				--field="Password : ":H \
				--field="Encryption key: ":H \
    			--field="Confirm key: ":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\">Key input\ndoes not match</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
## Decryption mail.conf
		cfgpass=`yad --form \
    		--title="Decryption mail.conf" \
			--image "dialog-password" --on-top \
    		--text="<b>Enter the Decryption key</b>" \
			--field="Decryption key: ":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\">The key input\ndoes not match</span>" --timeout=3 --title="Décryptage" --image=dialog-error --no-buttons --undecorated --fixed --borders=5
				fmail_crypt
		fi				
	else
## Encryption mail.conf
		cfgpass=`yad --form \
    		--title="Encryption mail.conf" \
			--image "dialog-password" --on-top \
    		--text="<b>Enter the Encryption key</b>" \
			--field="Encryption key: ":H \
    		--field="Confirm key: ":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\">Key input\ndoes not match</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\">The file mail.conf\nwas encrypted</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
