#!/usr/bin/perl
#
# Nextrecord.pl returns the next consecutively named file with base
# bname in directory bdir (i.e. if vmail1.ul exists, return /msgs/vmail2)  
# Designed to work with the simple answering machine, YMMV.
# Pass bdir ending in a  /
#
# Copyright 2000 OST Corporation
#  
# V1.0 3/31/00 - created - rsb
# V1.1 10/1/00 - begin mods for bayonne - rsb

use strict;
use lib $ENV{'SERVER_LIBEXEC'};
use TGI;

my($version) = "1.1";
my($debug_level) = 1;
my(@file_list);  
my($directory);
my($file_base);
my($next);

#$directory = "/home/ivrapp/msgs/";
#$file_base = "vmsg"; 

$directory = $TGI::QUERY{bdir};
$file_base = $TGI::QUERY{bname};

if ($debug_level != 0)
{  
  TGI::set("val2", $directory);
  TGI::set("val3", $file_base);
}

@file_list = voice_files($directory);
$next = $directory . next_file($file_base, @file_list);

TGI::set("msgname", $next);

###############################################################################
# voice_files - return names of potential voice files in a directory
###############################################################################
sub voice_files {

  # save the full name and print the immediate directory name
  my($full_dir) = $_[0];
  my($stripped_dir) = ($_[0] =~ /(\w+)$/);
	
  #print $full_dir;

  if (!opendir (DIR, "$_[0]") )
    {
      no_such_dir($_[0]);
      return 0;
    }

  my(@allfiles) = readdir DIR;
  my($j) = 0;
  my($filename);
  my(@goodfiles);

  foreach $filename ( @allfiles ) {

    # skip files starting with a dot or ending in ~
    next if  ($filename  =~ /^\./);
    next if  ($filename  =~ /~$/);

    # also skip special files
    stat($filename);
    next if -d $filename;

    #save the rest, stripping .ul, .au, .wav, .ra, or .mp3
    ($goodfiles[$j]) = ($filename =~ /(.+)(\.ul$|\.au$|\.wav$|\.ra$|\.mp3$)/);
    $j++;

  }

  closedir DIR;

  #print @goodfiles;
  return @goodfiles;
}

###############################################################################
# next_file - return a name with basename of first arg, and an  
# appended numerical extension one higher than any name in second arg.
###############################################################################
sub next_file
{
    my($highest) = 0;
    my($next);
    my($base);
    my(@unsorted);
    
    ($base, @unsorted) = @_;

    #print "\n next_file base $base unsorted @unsorted\n";

    foreach $next (@unsorted) {
	if ($next =~ /^$base/) {
	    #print "\n$next has $base";
	    if (($next cmp $highest) >= 0) {  
		# bugbug ...lock file...
		$highest = $next;
		#print "\n$highest is highest";
	    }	
	}
    }
    
    ($next) = ($highest =~ /(\d+$)/);
    #print "\nthe number part of highest $next";
    $highest = $next + 1;
    $next = $base.$highest;

    #print "\n next file is : $next\n";

    return $next;
}

###############################################################################
# no_such_dir - if the directory read fails        
###############################################################################
sub no_such_dir
{
  #print "\nSORRY, $_[0] NOT AVAILABLE";
}

#########################################################################
#EOF



