#!/bin/sh

# $Id: paypal_functions,v 1.21 2013/08/21 21:46:16 gilles Exp gilles $

paypal_prerequisites() {
	perl -mMIME::Lite -e '' || echo 'sudo aptitude install libmime-lite-perl'
	perl -mMIME::Parser -e '' || echo 'sudo aptitude install libmime-tools-perl'
	perl -mUnicode::MapUTF8 -e '' || echo 'sudo aptitude install libunicode-maputf8-perl'
}

paypal_init_laposte() {
	user=gilles.lamiral
	passfile=/g/var/pass/secret.gilles_laposte
	host=imap.laposte.net
	tmpdir=/g/var/paypal_reply
	folder=INBOX
}

paypal_init_petite() {
	user=gilles@est.belle
	passfile=/g/var/pass/secret.gilles_mbox
	host=p
	tmpdir=/g/var/paypal_reply
	folder='INBOX.03_imapsync.imapsync_paypal'
}

paypal_init_petite_INBOX() {
	user=gilles@est.belle
	passfile=/g/var/pass/secret.gilles_mbox
	host=p
	tmpdir=/g/var/paypal_reply
	folder='INBOX'
}


paypal_init_petite_dev() {
	user=gilles@est.belle
	passfile=/g/var/pass/secret.gilles_mbox
	host=p
	tmpdir=/g/var/paypal_reply_dev
	folder='INBOX.03_imapsync.imapsync_paypal_dev'
}


get_mail() {
	# creation des répertoires
	mkdir -p $tmpdir/msg_in/
	mkdir -p $tmpdir/msg_id/
	(
	cd $tmpdir/msg_in/
	# recuperation des messages de la boite sans destruction des messages
	# transférés
	paypal_imapget --host $host --user $user --passfile $passfile \
	         --folder $folder
	)
}

get_mail_PP1470() {
	# creation des répertoires
	mkdir -p $tmpdir/msg_in/
	mkdir -p $tmpdir/msg_id/
	(
	cd $tmpdir/msg_in/
	# recuperation des messages de la boite sans destruction des messages
	# transférés
	paypal_imapget --host $host --user $user --passfile $passfile \
	         --folder $folder --search TEXT --search PP1470
	)
}


