#! /usr/bin/perl

$ARGC = @ARGV;
if ($ARGC < 1)  {
  die " Need an NMM-Version \n ./release-script <NMM-Version> (e.g. 0.2.1)\n";
}

#! The 1. argument is the version
$NMM_VERSION = $ARGV[0];

@NMM_VERSION_NUMBERS = split /\./, $NMM_VERSION;

$test = $ARGV[1];

$prefix;
if ($test eq "test") {
  $prefix = "/home/repplix/download/www";
} else {
  $prefix = "/share/www";
}

$NMM_dir              = "$prefix/NMM";

$NMM_home             = "$prefix/NMM/s_en_index.html";

$GRAPHICS_home        = "$prefix/s_en_index.html";

$NMM_download_site    = "$prefix/NMM/Download/s_en_index.html";

$NMM_news_site        = "$prefix/NMM/News/s_en_index.html";

$NMM_external_libs_site = "$prefix/NMM/Docs/nmm-$NMM_VERSION-external-libs.html";

$NMM_online_docu      = "$prefix/NMM/Docs/s_en_index.html";

$NMM_project_links    = "$prefix/NMM/Links/s_en_index.html";


#some temporary files
$TMP_NMM_home             = "$prefix/NMM/s_en_index.html.tmp";

$TMP_GRAPHICS_home        = "$prefix/s_en_index.html.tmp";

$TMP_download_site    = "$prefix/NMM/Download/s_en_index.html.tmp";

$TMP_news_site        = "$prefix/NMM/News/s_en_index.html.tmp";

$TMP_external_libs_site = "$prefix/NMM/Docs/nmm-$NMM_VERSION-external-libs.html.tmp";

$TMP_online_docu      = "$prefix/NMM/Docs/s_en_index.html.tmp";




if ( $ARGV[1] eq "undo" ) {
  system("cp $NMM_home.bak $NMM_home");
  system("cp $GRAPHICS_home.bak $GRAPHICS_home");
  system("cp $NMM_download_site.bak $NMM_download_site");
  system("cp $NMM_news_site.bak $NMM_news_site");
  system("cp $NMM_external_libs_site.bak $NMM_external_libs_site");
  system("cp $NMM_online_docu.bak $NMM_online_docu");
  system("cp $NMM_project_links.bak $NMM_project_links");
  system("cp $NMM_project_links.bak $NMM_project_links ");
  system("cp $NMM_dir/Download/mmboxrc_sample.bak $NMM_dir/Download/mmboxrc_sample");

  die "Undo the last website update \n";
}





$day = (localtime(time))[3] ;
$month = (localtime(time))[4] + 1;     # range of this value is between 0..11
$year = (localtime(time))[5] + 1900;   # value is relative to 1900

$change_log = "<!-- CHANGE_LOG_URL -->";
$change_log_url = "<br><i>NMM</i> <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/ChangeLog\">(ChangeLog)</a>";

$download_url_tag = "<!-- NMM_DOWNLOAD_URL -->";
$download_url_string = "<li><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION.tar.gz\">nmm-$NMM_VERSION.tar.gz</a> (release $year-$month-$day) <a href=\"http://graphics.cs.uni-sb.de/NMM/News/announcement_$NMM_VERSION_NUMBERS[0]_$NMM_VERSION_NUMBERS[1]_$NMM_VERSION_NUMBERS[2].txt\">release announcement</a> </li>";#

$api_url_tag = "<!-- NMM_API_URL -->";
$api_url_string = "<li> <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-api-html.tar.gz\">nmm-$NMM_VERSION-api-html.tar.gz</a></li>";

$binary_packages = "<i>Binary Packages:</i>";
$binary_packages_link = "<li> <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-binaries-gcc2-i686.tar.gz\"> NMM $NMM_VERSION, compiled for systems with gcc 2.95.x </a> and <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-binaries-gcc3-i686.tar.gz\"> NMM $NMM_VERSION, compiled for systems with gcc 3.x</a> </li> ";

$external_files_tag = "<!-- NMM_EXTERNAL_FILES_URL -->";
$external_libs_string = "<li> <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-external-libs.tar.gz\">nmm-$NMM_VERSION-external-libs.tar.gz</a> and";
$external_includes_string = "<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-external-includes.tar.gz\">nmm-$NMM_VERSION-external-includes.tar.gz</a></li>";

