#!/usr/bin/perl
 
# -------------------------------------------------------------------------
#
# $Source: /home/gerd/src/netscape2xcmail/RCS/ns2xcmail $
# $Revision: 1.4 $
# $Date: 1997/08/04 01:09:55 $
#
# $State: Exp $
#
# $Author: gerd $
#
# -------------------------------------------------------------------------
#
# $Locker:  $
#
# -------------------------------------------------------------------------
#
# ns2xcmail - (c) Copyright 1997 G. Pruemm
#
# This program may be distributed, modified and re-used free of charge 
# under the following conditions:
#
# - This program, modified versions of this program and programs which
#   make use of this program or of parts of it must be distributed free of 
#   charge.
#
# - The revision number as shown above, this copyright notice, the 
#   program description as shown below and a description of modifications 
#   must always be included in any source code, documentation and online 
#   information.
#
# -------------------------------------------------------------------------
#
# Description
#
# CGI-script to convert a Netscape 3.x address book to an 
# XCmail address book
#
# Entries being converted are:
#
# - Netscape "EMail Address" --> XCmail "E-Mail Address"
# - Netscape "Nickname"      --> XCmail "Alias"
# - Netscape "Name"          --> XCmail "Last Name"
# 
# If Netscape "Name" matches the scheme "<last name>, <first name>", 
# XCmail "Last Name" and "First Name" are being set.
#
# Netscape address book "Description" fields are not being converted to 
# XCmail address book "Info" fields.
#
# -------------------------------------------------------------------------
#
# $Log: ns2xcmail $
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Activate debug output

$DEBUG = $ENV{DEBUG};


# =========================================================================
# Function/Procedure Predeclarations
# =========================================================================

sub println;
sub println_debug;
sub print_version;
sub print_purpose;
sub print_usage;
sub print_error_and_exit;


# =========================================================================
# Prepare debug output
# =========================================================================

if ( defined $DEBUG )
{
  println_debug "DEBUG MODE on";
  println_debug "";
}


# =========================================================================
# Script specific settings
# =========================================================================

# -------------------------------------------------------------------------
# The name of the application to be used in filenames etc.
 
$APP_NAME="ns2xcmail";
println_debug "APP_NAME\n  $APP_NAME" if defined $DEBUG;


# -------------------------------------------------------------------------
# Purpose/Usage description for output with command line switch '-h'

$APP_PURPOSE="purpose: convert a Netscape 3.x address book to an XCmail address book";
println_debug "APP_PURPOSE\n  $APP_PURPOSE" if defined $DEBUG;

$APP_USAGE="usage:   $APP_NAME [<input file> [<output file>]] | -h | -V";
println_debug "APP_USAGE\n  $APP_USAGE" if defined $DEBUG;


# ---------------------------------------------------------------------
# Retrieve $APP_VERSION and $APP_RELEASE from $Revision: 1.4 $ tag of RCS
#
# $APP_VERSION is being used to find the config file later on

($dummy,$REVISION) = split /\s+/, '$Revision: 1.4 $';

($APP_VERSION,$APP_RELEASE) = split /\./, $REVISION;

println_debug "APP_VERSION\n  $APP_VERSION" if defined $DEBUG;
println_debug "APP_RELEASE\n  $APP_RELEASE" if defined $DEBUG;


# ---------------------------------------------------------------------
# Retrieve $APP_DATE from $Date: 1997/08/04 01:09:55 $ tag of RCS

($dummy,$APP_DATE,$APP_TIME) = split /\s+/, '$Date: 1997/08/04 01:09:55 $';
println_debug "APP_DATE\n  $APP_DATE" if defined $DEBUG;
println_debug "APP_TIME\n  $APP_TIME" if defined $DEBUG;


# =========================================================================
# Constants
# =========================================================================

my $output_format = 'xcmail';
#my $output_format = 'verbose';
#my $output_format = 'html';


# =========================================================================
# Global variables
# =========================================================================


# =========================================================================
# Functions / Procedures
# =========================================================================

# -------------------------------------------------------------------------
# A little helper

sub println
{
  my $text;


  if ( scalar (@_) > 0 )
  {
    $text=$_[0];
  }
  else
  { 
    $text="";
  }

  print "$text\n";
}


# -------------------------------------------------------------------------
# Print debug output

sub println_debug
{
  my $text;


  if ( scalar (@_) > 0 )
  {
    $text=$_[0];
  }
  else
  { 
    $text="";
  }

  print STDERR "$text\n";
}


# -------------------------------------------------------------------------
# Print error message and exit program

sub print_error_and_exit
{
  if ( scalar (@_) > 0 )
  {
    $error_message=$_[0];
  }
  else
  { 
    $error_message="";
  }

  print STDERR "$APP_NAME: *** ERROR *** $error_message\n";
  exit -1;
}


# -------------------------------------------------------------------------
# Print version, purpose and usage messages

sub print_version
{
  print STDERR "$APP_NAME ${APP_VERSION}.${APP_RELEASE} [$APP_DATE $APP_TIME]\n";
}

