#! /bin/bash


GLOBALFILE=
while true
do
  case $1 in
    -f) GLOBALFILE=$2
		  shift 2
		  ;;
     *)
		break
		;;
  esac
done

if [ -f <CONFIGDIR>/$GLOBALFILE ]
then
	. <CONFIGDIR>/$GLOBALFILE
elif [ -n "$GLOBALFILE" ]
then
	echo "<CONFIGDIR>/$GLOBALFILE introuvable !"
	exit 1
elif [ -f <CONFIGDIR>/globals_ZEN.sh ]
then
	. <CONFIGDIR>/globals_ZEN.sh
else
	. <CONFIGDIR>/globals.sh
fi

case $DEBUG in
  1) set -x
     ;;
  *)
     ;;
esac

date=`date '+%Y-%m-%d'`
timestamp=`date '+%Y-%m-%d-%H-%M'`
export LOGFILE=${LOGDIR}/${APPLICATION}_${date}.log
MAILDIR=$LOGDIR/mailings

error() {
		echo "ERROR:mailer.sh:$date: $1" 1>&2
		echo "ERROR:mailer.sh:$date: $1" >> $LOGFILE
}

exec_sql() {
	exec-sql.sh -t 30 -H ${DBHOST} -d ${DBNAME} -u "${DBUSER}" -p "${DBPWD}" -l $LOGFILE -c "$1" | dos2unix -f -- 
	if [ $? -ne 0 ]
		then
		error "cannot execute '$1' on ${DBNAME}@${DBHOST}"
	fi
}


TMPDIR=/tmp/`whoami`
mkdir -p ${TMPDIR}
if [ $? -ne 0 ]
then
	error "cannot mkdir -p ${TMPDIR}"
	exit 1
fi

cd $TMPDIR

LOCKFILE=${TMPDIR}/`basename $0 .sh`_${APPLICATION}.lock

if [ -f ${LOCKFILE} ]
then
	error "$0 already running ..."
	exit 1
fi

# BEGIN
touch ${LOCKFILE}

WGET="wget -q -O -"

for line in `exec_sql "SELECT DISTINCT on (a2.uid) a1.uid AS affectation_assistant, a2.uid AS affectation_commercial, u.login, u.pwd, a2.ref_departement \
FROM employe com \
INNER JOIN affectation a2 ON (a2.ref_employe=com.uid AND a2.ref_statut=0 AND a2.fin IS NULL) \
INNER JOIN est_assiste_par eap ON (eap.ref_est_assiste_employe=com.uid AND eap.ref_statut=0 AND eap.fin IS NULL AND eap.ref_departement=a2.ref_departement) \
INNER JOIN userid u ON (u.ref_personne_physique = com.uid AND u.ref_statut=0) \
INNER JOIN employe assi ON (eap.ref_assiste_employe = assi.uid AND assi.ref_statut=0) \
INNER JOIN affectation a1 ON (a1.ref_employe = assi.uid AND a1.ref_statut=0 AND a1.fin IS NULL AND a1.ref_fonction=3 AND a1.ref_departement=a2.ref_departement)"`
  do
  uid_affectation_assi=`echo $line | cut -d'|' -f1`
  uid_affectation_com=`echo $line | cut -d'|' -f2`
  login=`echo $line | cut -d'|' -f3`
  pwd=`echo $line | cut -d'|' -f4`
  uid_departement=`echo $line | cut -d'|' -f5`
  
  url_commercial="http://$login:$pwd@localhost:9673/$ZOPEPRODUCT/${uid_departement}/${uid_affectation_assi}/${uid_affectation_com}"
  line=`$WGET "${url_commercial}/get_mailing"`
  if [ -n "$line" ]
	  then
	  rm -f message.html
	  url_commercial=`echo $line | cut -d'|' -f1`
	  id_content=`echo $line | cut -d'|' -f2`

	  $WGET --http-user=${login} --http-passwd=${pwd} "${url_commercial}/${id_content}" > message.html
	  
	  if [ -s message.html ]
		  then
		  id_mail_list=`echo $line | cut -d'|' -f3`
		  $WGET --http-user=${login} --http-passwd=${pwd} "${url_commercial}/${id_mail_list}" | grep -v '^[[:space:]]*$' > mails.lst
		  
		  if [ -s mails.lst ]
			  then
			  subject=`echo $line | cut -d'|' -f4`
			  from=`echo $line | cut -d'|' -f7`
			  fileuid=`echo $line | cut -d'|' -f6`

			  if [ -n "$fileuid" ]
				  then
				  query="SELECT ztitle, source FROM file_dossier WHERE uid=${fileuid}"
				  echo "query = $query" >> $LOGFILE
				  line=`exec_sql "$query"`
				  filename="`echo $line | cut -d'|' -f1`"
				  source="$UPLOADFILESDIR/`echo $line | cut -d'|' -f2`"
				  cp "$source" "./$filename"
			  fi

			  # record mailing
			  mailingdir=$MAILDIR"/mailing_"$timestamp"_"$uid_affectation_com
			  mkdir -p $mailingdir
			  cp mails.lst message.html $mailingdir
			  touch $mailingdir/meta.txt
			  echo -e "subject :\n $subject" >> $mailingdir/meta.txt
			  echo -e "from :\n $from" >> $mailingdir/meta.txt
			  echo -e "login :\n $login" >> $mailingdir/meta.txt
			  echo -e "command :\n mailing -f mails.lst -l $LOGFILE \
 -e $from -s $subject -m message.html -c utf-8 -H $SMTPHOST\n" >> $mailingdir/meta.txt

			  # send mailing
			  echo "`date` : envoi mailing pour $from" >> $LOGFILE
			  if [ -n "$fileuid" ]
				  then
				  mailing -f mails.lst -l $LOGFILE -e "$from" -s "$subject" -m message.html -C "utf-8" -H $SMTPHOST -a ./"$filename"
			  else
				  mailing -f mails.lst -l $LOGFILE -e "$from" -s "$subject" -m message.html -C "utf-8" -H $SMTPHOST
			  fi
			  echo "-end-" >> $LOGFILE
			  echo "mailing de $from envoyé (voir $mailingdir)" >> $LOGFILE
		  else
			  error "mail list empty for $login"
		  fi
	  fi
  fi
done

cd - > /dev/null 2>&1

# END
rm -f ${LOCKFILE}
exit $err