$external_files_link_tag = "<!-- NMM_EXTERNAL_FILES_LINK_URL -->";
$external_files_link_string = "<li><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/nmm-$NMM_VERSION-external-libs.html\">nmm-$NMM_VERSION</a></li>";


$announce_string = "<br>&nbsp;&nbsp;&nbsp;The NMM team is happy to announce the release of <a href=\"Download/index.html\">NMM $NMM_VERSION</a>.";
@month_as_string = ("Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.");

$lib_site_tag = "<!--Item: replace with version -->";
$lib_site_string = "Libraries for NMM $NMM_VERSION";

$online_docu_tag = "<i>INTRODUCTION TO NMM";



# create a backup file
system("cp $NMM_download_site $NMM_download_site.bak");

#Begin> Open the download.html file
open(NMM_DOWNLOAD_SITE, "<$NMM_download_site");
open(TMP_DOWNLOAD_SITE, ">$TMP_download_site");

#Begin> This status variables are set to 1 if an link is added to the html file.
$download_entry = 0;
$api_entry = 0;
$external_files_entry = 0;
$external_files_link = 0;
#End<

while(<NMM_DOWNLOAD_SITE>) {
  print TMP_DOWNLOAD_SITE $_; #write the old line

  chomp($_);
  #Begin> Check for the ChangeLog-TAG
  if ($_ eq $change_log) {
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    print TMP_DOWNLOAD_SITE "$change_log_url\n";
  }
  #End<

  #Begin> Check for the DOWNLOAD_URL-TAG
  if ($_ eq $download_url_tag) {
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    chomp($tmp_string);
    if ($tmp_string eq $download_url_string) {
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    } else {
      $download_entry = 1; #set this status flag to true
      print TMP_DOWNLOAD_SITE "$download_url_string\n";
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    }
  }
  #End<

  #Begin> Checkl the precompiled libraries
  if ($_ eq $binary_packages) {
    #Read the <ul> flag and write it
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    print TMP_DOWNLOAD_SITE "$tmp_string\n";
    print TMP_DOWNLOAD_SITE "$binary_packages_link\n";
  }
  #End<

  #Begin> Check for the API_URL-TAG
  if ($_ eq $api_url_tag) {
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    chomp($tmp_string);
    if ($tmp_string eq $api_url_string) {
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    } else {
      $api_entry = 1; #set this status flag to true
      print TMP_DOWNLOAD_SITE "$api_url_string\n";
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    }
  }
  #End<

  #Begin> Check for the external_files-TAG
  if ($_ eq $external_files_tag) {
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    chomp($tmp_string);
    if ($tmp_string eq $external_libs_string) {
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    } else {
      $external_files_entry = 1;#set this status flag to true
      print TMP_DOWNLOAD_SITE "$external_libs_string\n";
      print TMP_DOWNLOAD_SITE "$external_includes_string\n";
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    }
  }
  #End<

  #Begin> Check for the external_link-TAG
  if ($_ eq $external_files_link_tag) {
    $tmp_string = <NMM_DOWNLOAD_SITE>;
    chomp($tmp_string);
    if ($tmp_string eq $external_files_link_string) {
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    } else {
      $external_files_link = 1;
      print TMP_DOWNLOAD_SITE "$external_files_link_string\n";
      print TMP_DOWNLOAD_SITE "$tmp_string\n"; #write the old line
    }
  }
  #End<
}

#Begin> Print some status messages
print "Changed the file $NMM_download_site \n";

if ( $download_entry == 1) {
  print "Added a link to nmm-$NMM_VERSION.tar.gz \n";
} else {
  print "Added *NOT* a link to nmm-$NMM_VERSION.tar.gz \n";
}

if ( $api_entry == 1 ) {
  print "Added a link to nmm-$NMM_VERSION-api-html.tar.gz \n";
} else {
  print "Added *NOT* a link to nmm-$NMM_VERSION-api-html.tar.gz \n";
}

if ( $external_files_entry == 1) {
  print "Added a link to nmm-$NMM_VERSION-external-libs.tar.gz and nmm-$NMM_VERSION-external-includes.tar.gz \n";
} else {
  print "Added *NOT* a link to nmm-$NMM_VERSION-external-libs.tar.gz and nmm-$NMM_VERSION-external-includes.tar.gz \n";
}

