;;; -*- coding: latin-1 -*-
;;; This file contains the needed macro to make some thumbnails.
;;; Licence: GPL 
;;; Authors: Arnaud Legrand (idea, preliminary coding)
;;; Authors: Martin Quinson (proper, clean coding ;)

;;; There is two tags:
;;; <diapo file=..> makes a thumbnail of an image
;;; <diaporama dir=... gridargs> makes thumbnails of all images contained in dir.
;;;    gridargs: specification of the grid in which we put the thumbnails.
;;;      Example:
;;;      <diaporama dir="photos/Halloween" 
;;;                 layout=3x3 align=ccc valign=mmm border=0 spacing=5
;;;                 width='20%' />

;;;
;;; FAQ about diaporama
;;;  Q: How can I control the order in which the diapo are displayed?
;;;  A: The files are sorted alphabetically. Just adapt the filenames.
;;;
;;;  Q: How can I leave a cell empty?
;;;  A: Files of size 0 are not displayed, but use a cell. So, just touch a
;;;     ghost file with the right name.

#use wml::std::tags
#use wml::std::href
#use wml::std::grid

<define-tag diapo whitespace=delete>
  <preserve file prop><set-var %attributes>
  
   ;;; test the args
   <ifeq <get-var file /> "" 
      <compound>
	<warning "Argument 'file' mandatory in tag 'diapo'">
	<exit 1>
      </compound>
   />
  
  <set-var prop="<get-file-properties <get-var file /> />" />
;;;  <warning file=<get-var file /> size=<get-var prop[0] /> />
  <ifneq <get-var prop[0] /> 0
    <compound>
      <perl>{
        my $file;<perl:assign $file><get-var file/></perl:assign>
        my $output=makthn($file);
        <perl:print><href url="$file" image="$output"></perl:print> 
        1;
      }</perl>
    </compound>
  />
  <restore file prop>
</define-tag>

<perl>
sub makthn {
  my ($file) = shift;
  if ($file =~ /\.JPG$/i) {
    my ($ident) = `identify $file`; chomp($ident);
    $ident =~ /.* (\d*)x(\d*).*/;
    ($width,$height)=($1,$2);
  
    my ($dir) = $file;
    $dir =~ s/\/[^\/]*$//;
    system "mkdir -p thn/$dir";
    if (! -e "thn/$file") {
       if ($width>$height) {
          system "convert -scale 150 $file thn/$file";
       } else {
          system "convert -scale x150 $file thn/$file";
       }
    }
    return "thn/$file";
  } else {if ($file =~ /\.MPG$/i) {
    my ($output) = $file;
    $output=~ s/\.mpg$/.jpg/i;

    my ($dir) = $file;
    $dir =~ s/\/[^\/]*$//;
    system "mkdir -p thn/$dir";
    if (! -e "thn/$output") {
       system "mplayer -vo jpeg -jpeg outdir=/tmp/ -ao null -frames 1 $file >/dev/null 2>&1";
#       system "mplayer -vo jpeg -jpeg outdir=/tmp/ -ao null -frames 1 $file";
       system "mv /tmp/00000001.jpg thn/$output";
    }
    return "thn/$output";
  } else {
      print STDERR "$file ne semble pas etre une image (pas .jpg). Ignoré.\n";
      return 1;
  }}

}
</perl>

<define-tag diaporama endtag=none>
  <preserve list fich dir excl matching
            layout align valign width padding spacing border bgcolor color summary>
  <defvar matching ".*\\.[jJmM][pP][gG]">
  <set-var %attributes>
  <set-var list=<directory-contents <get-var dir /> matching=<get-var matching/> /> />
  <sort list caseless=true />
  <grid layout="<get-var layout />"
        align="<get-var align />" 
	valign="<get-var valign />"
	width="<get-var width />"
	padding="<get-var padding />"
	spacing="<get-var spacing />"
	border="<get-var border />"
	bgcolor="<get-var bgcolor />"
	color="<get-var color />"
	summary="<get-var summary />"
	>
  <foreach fich list>
     <ifneq "<get-var fich />" ""
       <compound> 
         <cell>;;;dir=<get-var dir/>;file=<get-var fich /></cell>;;;
	 <diapo file="<get-var dir/>/<get-var fich />" /></cell>
       </compound>
     />
   </foreach>
   </grid>
   <restore list fich dir excl matching
            layout align valign width padding spacing border bgcolor color summary>
</define-tag>

;;;
;;; size shouldn't be hardecoded
;;;
<define-tag downloadicon whitespace=delete>
  <preserve file fmt><set-var %attributes>
   ;;; test the args
   <ifeq <get-var file /> "" 
      <compound>
	<warning "Argument 'file' mandatory in tag 'downloadicon' (called from '<get-var fmt />')">
	<exit 1>
      </compound>
   />
   ;;; do the job
   <a href="<get-var file />"><img src="Utils/Icons/<get-var fmt />.png" width="24" height="26"></a>
  <restore file fmt>
</define-tag>

<define-tag ps whitespace=delete>
  <preserve file><set-var %attributes>
    <downloadicon file="<get-var file />" fmt="ps">
  <restore file>
</define-tag>

<define-tag pdf whitespace=delete>
  <preserve file><set-var %attributes>
    <downloadicon file="<get-var file />" fmt="pdf">
    (<perl>{
     my $file;<perl:assign $file><get-var file /></perl:assign>;
     my $cmd = `pdfinfo $file|grep Pages |sed 's/.* //'`;
     <perl:print>$cmd</perl:print>
     }</perl> pages)
  <restore file>
</define-tag>

<define-tag word-doc whitespace=delete>
  <preserve file><set-var %attributes>
   <a href="<get-var file />"><img src="Utils/Icons/word.jpeg" width="24" height="26"></a>
  <restore file>
</define-tag>