# name:         "updatehistory"
### description:  update file "${TOPAZINIDIR}/history"
# Written by Hisao Kawaura
#
# Ver. 1.02  (Sep. 24, 2000)
#

$hfile = "${TOPAZINIDIR}/history";
@oldhistory = 0;
@newhistory = 0;
$historylength = 30;
$newfile = $ARGV[0];
if(isfileexist($hfile))
{
  open(F, $hfile);
  while($line = <F>)
  {
    if($line =~ "^[ ]*([^ \n]+)[ ]*")    
    {
      @oldhistory ,= $1;
    }
  }
  close F;
}
$length = @oldhistory; 
$match = 0;
$matchno = -1;
foreach $i (@oldhistory)
{
  $matchno++;
  if ($newfile eq $i)
  {
    $match = 1;
    last;
  }
}

if ($match == 0)
{
  if ($length < $historylength)
  {
    @newhistory = ($newfile, @oldhistory);
  }
  elsif($length == $historylength)
  {
    @newhistory ,= $newfile;
    for ($i = 0; $i < $length - 1; $i++) 
    {
      @newhistory ,= $oldhistory[$i];
    }
  }
  else
  {
    @newhistory = @oldhistory;
  }
}
else
{
  @newhistory ,= $oldhistory[$matchno]; 
  for ($i = 0; $i < $matchno; $i++)
  {
    @newhistory ,= $oldhistory[$i]; 
  }
  for ($i = $matchno + 1; $i < $length; $i++)
  {
    @newhistory ,= $oldhistory[$i]; 
  }
}

open(F, ">$hfile");
foreach $i (@newhistory)
{
  print F "$i\n";
}
close F;


