#!/usr/bin/awk -f
#
# info2web -- call hepu2fet_key appropriately
#
# usage: info2web webbase=/data/www galleries.info
#
# http://www.heikopurnhagen.net/software/info2web
#
# This program is available under the terms of the GNU General Public
# License (GPL). All usual disclaimers apply!
#
# Heiko Purnhagen   (mail@heikopurnhagen.net)
# HP 20080817
# HP 20090930 if DIRIDX copy and touch index.html if source newer

BEGIN {
  numinfo = 0;
  diridx = 0;
  webbase = "/tmp";
}

{
  if ($1 == "DIRIDX") {
    idxhepudir = $2;
    getline;
    idxtitle = $0;
    getline;
    idxcaption = $0;
    getline;
    idxyear = $0;
    getline;
    idxwebdir = $0;
    getline;
    idxthumb = $0;
    diridx = 1;
  }
  else if ($1 == "DIRKEY") {
    hepudir[numinfo] = $2;
    key[numinfo] = $3;
    getline;
    title[numinfo] = $0;
    getline;
    caption[numinfo] = $0;
    getline;
    srcdir[numinfo] = $0;
    getline;
    year[numinfo] = $0;
    getline;
    webdir[numinfo] = $0;
    getline;
    thumb[numinfo] = $0;
    numinfo++;
  }
}  

END {
  print "INFO2WEB diridx=" diridx " numinfo=" numinfo > "/dev/stderr";
  for (i=0; i<numinfo; i++) {
    if (key[i]=="") {
      hepuindex = hepudir[i] "/index.html";
      webindex = webbase "/" webdir[i] "/index.html";
      if (!system("test " hepuindex " -nt " webindex)) {
	system("hepu2fet_key " hepudir[i] ".rot " hepudir[i] " " webbase "/" webdir[i] " " year[i]);
      }
    }
    else {
      hepuindex = hepudir[i] "/index" key[i] ".html";
      webindex = webbase "/" webdir[i] "/index" key[i] ".html";
      if (!system("test " hepuindex " -nt " webindex)) {
	system("hepu2fet_key " hepudir[i] key[i] ".rot " hepudir[i] " " webbase "/" webdir[i] " " year[i] " " key[i]);
      }
    }
  }
  if (diridx) {
    if (!system("test " idxhepudir "/index.html -nt " webbase "/" idxwebdir "/index.html")) {
      print "COPYING " webbase "/" idxwebdir "/index.html" > "/dev/stderr";
      system("cp -a " idxhepudir "/index.html " webbase "/" idxwebdir "/index.html");
      system("touch " webbase "/" idxwebdir "/index.html");
    }
  }
}

#--