if ( $external_files_link == 1) {
  print "Added a link to nmm-$NMM_VERSION-external-libs.html \n";
} else {
  print "Added *NOT* a link to nmm-$NMM_VERSION-external-libs.html \n";
}
#End<

#Clealup
close(NMM_DOWNLOAD_SITE);
close(TMP_DOWNLOAD_SITE);

unlink($NMM_download_site);
rename($TMP_download_site, $NMM_download_site);
# End<


#Begin> Handle News site
#create a backup file
system("cp $NMM_news_site $NMM_news_site.bak");

#Open the News.html file
open(NMM_NEWS_SITE, "<$NMM_news_site");
open(TMP_NEWS_SITE, ">$TMP_news_site");
$first_occurence = 1;
$change_news_site = 0;

while(<NMM_NEWS_SITE>) {
  chomp($_);
  #Begin> Check for the 1. <p>-TAG
  if ( $_ =~ "<p>") {
    if ($first_occurence == 1) {
      $first_occurence = 0;
      $tmp_string = <NMM_NEWS_SITE>;
      chomp($tmp_string);
      if ($tmp_string eq $announce_string) {
        print TMP_NEWS_SITE "$_\n"; #write the old line
        print TMP_NEWS_SITE "$tmp_string\n"; #write the temporary string
      } else {
        #add the Announce string
        print TMP_NEWS_SITE "<p> $month_as_string[$month-1] $day, $year\n";
        print TMP_NEWS_SITE "$announce_string\n";
        print TMP_NEWS_SITE "$_\n";
        print TMP_NEWS_SITE "$tmp_string\n";
        $change_news_site = 1;
      }
    } else {
      print TMP_NEWS_SITE "$_\n";
    }
  } else {
    print TMP_NEWS_SITE "$_\n";
  }
}

if ($change_news_site == 1) {
  print "Added an announce entry on $NMM_news_site\n";
} else {
  print "Added *NOT* an announce entry on $NMM_news_site\n";
}
#End<
#Cleanup
close(NMM_NEWS_SITE);
close(TMP_NEWS_SITE);

unlink($NMM_news_site);
rename($TMP_news_site, $NMM_news_site);
# End<


#Begin> Hande the external_lib page
system("cp $NMM_external_libs_site $NMM_external_libs_site.bak");
open(NMM_LIBS_SITE, "<$NMM_external_libs_site");
open(TMP_LIBS_SITE, ">$TMP_external_libs_site");

while(<NMM_LIBS_SITE>) {
  print TMP_LIBS_SITE $_;
  chomp($_);
  if ($_ eq $lib_site_tag) {
    <NMM_LIBS_SITE>;  #skip the next line
    print TMP_LIBS_SITE "$lib_site_string\n";
  }
}
# Begin> Cleanup
close(NMM_LIBS_SITE);
close(TMP_LIBS_SITE);

unlink($NMM_external_libs_site);
rename($TMP_external_libs_site, $NMM_external_libs_site);
# End<


#Begin> Handle the online documentation
open(NMM_ONLINE_DOCU, "<$NMM_online_docu");
open(TMP_ONLINE_DOCU, ">$TMP_online_docu");

#create a backup file
system("cp $NMM_online_docu $NMM_online_docu.bak");

