#!/usr/bin/perl

# Generate the Hips node list according to a list of HiPS directories
# provided by a simple ASCII file
# Author: P.Fernique [CDS]
# Version: 1.0 (April 2015) - compatible with HiPS v1.3
#
# => ADJUST THE FOLLOWING VARIABLES
#    ACCORDING TO YOUR HIPS NODE CONFIGURATION

# File containing the list of HiPS directories (one path per line)
$HIPS="/etc/hips.txt";

# Default hips_status if not present in the properties file
$DEFAULT_STATUS = "public master clonableOnce";

# HiPS pathname prefix to be removed for building the hips_service_url)
# if not present in the properties file
# => hips_service_url = $URL_PREFIX/$PATH_SUFFIX
$PATH_PREFIX = "/var/www";

# HiPS node URL prefix (without ending "/")
$URL_PREFIX = "http://your.hips.node";

# ------------ DO NOT MODIFIED AFTER THIS LINE ------------------

# Extract publisher_did, hips_release_date, hips_service_url, hips_status
# from $path/properties file
sub getProp {
   my($path)=@_;
   my($did,$date,$url,$status,$pdid,$creator,$obs,$publisher);
   (open P,"$path/properties") || (return undef);
   while(<P>) {
      chomp;
      $did=$1       if /^creator_did\s*=\s*(.*)/;
      $creator=$1   if /^creator_id\s*=\s*(.*)/;
      $obs=$1       if /^obs_id\s*=\s*(.*)/;
      $pdid=$1      if /^publisher_did\s*=\s*(.*)/;
      $publisher=$1 if /^publisher_id\s*=\s*(.*)/;
      $date=$1      if /^hips_release_date\s*=\s*(.*)/;
      $url=$1       if /^hips_service_url\s*=\s*(.*)/;
      $status=$1    if /^hips_status\s*=\s*(.*)/;
   }
   close P;
   
   # Support for old and new identication possibilities
   if( !defined $did ) {
     if( defined $pdid ) { $did=$pdid; }
     elsif( defined $obs ) {
	   if( defined $creator ) { $did = $creator."?".$obs; }
	   elsif( defined $publisher ) { $did = $publisher."?".$obs; }
     }
   }
   
   return ($did,$date,$url,$status);
}

# Find hips_release_date directly from mtime of $path/properties file
sub createDate {
   my($path)=@_;
   my($mtime,$date);
   
   $mtime = (stat("$path/properties"))[9];
   return undef if !defined $mtime;
   my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($mtime);
   $date=sprintf("%04d-%02d-%02dT%02d:%02dZ",($year+1900),($mon+1),$mday,$hour,$min);
   return $date;
}

# Find hips_service_url directly from the $path and the generic $URLPREFIX
sub createUrl {
   my($path)=@_;
   my($url);
   my($s1)=$URL_PREFIX;
   $s1=$s1."/" if $s1!~/\/$/;
   my($s2)=$PATH_PREFIX;
   $s2=$s2."/" if $s2!~/\/$/;
   $path=~s/\\/\//g;
   $s2=~s/\\/\//g;
   return undef if index($path,$s2)!=0;
   return $s1 . substr($path,length($s2) );
}

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime();
$DATE=sprintf("%04d-%02d-%02dT%02d:%02dZ",($year+1900),($mon+1),$mday,$hour,$min);

# HTTP header
print "Content-type: text/plain\n\n";
#print "# Hipslist of $URL_PREFIX HiPS node\n# Date: $DATE\n# Do not copy any HIPS with unclonable status !\n\n";
print "# Hipslist of $URL_PREFIX HiPS node\n# Date: $DATE\n# Do not mirror any HIPS without getting before the copyright owner agreement\n# Even with this agreement, do not mirror any HiPS with unclonable status !\n\n";


# Browse the HiPS directory list found in $HIPS file
open F,$HIPS;

while(<F>) {
   chomp;
   $path = $_;
   
   # Comment or blank line ?
   next if /^#/ || /^\s*$/;
   
   # Trailing '/' => removed
   $path=~s/\/$//;
   
   # Get properties from the "properties" file
   ($did,$date,$url,$status) = &getProp($path);
   
   # Check the identifier
   if( !defined $did ) {
      print "#*** creator_did unknown for $path => HiPS ignored\n\n";
      next;
   }
   if( $did!~/^ivo:\/\// ) {
      print "#*** Not IVOID creator_did [$did] for $path => HiPS ignored\n\n";
      next;
   }
   
   # Try to complete missing properties (if possible)
   if( !defined $date ) { $date = &createDate($path); }

   if( !defined $url )  { $url  = &createUrl($path); }

   if( !defined $url )  {
      print "#*** hips_service_url unknown for $path => HiPS ignored\n\n";
      next;
   }
   if( !defined $status ) { $status = $DEFAULT_STATUS; }
 
   # Generate the record
   my($rec);
   $rec .= sprintf("%-20s = %s\n","creator_did",$did);
   $rec .= sprintf("%-20s = %s\n","hips_release_date",$date);
   $rec .= sprintf("%-20s = %s\n","hips_service_url",$url);
   $rec .= sprintf("%-20s = %s\n","hips_status",$status);
   print "$rec\n";
}

