## $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:   function Create_reference
   ##                This function generates a reference from a counter
   ## Author:	     T.Baron
   ## PARAMETERS:    counter_path: path to the counter
   ##                ref_format: format of the generated reference

   function Create_Reference($counter_path,$ref_format)
   {
      global $COUNTERS;

      # lock the database ...
      putlock();

      //test if the counters directory exists, if not attempts to create it
      if (!is_dir("$COUNTERS"))
         if (!mkdir("$COUNTERS"))
            outError("File System: Cannot create working directory $COUNTERS");
      if (!file_exists("$COUNTERS/${counter_path}"))
      {
         $fp = fopen("$COUNTERS/${counter_path}","w+");
	 if (!fp)
            outError("File System: Cannot create counter file "
                . "$COUNTERS/${counter_path}");
         fwrite ($fp,"0");
         fclose($fp);
      }

      // retrieve current counter value
      $fp = fopen("$COUNTERS/${counter_path}","r");
      $id = fread($fp,100);
      fclose($fp);

      // increment the counter
      $id = $id+1;

      // store new value
      $fp = fopen("$COUNTERS/${counter_path}","w");
      fwrite ($fp,$id);
      fclose($fp);

      # ... unlock the database ...
      unlock();

      # create final value
      $reference = sprintf("%s-%03d",${ref_format},$id);

      # Return the report number prelude with the id concatenated on at the end
      return ($reference);
   }

   function putlock()
   {
      global $COUNTERS,$access;

      $locked = FALSE;

      // try 1mn
      for ($i=0;$i<60;$i++)
      {
         if (file_exists("$COUNTERS/counterlock"))
	 {
            $fp = fopen("$COUNTERS/counterlock","r");
	    $lid = fread($fp,filesize("$COUNTERS/counterlock"));
	    fclose($fp);
	    if ($lid == $access)
	    {
	       $locked = TRUE;
	       break;
	    }
	    else
	       sleep(1);
         }
	 else
	 {
	    $fp = fopen("$COUNTERS/counterlock","w+");
	    fwrite($fp,$access);
	    chmod("$COUNTERS/counterlock",0777);
	    $locked = TRUE;
	    break;
         }
      }

      if (!$locked)
         outError("Cannot lock counter file... It would be too dangerous to "
             . "go on... Please try later");
   }

   function unlock()
   {
      global $COUNTERS,$access;

      if (file_exists("$COUNTERS/counterlock"))
      {
         $fp = fopen("$COUNTERS/counterlock","r");
	 $lid = fread($fp,filesize("$COUNTERS/counterlock"));
	 fclose($fp);
	 if ($lid == $access)
            unlink("$COUNTERS/counterlock");
      }
   }
</protect>

?>