#!/usr/cs/bin/perl -w
#
# File: new_dir.pl
#
# Purpose: Creates a new test directory, creating all the needed
#          subdirectories and making "stubs" for all the needed
#          files.  Also has options for creating links to files in
#          an existing data directory.
#
# Modifications:
#   13-MAY-1998  txe  Initial creation
#   14-MAY-1998  txe  Added links to "source" directory
#

  require "subs/ir_subs.pm";

  $data_dir = GetArgument (0, "new data directory", "test2"); 
  $old_dir  = GetArgument (1, "old data directory (or RETURN for 'none')", "test");
    
  if (!CreateDataDir ($data_dir)) {
    print "ERROR: Failure creating data dir '$data_dir'\n";
    exit;
  }

  if ($old_dir eq "" || $old_dir eq "none") {
    &CreateStubs ($data_dir);
  }
  else {
    &CreateLinks ($data_dir, $old_dir);
  }

  print "\nSuccessfully created data dir '$data_dir'.\n";
  1;  

##################################################################

sub CreateDataDir {
  local ($data_dir) = @_;

  if (-e $data_dir) {
    print "Error: the directory '$data_dir' already exists\n";
    return 0;
  }

  if (!mkdir ($data_dir, 0775)) {
    print "Error: could not create data dir '$data_dir'\n";
    return 0;
  }

  ### create subdirectories ###
  
  if (!mkdir ("$data_dir/merit",   0775)
   || !mkdir ("$data_dir/ranks",   0775)
   || !mkdir ("$data_dir/cmp",     0775)
   || !mkdir ("$data_dir/viles",   0775)
   || !mkdir ("$data_dir/matrix",  0775)) {
#  || !mkdir ("$data_dir/f_and_w", 0775)) {
    print "Error: could not create all subdirectories in '$data_dir'\n";
    return 0;
  }

  return 1;
}

################################################################################

sub CreateLinks {
  local ($new_dir, $old_dir) = @_;

  if (!-e $new_dir) {
    print "Error: the new directory '$new_dir' does not exist\n";
    return 0;
  }

  if (!-e $old_dir) {
    print "Error: the old directory '$old_dir' does not exist\n";
    return 0;
  }

  print "Respond to each question with 'y'es, 'n'o, 'a'll, or 'q'uit\n\n";

  $auto_yes = 0;

  &CreateLink ("$old_dir/coll_map.txt", "$new_dir/coll_map.txt");
  &CreateLink ("$old_dir/f.txt",        "$new_dir/f.txt");
  &CreateLink ("$old_dir/w.txt",        "$new_dir/w.txt");
  &CreateLink ("$old_dir/mask.txt",     "$new_dir/mask.txt");
  &CreateLink ("$old_dir/query.txt",    "$new_dir/query.txt");
  &CreateLink ("$old_dir/merit/opt",    "$new_dir/merit/opt");
}

################################################################################

sub CreateLink {
  local ($old_file, $new_file) = @_;

  if (!$auto_yes) {
    print "Link '$new_file' to '$old_file'? [y/n/a/q] ";
    $choice = substr (<STDIN>, 0, 1);

    if ($choice eq 'a' || $choice eq 'A') {
      $auto_yes = 1;
      $choice = 'y';
    }
  }
  else {
    $choice = 'y';
  }
    
  if ($choice eq 'y' || $choice eq 'Y') {
    if (!-e $old_file) {
      print "Error: old file '$old_file' does not exist\n";
      exit;
    } 
    if (!link ($old_file, $new_file)) {
      print "Error: couldn't link '$new_file' to '$old_file'\n";
      exit;
    }
  }
  elsif ($choice eq 'q' || $choice eq 'Q') {
    print "\nGoodbye\n";
    exit;
  }
}

################################################################################

sub CreateStubs {
  local ($data_dir) = @_;

  $query_file = "$data_dir/query.txt";
  $mask_file  = "$data_dir/mask.txt";
  $opt_file   = "$data_dir/merit/opt";
  
  open (FP, ">$query_file") || die ("Could not create '$query_file'");
  print FP "# stub file, format: [query_id query_term term's_freq_in_query]\n";
  close (FP);

  open (FP, ">$mask_file") || die ("Could not create '$mask_file'");
  print FP "# stub file, format: [query_start query_end coll_start coll_end]\n";
  close (FP);

  open (FP, ">$opt_file") || die ("Could not create '$opt_file'");
  print FP "# stub file, format: [query_id dummy(0) doc_id relevance(0/1)]\n";
  close (FP);

  print "Remember to fill out the stub files and f_and_w directory\n";
  return 1;
}

################################################################################