extract_mail() {
	mkdir -p $tmpdir/msg_out/
	test -z "`ls $tmpdir/msg_in/`" && echo no mail && return
	(
	cd  $tmpdir/msg_out/
	test -z "`ls .`" || rm -rf *_d
	paypal_mimeexplode ../msg_in/*
	)
	#ls -d $tmpdir/msg_out/
}


convert_utf8() {
	mkdir -p $tmpdir/msg_out_utf8/
	test -z "`ls $tmpdir/msg_out/*_d/*.txt`" && echo no mail && return
	for f in $tmpdir/msg_out/*_d/*.txt; do
		b=`basename "$f"`
		d=`dirname  "$f"`
		bd=`basename "$d"`
		d_utf8="$tmpdir/msg_out_utf8/$bd"
		f_utf8="$d_utf8/$b"
		test -d "$d_utf8" && continue
		mkdir  "$d_utf8"
		if file "$f"  | grep -i UTF-8 > /dev/null
		then
			echo copying "$f" to "$f_utf8"
			cp "$f" "$f_utf8"
		else
			echo converting "$f" to "$f_utf8"
			8859_utf8 "$f" > "$f_utf8"
		fi
	done
}


troncate_last_2_chars() {
        length=`expr length "$1"`
        length_2=`expr $length - 2`
        expr substr "$1" 1 $length_2

}

build_reply() {
	mkdir -p $tmpdir/msg_reply/
	test -z "`ls $tmpdir/msg_out/`" && echo no mail && return
	for f in $tmpdir/msg_out_utf8/*/*.txt; do
		#echo "$f"
		d=`dirname  "$f"`
		bd=`basename "$d"`
		file_id=`troncate_last_2_chars $bd`
		d_reply="$tmpdir/msg_reply/$file_id"
		test -s "$d_reply/$file_id.txt" && continue
		mkdir -p "$d_reply"
		echo building "$d_reply/$file_id.txt"
		echo paypal_build_reply "$f" "$tmpdir/msg_id/$file_id"
		paypal_build_reply "$f" "$tmpdir/msg_id/$file_id" > "$d_reply/$file_id.txt"
                test -s "$d_reply/$file_id.txt" || rm "$d_reply/$file_id.txt"
	done
}

build_reply_arg() {
	for f in "$@"; do
		#echo "$f"
		d=`dirname  "$f"`
		bd=`basename "$d"`
		file_id=`troncate_last_2_chars $bd`
		d_reply="$tmpdir/msg_reply/$file_id"
		echo building "$d_reply/$file_id.txt"
		echo paypal_build_reply "$f" "$tmpdir/msg_id/$file_id"
		paypal_build_reply "$f" "$tmpdir/msg_id/$file_id"
	done
}


build_invoice_arg() {
	for f in "$@"; do
		#echo "$f"
		d=`dirname  "$f"`
		bd=`basename "$d"`
		file_id=`troncate_last_2_chars $bd`
		d_invoice="$tmpdir/invoices/$file_id"
		echo building "$d_invoice/imapsync_var.tex"
		echo paypal_build_invoice_from_email "$f" "$tmpdir/msg_id/$file_id"
		paypal_build_invoice_from_email "$f" "$tmpdir/msg_id/$file_id"
	done
}



debug_mode() {
        return 0
        return 1
}

send_reply() {
	mkdir -p $tmpdir/msg_sent/
	test -z "`ls $tmpdir/msg_reply/*/*.txt`" && echo no mail && return
	for f in $tmpdir/msg_reply/*/*.txt; do
		b=`basename "$f"`
		d=`dirname  "$f"`
		bd=`basename "$d"`
		d_sent="$tmpdir/msg_sent/$bd"
		test -f "$d_sent/$b" && continue
		mkdir -p "$d_sent"
                debug_mode && echo "paypal_send $f"
		test X"--send" = X"$1" && paypal_send --send --subscribe "$f" && touch "$d_sent/$b"
		#test X"--send" = X"$1" && touch "$d_sent/$b"
		test X"" = X"$1" && paypal_send "$f" 
	done
	#mailq
}

paypal_all() {
        paypal_prerequisites
	echo "Will get messages in $tmpdir/msg_in/"
	get_mail
	get_mail_PP1470
	echo "Done get messages in $tmpdir/msg_in/"
	echo "Will extract_mail in $tmpdir/msg_out/"
	extract_mail
	echo "Done extract_mail in $tmpdir/msg_out/"
	echo "Will converting to utf8 in $tmpdir/msg_out_utf8/"
	convert_utf8
	echo "Done converting to utf8 in $tmpdir/msg_out_utf8/"
	echo "Will build_reply in $tmpdir/msg_reply/"
	build_reply
	echo "Done build_reply in $tmpdir/msg_reply/"
	echo "Will send_reply $@"
	send_reply "$@"
	echo "Done send_reply $@"
}

#echo 'paypal_reply_petite'
paypal_reply_petite() {
	echo "Doing paypal_reply_petite"
	echo paypal_init_petite
	paypal_init_petite
	paypal_all "$@"
	echo paypal_init_petite_INBOX
	paypal_init_petite_INBOX
	paypal_all "$@"
	echo "Done paypal_reply_petite"
}

#echo 'paypal_reply_laposte'
paypal_reply_laposte() {
	echo "Doing paypal_reply_laposte"
	echo paypal_init_laposte
	paypal_init_laposte
	paypal_all "$@"
	echo "Done paypal_reply_laposte"
}

paypal_all_dev() {
        paypal_prerequisites
	echo "Will get messages in $tmpdir/msg_in/"
        get_mail
	get_mail_PP1470
	echo "Done get messages in $tmpdir/msg_in/"
	echo "Will extract_mail in $tmpdir/msg_out/"
	extract_mail
	echo "Done extract_mail in $tmpdir/msg_out/"
	echo "Will converting to utf8 in $tmpdir/msg_out_utf8/"
	convert_utf8
	echo "Done converting to utf8 in $tmpdir/msg_out_utf8/"
	echo "Will build_reply in $tmpdir/msg_reply/"
	build_reply
	echo "Done build_reply in $tmpdir/msg_reply/"
	echo "Will send_reply $@"
	send_reply "$@"
	echo "Done send_reply $@"
}


paypal_reply_petite_dev() {
	echo "Doing paypal_reply_petite_dev"
	echo paypal_init_petite_dev
	paypal_init_petite_dev
	paypal_all_dev "$@"
	echo "Done paypal_reply_petite_dev"
}