sub print_purpose
{
  print STDERR "$APP_USAGE\n";
}

sub print_usage
{
  print STDERR "$APP_PURPOSE\n";
}


# =========================================================================
# Main
# =========================================================================

# -------------------------------------------------------------------------
# Evaluate command line parameters, if any
# -------------------------------------------------------------------------

if ( scalar (@ARGV) == 0 )
{
  $infile="";
  $outfile="";
}
elsif ( scalar (@ARGV) == 1 )
{
  if ( $ARGV[0] eq '-h' )
  {
    print_purpose;
    print_usage;
    exit -1;
  }
  elsif ( $ARGV[0] eq '-V' )
  {
    print_version;
    exit -1;
  }
  else
  {
    $infile=$ARGV[0];
    $outfile="";
  }
}
elsif ( scalar (@ARGV) == 2 )
{
  $infile=$ARGV[0];
  $outfile=$ARGV[1];
}
else
{
  print_purpose;
  print_usage;
  exit -1;
}


# -------------------------------------------------------------------------
# Open the input file

if ( $infile ne '' )
{
  if (-e "$infile")
  {
    open (FD_INFILE, "<$infile")
  }
  else
  {
    print_error_and_exit "Input file $infile does not exist\n";
  }
}
else
{
  open (FD_INFILE, "<-")
}


# -------------------------------------------------------------------------
# Open the output file

if ( $outfile ne '' )
{
  open (FD_OUTFILE, ">$outfile")
}
else
{
  open (FD_OUTFILE, ">-")
}


# -------------------------------------------------------------------------
# read the input file, convert its contents and write the converted data
# to the output file

println_debug if defined $DEBUG;

while ($line = <FD_INFILE>)
{
  my $email_address;
  my $name;
  my $firstname;
  my $nickname;

  # remove trailing carriage-return / line-feed, if any
  chop $line;

  # skip line, if this does not contain a mail address
  next if ($line !~ m/MAILTO/i );

  # skip line, if this is an alias
  next if ($line =~ m/ALIASOF/i );

  println_debug "line: $line" if defined $DEBUG;


  # -----------------------------------------------------------------------
  # Parse the data set 

  # parse e-mail address
  ($head, $email_address, $tail) = ($line =~ /^(.*mailto:)([^"]*)(.*)/ );

  println_debug "  email_address: head         : $head" if defined $DEBUG;
  println_debug "  email_address: email_address: $email_address" if defined $DEBUG;
  println_debug "  email_address: tail         : $tail" if defined $DEBUG;


  # parse nickname
  if ($line =~ m/NICKNAME/i )
  {
    ($head, $nickname, $tail) = ($line =~ /^(.*NICKNAME=")([^"]*)(.*)/ );
  }
  else
  {
    $nickname = "";
  }

  println_debug "  nickname     : head         : $head" if defined $DEBUG;
  println_debug "  nickname     : nickname     : $nickname" if defined $DEBUG;
  println_debug "  nickname     : tail         : $tail" if defined $DEBUG;


  # parse name
  if ($line =~ m/^(.*<A.*?>)([^<]*)(.*)/i )
  {
    ($head, $name, $tail) = ($line =~ /^(.*<A.*?>)([^<]*)(.*)/ );
  }
  else
  {
    $name = "";
  }

  println_debug "  name         : head         : $head" if defined $DEBUG;
  println_debug "  name         : name         : $name" if defined $DEBUG;
  println_debug "  name         : tail         : $tail" if defined $DEBUG;


  # extract firstname from name (assumption: "<Name>, <Firstname>")
  if ($name =~ m/^(.*), (.*)/i )
  {
    ($name, $firstname) = ($name =~ /^(.*), (.*)/ );
  }
  else
  {
    $firstname = "";
  }

  println_debug "  firstname    : name         : $name" if defined $DEBUG;
  println_debug "  firstname    : firstname    : $firstname" if defined $DEBUG;

  println_debug if defined $DEBUG;


  # -----------------------------------------------------------------------
  # Print the data set 

  if ($output_format eq 'verbose')
  {
    print FD_OUTFILE "EMAIL_ADDRESS=\"${email_address}\""; 
    print FD_OUTFILE " NAME=\"${name}\""; 
    print FD_OUTFILE " FIRSTNAME=\"${firstname}\""; 
    print FD_OUTFILE " NICKNAME=\"${nickname}\""; 
    print FD_OUTFILE "\n"; 
  }
  else
  {
    print FD_OUTFILE "${nickname}\t"; 
    print FD_OUTFILE "${name}\t"; 
    print FD_OUTFILE "${firstname}\t"; 
    print FD_OUTFILE "${email_address}\t"; 
    print FD_OUTFILE "0\t"; 
    print FD_OUTFILE "\n"; 
  }
}

# -------------------------------------------------------------------------
# Close the files

close (FD_INFILE);
close (FD_OUTFILE);


# -------------------------------------------------------------------------
# End
# -------------------------------------------------------------------------