while(<NMM_ONLINE_DOCU>) {
  chomp($_);

  #Begin> set the docu to the new URL
  if ($_ =~ $online_docu_tag) {
    print "FOUND ONLINE DOCU TAG\n" ;
    # Skip the next 15 lines
    $tmp = "<i>OTHER DOCUMENTATION</i>";
    $test;
    while ($test ne $tmp) {
      $test = <NMM_ONLINE_DOCU>;
      chomp ($test);
    }
    print TMP_ONLINE_DOCU "<i>INTRODUCTION TO NMM $NMM_VERSION_NUMBERS[0].$NMM_VERSION_NUMBERS[1].x</i>\n";
    print TMP_ONLINE_DOCU "<blockquote>\n";
    print TMP_ONLINE_DOCU "<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/plugins/index.html\">Developing Plug-ins for NMM</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/plugins.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/idl/index.html\">The NMM Interface Definition Language</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/idl.pdf\">pdf</a>]  \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/state/index.html\">States and State Transitions of NMM Nodes</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/state.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/format/index.html\">Format programming</a> \n[<a href=\"http://graphics.cs.uni-sb.de/dist-$NMM_VERSION/NMM/Docs/format.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/time/index.html\">Synchronisation</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/time.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/serialization/index.html\">Serialization</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/Docs/serialization.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/messages/index.html\">Debug, error, warning and message output</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/messages.pdf\">pdf</a>]\n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/threads/index.html\">Threads</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/threads.pdf\">pdf</a>]\n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/dirstruct/index.html\">Directory Structure</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/dirstruct.pdf\">pdf</a>]\n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/style/index.html\">Coding Style</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/style.pdf\">pdf</a>]\n";
    print TMP_ONLINE_DOCU "<br><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/clic/index.html\">Clic - An application for setting up multimedia flow graphs</a> \n[<a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/clic.pdf\">pdf</a>] \n";
    print TMP_ONLINE_DOCU "</blockquote>\n";
    print TMP_ONLINE_DOCU "$tmp\n";
  } else {
    print TMP_ONLINE_DOCU "$_\n";
  }

  #End<

  #Begin> Add the doxygen docu
  if ($_ eq "<ul>"){
    $docu_link = "<li><a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Docs/Doxygen/html/index.html\">Doxygen-HTML-Docs (NMM $NMM_VERSION)</a> or as <a href=\"http://graphics.cs.uni-sb.de/NMM/dist-$NMM_VERSION/Download/nmm-$NMM_VERSION-api-html.tar.gz\">tar.gz</a></li>";
    $tmp_line = <NMM_ONLINE_DOCU>;
    chomp($tmp_line);
    if ($tmp_line eq $docu_link) {
      print TMP_ONLINE_DOCU "$tmp_line\n";
    } else {
      print TMP_ONLINE_DOCU "$docu_link\n";
      print TMP_ONLINE_DOCU "$tmp_line\n";
    }
  }
  #end<


}
close(NMM_ONLINE_DOCU);
close(TMP_ONLINE_DOCU);

unlink($NMM_online_docu);
rename($TMP_online_docu, $NMM_online_docu);
#End<


#Begin> Handle the NMM-Home site
#create a backup file
system("cp $NMM_home $NMM_home.bak");

open(NMM_HOME, "<$NMM_home");
open(TMP_NMM_HOME, ">$TMP_NMM_home");

while(<NMM_HOME>) {
  chomp($_);
  #Begin> Check for the "Latest news"
  if ( $_ =~ /Latest news/) {
    print "Found latest news \n";
    print TMP_NMM_HOME "$_\n";
    print TMP_NMM_HOME "<p> $month_as_string[$month-1] $day, $year\n";
    print TMP_NMM_HOME "$announce_string\n";
  } else {
    print TMP_NMM_HOME "$_ \n";
  }
}

close(NMM_HOME);
close(TMP_NMM_HOME);
unlink($NMM_home);
rename($TMP_NMM_home, $NMM_home);

#End<


#Begin> Handle the NMM-Home site
#create a backup file
system("cp $GRAPHICS_home $GRAPHICS_home.bak");

open(GRAPHICS_HOME, "<$GRAPHICS_home");
open(TMP_GRAPHICS_HOME, ">$TMP_GRAPHICS_home");

while(<GRAPHICS_HOME>) {
  chomp($_);
  #Begin> Check for the "Latest news"
  if ( $_ =~ /NEWS/) {
    print "NEWS Entry \n";
    print TMP_GRAPHICS_HOME "$_\n";
    print TMP_GRAPHICS_HOME "<p> $month_as_string[$month-1] $day, $year\n";
    print TMP_GRAPHICS_HOME "<br>&nbsp;&nbsp;&nbsp;The NMM team is happy to announce the release of <a href=\"NMM/Download/index.html\">NMM $NMM_VERSION</a>.\n<!--";
  } else {
    print TMP_GRAPHICS_HOME "$_ \n";
  }
}

close(GRAPHICS_HOME);
close(TMP_GRAPHICS_HOME);
unlink($GRAPHICS_home);
rename($TMP_GRAPHICS_home, $GRAPHICS_home);

#copy links to external libraries
system("cp $NMM_project_links $NMM_project_links.bak");
system("cp links.html $NMM_project_links");

#create a backup of the old mmboxrc_example and replace it with the current one
system("cp $NMM_dir/Download/mmboxrc_sample $NMM_dir/Download/mmboxrc_sample.bak");
system("cp resources/mmbox/mmboxrc_sample $NMM_dir/Download/mmboxrc_sample");


#End<


