#!/usr/local/bin/webwork-perl

####################################################################
# Copyright @ 1995-1999 University of Rochester
# All Rights Reserved
####################################################################

use lib '.'; use webworkInit; # WeBWorKInitLine

# Called as
# showDatabase.pl courseID

use Global;
use Auth;
use strict;

if (@ARGV != 2)
  {
   print "\nSyntax is export_webwork-database.pl courseID outputTextFile\n";
   print "      (e.g. export_webwork-database.pl  demoCourse textDatabase)\n\n";
   exit(0);
  }

my $course = $ARGV[0];
my $outFileName = $ARGV[1];

# Give caller options


# establish environment for this script

  &Global::getCourseEnvironment($course);

# Directory paths

my $databaseDirectory = getCourseDatabaseDirectory();
my $courseScriptsDirectory = getWebworkScriptDirectory();

# File names

  require "${courseScriptsDirectory}$Global::DBglue_pl";
  require "${courseScriptsDirectory}$Global::HTMLglue_pl";
  require "${courseScriptsDirectory}$Global::FILE_pl";

  my $databaseFile = $Global::database;

# get all psvn's for set

my %PROBSET;
my $wwDbObj;			# Object for referencing the database

&read_psvn_record(\$wwDbObj, \%PROBSET, "${databaseDirectory}${databaseFile}", 'R', $Global::standard_tie_permission);

my ($key,$item);
my @keys = sort keys %PROBSET;

open(OUTFILE,">$outFileName") or wwerror("$0","can't open $outFileName for writing");

foreach $key (@keys) {
    print OUTFILE "[$key]\n";
    my $string = $PROBSET{$key};
#    print  OUTFILE "|$string|\n";
	my @probSetRecord = split(/[\&]/,$string);
	@probSetRecord = sort @probSetRecord;
	foreach $item (@probSetRecord) {
	    print OUTFILE "\t$item\n";
	}
	print OUTFILE "\n";
}

close(OUTFILE);
