#!/usr/local/bin/perl
#################################################
# dbfileutil.pl Ver 1.0                         #
# By Po-Han Lin                                 #
# pohanlin@gmail.com                            #
# http://www.edepot.com                         #
#                                               #
# Export/Import between DB_File and SDBM_File   #
# from Windows and Unix platforms               #
#                                               #
# SDBM_Files can be ftp'ed across Unix/Windows  #
# platforms and work without a hitch. (pag and  #
# dir files).  DB_Files can't be ftp'ed across  #
# platform.  (something to do with edian)       #
# I like to use DB_File instead of SDBM_File    #
# This util will export a DB_File to SDBM_Files #
# for you to ftp.  Then you can import the      #
# files (after ftping) by coverting to DB_File  # 
# at destination workstation using this same    #
# util.  You can also clean up the SDBM_Files   #
# after you are done ftping (source side) and   #
# done importing (destination side)             #
# This same code will work on the unix or win   #
# side.  NOTE: be sure DB_File has less than    #
# 1024 bytes per entry, or it wont fit into a   #
# SDBM_File (unless you recompile the SDBM_File #
# to support bigger buckets.                    #
#                                               #
# By Po-Han Lin                                 #
# pohanlin@gmail.com                            #
# http://www.edepot.com/phl.html                #
# This program is free.  Donations accepted     #
#                                               #
# Ver 1.0                                       #
#################################################



$maindomain = "www.yourhostname.com";
$mainwwwpath="/home/www/wwwdirectory";
$mainwwwurl="http://$maindomain";
$maincgiurl="$mainwwwurl/cgi-bin";





use DB_File;
use SDBM_File;
use Fcntl;

require "/home/www/edepot/cgi-bin/mainconfig.pl";
$eGlossaryUrl = "$maincgiurl/dbfileutil.pl";
print "Content-type: text/html\n\n";
  # Get the input
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  # Split the name-value pairs
  @pairs = split(/&/, $buffer);
  foreach my $pairentry (@pairs) {
     my($name, $value) = split(/=/, $pairentry);

     # Un-Webify plus signs and %-encoding
     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $value =~ s///g;
     if ($name =~ /term/i) {
       $value =~ s/^\s+//;
       $value =~ s/\s+$//;
     }
  $FORM{$name} = $value;
  }



  # Process GET requests
  $param=$FORM{'param'};

  if ($FORM{'action'} eq "Import") {
    print "Imported";
    $eGlossaryPath = "$mainwwwpath/$param";
    tie %oldhash, "SDBM_File", $eGlossaryPath, O_RDWR|O_CREAT, 0644;
    tie %newhash, "DB_File", $eGlossaryPath, O_RDWR|O_CREAT, 0644;
    %newhash = %oldhash;
    
  } elsif ($FORM{'action'} eq "Export") {
    print "Exported";
    $eGlossaryPath = "$mainwwwpath/$param";
    tie %oldhash, "DB_File", $eGlossaryPath, O_RDWR|O_CREAT, 0644;
    tie %newhash, "SDBM_File", $eGlossaryPath, O_RDWR|O_CREAT, 0644;
    %newhash = %oldhash;
  } elsif ($FORM{'action'} eq "CleanWindows") {
   print "Cleaned";
     $pathname = $mainwwwpath . "\\$param.pag";
   system("del \"$pathname\" ") == 0 or die "failed";
     $pathname = $mainwwwpath . "\\$param.dir";
   system("del \"$pathname\" ") == 0 or die "failed"; 
  } elsif ($FORM{'action'} eq "CleanUnix") {
     print "Cleaned";
     $pathname = $mainwwwpath . "/$param.pag";
   system("rm \"$pathname\" ") == 0 or die "failed";
     $pathname = $mainwwwpath . "/$param.dir";
   system("rm \"$pathname\" ") == 0 or die "failed"; 

  } else {
    &printInputForm;
  }



sub printInputForm {

    print "<form method=\"post\" action=\"$eGlossaryUrl\">";
    print "<select name=\"action\">";
    print "<option selected value=\"Export\">Export";
    print "<option value=\"Import\"gt;Import";
    print "<option value=\"CleanWindows\">CleanWindows";
    print "<option value=\"CleanUnix\">CleanUnix";
    print "</select>";
    print "<input type=\"text\" name=\"param\" size=\"30\" value=\"FILENAME\">";
    print "<input type=\"submit\" value=\"Convert\">";
    print "</form>";

}

1;