#!/usr/bin/perldb -w
# FILE:  convert_to_db.pl

# File that converts a flat text file into a database, using Berkley DB.
# Requires two file names on command line:  
#    - name of text file containing product information, and
#    - name of database file to create.
# It assumes that the text file uses | for delimiters

#Use the DBM module.
use Getopt::Long;
use DB_File;

# Set up the command line options.
#my $ret = GetOptions ("f|filename:s", "d|database:s");
#my $filename = $opt_f || die "Usage: $0 -f filename -d database\n";
#my $database = $opt_d || die "Usage: $0 -f filename -d database\n";

($filename, $database) = @ARGV;

my %inventory;   #inventory will be the name of the hash 

# Open the input file.
open (INPUT, "$filename") || die "Can not open the file $filename : $!\n";

#Open the DBM database file.
dbmopen (%inventory, $database, 0700) || die "Can not open the DBM database 
$database : $!\n";

#Loop through the input file and put it into the DBM database.
while (<INPUT>)
{
  chomp;

  #split the line
  #assumes the text file uses | for delimiters and has field names
  # for number, type, name, price, and desc:

  my ($number, $type,$name,$price,$desc) = split (/\|/);

  #save the information into the DBM file.
  $inventory{$number} = "${type}|${name}|${price}|${desc}";
}

#Close the dbm database
dbmclose %inventory;

#Close the input file.
close (INPUT);
