#!/usr/bin/perl -w
# pcd2html module pcd2html_create_makefile
# Copyright Andreas Tille <tille@physik.uni-halle.de>
#
# This module creates Makefiles in each subdirectory from a rules file
use pcd2html_uti;

if ( OpenRules () ) { die "Error while opening rules file.\n" ; }

open(MAKEFILE, ">Makefile" ) || die "Unable to open Makefile\n" ;
select (MAKEFILE) ;

$_ = `pwd` ;
chomp ;
s/.*\/([^\/]*)$/$1/;
$dir = $_ ;

print "# Makefile in $dir created by pcd2html_create_makefile\n" ;

print "SOURCES= " ;

while ( <RULES> ) {
  unless ( /\s*#/ ) {
    chomp ;
    if ( /(\w*):extra/ ) {
      print "\\\n\t$1_extra " ;
      system("touch $1_extra.img");
    } else {
      /(\w*):(\d{3})/ ;
      print "\\\n\t$1_$2 " ;
      system("touch $1_$2.img");
    }
  }
}
close RULES ;

print "\n" ;
print ".SUFFIXES: .img .jpg .html\n\n" ;

print ".img.html:\n" ;
print "	\@${datadir}/pcd2html_create_html \$<\n\n" ;

print "all: \$(SOURCES:%=%.html) Makefile\n\n" ;

print "Makefile: rules\n" ;
print "	\@touch *.img\n" ;
print "	\@rm -rf *~\n" ;
print "	\@${datadir}/pcd2html_create_makefile\n\n" ;

print "\ncleanhtml:\n" ;
print "	\@rm -rf *.html *~\n\n" ;

print "clean:\n" ;
print "	\@rm -rf *.img *~\n" ;
print "	\@make cleanhtml\n\n" ;

print "distclean:\n" ;
print "	\@make clean\n" ;
print "	\@rm -rf *.img *.jpg Makefile\n" ;
  
select (STDOUT) ;
close(MAKEFILE) ;


