#!/bin/sh
set -e

PKG=`basename $0 | sed -e 's/\.[^.]*$//'`
BASE=`echo $PKG | sed "s/^\(.*\)-[0-9]\+x[0-9]\+/\1/"`

IMGSRC=/var/lib/${BASE}
IMGTARGET=/usr/share/wallpapers/at

WIDTH=16
HEIGHT=9

A1WIDTH=5
A1HEIGHT=4
A1LTERNATIVE="${BASE}-1280x1024"
A2WIDTH=4
A2HEIGHT=3
A2LTERNATIVE="${BASE}-1600x1200"

check_ratio () {
  # check whether $1 x $2 has ration $WIDTH x $HEIGHT
  j=$(($2/$HEIGHT))
  jxh=$(($j*$HEIGHT))
  if [ $2 -ne $jxh ] ; then
     echo $2 is no multiple of $HEIGHT.
     ratiotest=-1
     return
  fi
  jxw=$(($j*$WIDTH))
  if [ $1 -ne $jxw ] ; then
     echo $1 x $2 has not ratio $WIDTH x $HEIGHT.
     ratiotest=-2
     return
  fi
  ratiotest=0
}

if  [ ! -d ${IMGSRC} ] ; then
   cat <<...
Directory ${IMGSRC} does not exist.
Reconfiguration of package $PKG is not possible
because the image source were deleted to save disk space.
Thus you have to reinstall the package $PKG to obtain the image sources back.
...
exit
fi

. /usr/share/debconf/confmodule

if [ -d ${IMGTARGET} ] ; then
   rm -f ${IMGTARGET}/*.jpg
else
   mkdir -p ${IMGTARGET}
fi

# get screen resolution from debconf database
db_get ${BASE}/resolution

w=`echo $RET | sed "s/^\([0-9]\+\)x\([0-9]\+\)/\1/"`
h=`echo $RET | sed "s/^\([0-9]\+\)x\([0-9]\+\)/\2/"`
if [ "${w}x${h}" != $RET ] ; then
   cat <<...
$RET is no valid screen resolution string.  Screen resoltions should be
given as WIDTHxHEIGHT.  Please use
     dpkg-reconfigure $PKG
to finish installation process.
...

else
  # if a correct resolution string was given ...
  case "$RET" in
    "1600x1200")
	cp -a ${IMGSRC}/*.jpg ${IMGTARGET}
      ;;
    *)
        check_ratio $w $h
        if [ $ratiotest -ne 0 ] ; then
           echo "You submitted a screen resolution which is not optimally supported by this package."
           WIDTH=$A1WIDTH
           HEIGHT=$A1HEIGHT
           check_ratio $w $h
           if [ $ratiotest -eq 0 ] ; then
	      echo "The package $A1LTERNATIVE supports your screen resolution perfectly."
           else
              WIDTH=$A2WIDTH
              HEIGHT=$A2HEIGHT
              check_ratio $w $h
              if [ $ratiotest -eq 0 ] ; then
                 echo "The package $A2LTERNATIVE supports your screen resolution perfectly."
	      else
                 cat <<...
Please note that the images might not fit your screen optimal.
If you want optimal sized images just write a friendly E-Mail to
    Andreas Tille <tille@debian.org>
and tell him that it would be nice to support screen size $RET
in his wallpapers-at package.
...
	      fi
	   fi
        fi

        if [ ! -x /usr/bin/convert ] ; then
	    echo "Installation of imagemagick package might be not finished."
	    echo "Please check Installation of this package and try to install ${PKG} again."
	    exit -1
	fi
	cd ${IMGSRC}
        echo "Rescaling the images.  This may take a while ..."
	for img in `ls *.jpg` ; do
	    /usr/bin/convert -geometry ${RET}! ${img} ${IMGTARGET}/${img}
	    rm -f ${img}
	done
      ;;
  esac
  rm -rf ${IMGSRC}
fi

#DEBHELPER#
