## $Id$

## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
<?
<protect>
## $Id$
## DO NOT EDIT THIS FILE!  IT WAS AUTOMATICALLY GENERATED FROM CDSware WML SOURCES.


   ## Description:   This function saves the revised file in ARCHIVE and rename
   ##		     older versions if needed
   ## Author:	     T.Baron
   ##
   ## PARAMETERS:    -

   function Move_Revised_Files($param_array,$MAINPATH) 
   {
      global $DISTILLER, $GZIP, $ACROREAD;

      // Set local variables
      $fp = fopen("$MAINPATH/archivepath","r");
      $archivepath = fread($fp,filesize("$MAINPATH/archivepath"));
      fclose($fp);
      $fp = fopen("$MAINPATH/formatfile","r");
      $formatfile = fread($fp,filesize("$MAINPATH/formatfile"));
      fclose($fp);
      $fp = fopen("$MAINPATH/revtype","r");
      $type = fread($fp,filesize("$MAINPATH/revtype"));
      fclose($fp);
      $type = ereg_replace("\n","",$type);
      if ($type == "") { $type = "update"; }

      if (ereg("update",$type))
      {
         if(is_dir ("$archivepath/obsolete") == 0)
	    mkdir("$archivepath/obsolete", 0777);
      }
      elseif(ereg("add",$type))
      {
         if (!is_dir("$archivepath/$formatfile"))
	    mkdir ("$archivepath/$formatfile",0777);

	 $archivepath = "$archivepath/$formatfile";

	 $fp = fopen("$MAINPATH/MainFile","r");
	 $formatfile = fread($fp,filesize("$MAINPATH/MainFile"));
	 fclose($fp);

	 $filesdir = opendir("$MAINPATH/files/MainFile");
	 while ($file = readdir($filesdir))
	    if ($file != "." && $file != "..")
	    {
	       $lastmovedfile = $file;
	       copy("$MAINPATH/files/MainFile/$file","$archivepath/$file");
	    }
      }

      if (!ereg("add",$type))
      {
         // Get version number
	 $number = 1;
	 $handle=opendir("$archivepath");
	 while ($file = readdir($handle))
	    if (ereg("${formatfile}_obsolete_version(.)",$file,$num))
	       if ($num[1] > $number-1) { $number=$num[1]+1; }
	
	 // Beginning of directory
	 rewinddir($handle);

	 // Rename older files with version number
	 while ($file = readdir($handle)) 
	 {
	    if (ereg("${formatfile}",$file))
	    {
	       if (!ereg("version",$file))
	       {
	          // special case for ps.gz
		  if (!ereg("(.*)\.(ps.gz)",$file,$oldname))
		     ereg("(.*)\.([^\.]*)",$file,$oldname);

		  $now = strftime("%d_%m_%Y");
		  if (!ereg("update",$type))
		  {
		     $newname = "$oldname[1]_obsolete_version${number}_${now}."
		        . "$oldname[2]";
		     rename("$archivepath/$file","$archivepath/$newname");
		  }
		  else
		     rename("$archivepath/$file"
		        ,"$archivepath/obsolete/$file");
	       }
	       else
	       {
	          if (ereg("update",$type))
		     rename("$archivepath/$file"
		        ,"$archivepath/obsolete/$file");
	       }
	    }
    	 }
	 closedir($handle);

	 // Move new files to archives
	 $handle = opendir("$MAINPATH/files/MainFile");
	 while ($file = readdir($handle)) 
	 {
	    if ($file != "." && $file != "..")
	    {
	       // special case for ps.gz
	       if (!ereg("(.*)\.(ps.gz)",$file,$oldname))
	          ereg("(.*)\.([^\.]*)",$file,$oldname);
	       copy("$MAINPATH/files/MainFile/$file"
	          ,"$archivepath/$formatfile.$oldname[2]");

	       if ($oldname[2] == "ps" && $DISTILLER != "")
	       {
	          system("echo 2 | $DISTILLER -pagesize 595 842 pts $archivepath/$formatfile.$oldname[2]");
		  if ($GZIP != "")
		     system("$GZIP $archivepath/$formatfile.$oldname[2]");
	       }

	       if ($oldname[2] == "pdf" && $ACROREAD != "")
	       {
	          system("$ACROREAD -toPostScript -level2 $archivepath/$formatfile.$oldname[2]");
		  if ($GZIP != "")
		     system("$GZIP $archivepath/$formatfile.ps");
	       }
	    }
	 }
	 closedir($handle);
      }

      // Create Note file for modified aleph record
      $now = strftime("%Y-%m-%d %H:%M:%S");
      $fp = fopen ("$MAINPATH/NO","w+");
      if (!ereg("add",$type))
         fputs($fp,"revised version number $number submitted on $now");
      else
         fputs($fp,"added appendix file $lastmovedfile on $now");
      fclose($fp);
   }
</protect>
?>