#! /bin/bash

##############################################################################
#                     Copyrighting many pictures.                            #
##############################################################################
#     This software is under GNU GPL                                         #
#                                                                            #
#     (c) 2005 - Olivier ROBERT                                              #
#     (c) 2005 - Yoann Moulin                                                #
#                                                                            #
# Need : imagemagick and gsfonts                                             #
##############################################################################

if [ -f ~/.copyright.conf ] ; then
  . ~/.copyright.conf
elif [ -f /etc/copyright.conf ] ; then
  . /etc/copyright.conf
else
  echo "No Configuration file found, create ~/.copyright.conf or /etc/copyright.conf"
	exit 1
fi

if [ -z "$1" ] ; then
  echo "$0 : too few argument, need file names to copyright."
  exit 1
fi

if [ "$orientation1" == "vertical" ] ; then
  rotate11='-rotate 90'
  rotate12='-rotate -90'
fi

if [ -n "$rotate11" ] ; then 
 
  if [ "$Xgravity1" == "bas" ] && [ "$Ygravity1" == "droite" ] ; then
    gravity1="gravity southwest"

  elif [ "$Xgravity1" == "bas" ] && [ "$Ygravity1" == "gauche" ] ; then
    gravity1="gravity northwest"

  elif [ "$Xgravity1" == "haut" ] && [ "$Ygravity1" == "droite" ] ; then
    gravity1="gravity southeast"

  elif [ "$Xgravity1" == "haut" ] && [ "$Ygravity1" == "gauche" ] ; then
    gravity1="gravity northeast"

  fi

else 

  if [ "$Xgravity1" == "bas" ] && [ "$Ygravity1" == "droite" ] ; then
    gravity1="gravity southeast"

  elif [ "$Xgravity1" == "bas" ] && [ "$Ygravity1" == "gauche" ] ; then
    gravity1="gravity southwest"

  elif [ "$Xgravity1" == "haut" ] && [ "$Ygravity1" == "droite" ] ; then
    gravity1="gravity northeast"

  elif [ "$Xgravity1" == "haut" ] && [ "$Ygravity1" == "gauche" ] ; then
    gravity1="gravity northwest"

  fi

fi

if [ "$orientation2" == "vertical" ] ; then
  rotate21='-rotate 90'
  rotate22='-rotate -90'
fi

if [ -n "$rotate21" ] ; then 
 
  if [ "$Xgravity2" == "bas" ] && [ "$Ygravity2" == "droite" ] ; then
    gravity2="gravity northwest"

  elif [ "$Xgravity2" == "bas" ] && [ "$Ygravity2" == "gauche" ] ; then
    gravity2="gravity southwest"

  elif [ "$Xgravity2" == "haut" ] && [ "$Ygravity2" == "droite" ] ; then
    gravity2="gravity northeast"

  elif [ "$Xgravity2" == "haut" ] && [ "$Ygravity2" == "gauche" ] ; then
    gravity2="gravity southeast"

  fi

else 

  if [ "$Xgravity2" == "bas" ] && [ "$Ygravity2" == "droite" ] ; then
    gravity2="gravity southeast"

  elif [ "$Xgravity2" == "bas" ] && [ "$Ygravity2" == "gauche" ] ; then
    gravity2="gravity southwest"

  elif [ "$Xgravity2" == "haut" ] && [ "$Ygravity2" == "droite" ] ; then
    gravity2="gravity northeast"

  elif [ "$Xgravity2" == "haut" ] && [ "$Ygravity2" == "gauche" ] ; then
    gravity2="gravity northwest"

  fi

fi

if [ ! -d $DIR ] ; then
  mkdir $DIR
fi

echo "copyrighting ..."

function setcopyright(){
  read picture
  convert "$picture" $font $rotate11 \
          $fill11 -draw "$gravity1 font-size $size1 text $xposition,$yposition '$COPYRIGHT'" \
          $fill12 -draw "$gravity1 font-size $size1 text `echo "$xposition+1"| bc`,`echo "$yposition+1" | bc` '$COPYRIGHT'" \
          $rotate12 $border $border_size -quality  $quality "$DIR/$picture"

  if [ $ref_switch == "on" ] ; then
    convert "$DIR/$picture" $font $rotate21 \
          $fill21 -draw "$gravity2 font-size $size2 text $xposition,$yposition '$REFERENCE'" \
          $fill22 -draw "$gravity2 font-size $size2 text `echo "$xposition+1"| bc`,`echo "$yposition+1" | bc` '$REFERENCE'" \
          $rotate22 -quality  $quality "$DIR/$picture"
  fi

}	

if [ -n "$debug" ] ; then
  echo "Reference switch = $ref_switch"
	echo "Copyright = $COPYRIGHT"
	echo "Reference = $REFERENCE"
fi

for picture in `ls $@`; do
  [ -n "$debug" ] && echo -n "$picture ... " 
  echo $picture | setcopyright
  [ -n "$debug" ] && echo "done" 
done
  echo "done."

