#!S_PERLBINARY_S
#=======================================================================
# Newtonlink   - transfer data between a Apple Newton Message Pad and
#                Unix applications
#
# Copyright (C) 1996-1998    The Newtonlink Developers
#                            (newtonlink@newton.bawue.de)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=======================================================================

# $Log: newtonlink,v $
# Revision 1.31  1998/09/02 15:45:43  kalli
# Wegen Umstellung auf CVS wird die Newtonlink-Versionsnummer nicht mehr automatisch
# aus der RCS Versionsnummer generiert, sondern muss per Hand eingegeben werden.
#
# Revision 1.30  1998/09/02 15:33:51  kalli
# *** empty log message ***
#
# Revision 1.29  1998/07/13 19:23:21  kalli
# New version 1.29
#
# Revision 1.28  1998/04/10 15:11:35  kalli
# *** empty log message ***
#
# Revision 1.27  1998/03/16 13:50:22  kalli
# Viele Aenderungen fuer 1.27
#
# Revision 1.26  1997/11/16 09:45:50  kalli
# Aenderungen fuer Linux Filesystem Standard
#
# Revision 1.25  1997/09/28 14:21:12  kalli
# Aenderungen von Andrew : PID-File
# Aufruf von tknl.tcl
#
# Revision 1.24  1997/07/12 19:13:21  kalli
# Feste Pfade im Aufruf der Perl-Module raus
# Abfrage des OS
#
# Revision 1.23  1997/05/31 09:50:53  kalli
# div
#
# Revision 1.22  1997/05/24 15:29:51  kalli
# Abfrage des OS-Typ per uname
#
# Revision 1.21  1997/04/22 20:15:35  kalli
# Test mode added
#
# Revision 1.2  1997/04/08 18:28:33  kalli
# New Version
#
# Revision 1.1  1997/03/27 20:00:09  kalli
# Aenderungen von Andrew :
# Newtonlink Configfile
#
# Revision 1.0  1997/03/06 18:56:42  kalli
# Aenderungen zu Rev. 1.0
#
# Revision 0.97  1997/03/02 21:45:44  kalli
# Aenderungen fuer Install via Sloup
#
# Revision 0.96  1997/02/23 15:42:27  kalli
# Neue Option : -mail
#
# Revision 0.95  1997/02/22 17:07:08  kalli
# No changes
#
# Revision 0.94  1997/02/19 22:23:47  kalli
# No changes
#
# Revision 0.93  1997/02/15 21:31:24  kalli
# Neue Option : -nsadr
#
# Revision 0.92  1997/01/05 16:07:02  kalli
# Neu : -inst mit minicom-Aufruf
#
# Revision 0.91  1996/12/14 19:54:34  kalli
# div.
#
# Revision 0.90  1996/12/04 17:42:55  kalli
# Version mit GUI per Pixmaps
#
# Revision 0.89  1996/12/01 12:44:38  kalli
# First internet version
#
# Revision 0.88  1996/11/24 21:13:08  kalli
# Erste Version mit finesse
#
# Revision 0.87  1996/11/24 20:01:45  kalli
# Umstellung auf Unterprogramme mit require
#
# Revision 0.86  1996/11/24 18:50:32  kalli
# Einige Bugfixes
#
# Revision 0.85  1996/11/24 12:48:41  kalli
# Sortierfunktion fuer addressbook-Eintraege
#
# Revision 0.84  1996/11/23 20:17:29  kalli
# Text mit mehr als einer Zeile bei Email und Print funktioniert jetzt
#
# Revision 0.83  1996/11/22 21:36:07  kalli
# addressbook neu aufgebaut
#
# Revision 0.82  1996/11/22 19:14:24  kalli
# *** empty log message ***
#
# Revision 0.81  1996/11/22 19:11:47  kalli
# RCS revision als Versionsnummer
#
# Revision 0.8  1996/11/21 21:16:16  kalli
# Erste Version mit eWorld-Mail
#
# Revision 0.7  1996/11/20 20:51:41  kalli
# Ausgabe auf Drucker, Bug : nur eine Zeile wird ausgegeben.
#
# Revision 0.6  1996/11/19 21:41:02  kalli
# Zugriff auf OutBox-Inhalt klappt
#
# Revision 0.5  1996/11/18 21:40:19  kalli
# Einstellung der Baudrate
#
# Revision 0.4  1996/11/17 20:55:38  kalli
# Funktion -input dazu
#
# Revision 0.3  1996/11/17 14:12:44  kalli
# Mehr Funktionen, ser. Uebertragung wird korrekt beendet.
#
# Revision 0.2  1996/11/16 20:00:13  kalli
# Erste Version mit -adr und -xrolo Parameter
#
# Revision 0.1  1996/11/16 18:47:24  kalli
# *** empty log message ***
#


sub CreatePersonalConfigFile {

    # look if personal Newtonlink directory exists
    if (!(-d "$ENV{'HOME'}/.newtonlink")) {
        mkdir ("$ENV{'HOME'}/.newtonlink", 493);
    }

    open (IN, "S_NEWTONLINK_CONFIGFILEDIR_S/newtonlink.config") || die "cannot open the default systemwide config file\n";
    open (OUT, ">$ENV{'HOME'}/.newtonlink/newtonlink.config") || die "cannot create ~/.newtonlink/newtonlink.config\n";

    while (<IN>) {
        print OUT $_
    }
}


$rev = "1.29";

# set newtonlink install directory
$instdir = "S_NEWTONLINK_INSTALLDIR_S";

# Look if we have a personal config file
if (-e "$ENV{'HOME'}/.newtonlink/newtonlink.config") {

    require "$ENV{'HOME'}/.newtonlink/newtonlink.config";
    &SetDefaults;

    if ("$rev" ne "$ConfigFileVersion") {
        &CreatePersonalConfigFile;
        printf "Your personal config file is out of date,\n";
        printf "created a new config file \"$ENV{'HOME'}/.newtonlink/newtonlink.config\"\n";
        printf "Edit it first to match your needs\n";
        exit;
    }

} else {

    # load default config file and run configuration to determine the System_Mode
    require "$instdir/newtonlink.config";
    &SetDefaults;

    if ($System_Mode eq "UNIX") {
        &CreatePersonalConfigFile;
        printf "No personal config file found,\n";
        printf "created a config file \"$ENV{'HOME'}/.newtonlink/newtonlink.config\"\n";
        printf "Edit it first to match your needs\n";
        exit;
    }
}


# include subroutines
require "$instdir/SetSloupBaudRate.pl";
require "$instdir/DeviceReadWrite.pl";
require "$instdir/GreetUser.pl";
require "$instdir/GetCards.pl";
require "$instdir/GetNotes.pl";
require "$instdir/GetMeetings4Plan.pl";
require "$instdir/Get4Xrolodex.pl";
require "$instdir/Get4Kjots.pl";
require "$instdir/Get4Addressbook.pl";
require "$instdir/Get4KsendfaxPhonebook.pl";
require "$instdir/Get4WebAddress.pl";
require "$instdir/Get4NetscapeMail.pl";
require "$instdir/Get4NetscapeAddressbook.pl";
require "$instdir/Get4XfmailAddressbook.pl";
require "$instdir/GetPrintOutbox.pl";
require "$instdir/GetFaxOutbox.pl";
require "$instdir/InstallPackage.pl";
require "$instdir/CallLpkg.pl";
require "$instdir/SendFile2Newton.pl";
require "$instdir/KeyboardInput.pl";
require "$instdir/Get4Mail.pl";
require "$instdir/GetPackageNames.pl";
require "$instdir/GetPackage.pl";
require "$instdir/GetSoupInfo.pl";
require "$instdir/SloupTest.pl";


#---------------------------------------------------------------------
# main
#---------------------------------------------------------------------

# set default values
&SetDefaults;

# get system type (for Unix systems with the uname command)
if ($System_Mode eq "OS-2") {
    $SystemType = "OS-2\n";
} else {
    $SystemType = `uname`;
}

# check for supported OS's
if ($SystemType eq "IRIX\n") {
    $SystemType = "IRIX"
} elsif ($SystemType eq "HP-UX\n") {
    $SystemType = "HP-UX"
} elsif ($SystemType eq "OSF1\n") {
    $SystemType = "OSF1"
} elsif ($SystemType eq "SunOS\n") {
    $SystemType = "SunOS"
} elsif ($SystemType eq "Linux\n") {
    $SystemType = "Linux"
} elsif ($SystemType eq "FreeBSD\n") {
    $SystemType = "FreeBSD"
} elsif ($SystemType eq "OS-2\n") {
    $SystemType = "OS-2"
} else {
    printf "Newtonlink Exit - Sorry, your operating system is currently not supported\n";
    exit;
}

printf "Newtonlink Version $rev, running on $SystemType\n";
printf "Newtonlink comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n";
printf "welcome to redistribute it under the terms of the GNU General Public License.\n";

# check if another newtonlink process is running
if (-e $PIDFILE){
   printf "\nAnother newtonlink process is running.\n";
   if (-o $PIDFILE){ 
       printf "\nNow trying to kill this process...\n";
       kill `cat $PIDFILE`;
   } else {
       die "\nYou do not own this process. Wait until it is finished or call sysadmin.\n";
   }
}

# Write a pid file 
open (PID, "> $PIDFILE") || die "Cannot open $PIDFILE";
printf (PID "$$");
close (PID);

# Look for command line parameters
if ($ARGV[0] eq "-adr") {
    printf "Get Newton cards for Addressbook file\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "Addressbook";

} elsif ($ARGV[0] eq "-webadr") {
    printf "Get Newton cards for WebAddress files\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "webadr";

} elsif ($ARGV[0] eq "-update") {
    printf "Running update\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "update";

} elsif ($ARGV[0] eq "-ksendfax") {
    printf "Get Newton cards for ksendfax phonebook\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "ksendfax";

} elsif ($ARGV[0] eq "-xrolo") {

    if ($ARGV[1] eq "") {
	$XrolodexFile = $XrolodexDefaultFile;
    } else {
	$XrolodexFile = $ARGV[1];
    }

    printf "Get Newton notes for Xrolodex file\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "Xrolodex";

} elsif ($ARGV[0] eq "-kjots") {
    printf "Get Newton notes for Kjots file\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "kjots";


} elsif ($ARGV[0] eq "-plan") {
    printf "Get Newton dates for Plan file\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "plan";

} elsif ($ARGV[0] eq "-pr") {

    if ($ARGV[1] eq "") {
	$PrintFile = $PrintDefaultFile;
    } else {
	$PrintFile = $ARGV[1];
    }

    printf "Get Newton outbox entrys for printing\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "pr";

} elsif ($ARGV[0] eq "-fax") {
    #printf "Get Newton outbox entrys for faxing\n";
    #printf "Make sure Sloup is running on your Newton\n";
    $mode = "fax";

} elsif ($ARGV[0] eq "-mail") {
    printf "Get Newton outbox entrys for Email\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "mail";

} elsif ($ARGV[0] eq "-getpnam") {
    printf "Get Newton package names\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "getpnam";

} elsif ($ARGV[0] eq "-getpkg") {
    if ($ARGV[1] eq "") {
	printf "Exit - Package name missing\n";
	unlink $PIDFILE;
	exit;
    }
    if ($ARGV[2] eq "") {
	printf "Exit - Package filename missing\n";
	unlink $PIDFILE;
	exit;
    }
    $PackageName = $ARGV[1];
    $PackageFileName = $ARGV[2];
    printf "Get Newton package $PackageName to file $PackageFileName\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "getpkg";

} elsif ($ARGV[0] eq "-nsmail") {
    printf "Get Newton outbox entrys for Netscape email\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "nsmail";

} elsif ($ARGV[0] eq "-nsadr") {
    printf "Get Newton cards for Netscape email addressbook\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "nsadr";
    
} elsif ($ARGV[0] eq "-xfadr") {
    printf "Get Newton card entrys for Xfmail addressbook\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "xfadr";

} elsif ($ARGV[0] eq "-send") {
    if ($ARGV[1] eq "") {
	printf "Exit - file name missing\n";
	unlink $PIDFILE;
	exit;
    }
    if ($ARGV[2] eq "") {
        $SendFileSoup = "";
    } else {
        $SendFileSoup = $ARGV[2];
    }
    $SendFile = $ARGV[1];
    printf "Send file $SendFile to Newton";
    if ($SendFileSoup ne "") {
        printf " soup $SendFileSoup";
    }
    printf "\nMake sure Sloup is running on your Newton\n";
    $mode = "send";

} elsif ($ARGV[0] eq "-inst") {
    if ($ARGV[1] eq "") {
	printf "Exit - file name missing\n";
	unlink $PIDFILE;
	exit;
    } else {
	$PackageFile = $ARGV[1];
	printf "Install Newton package $PackageFile\n";
	printf "Make sure Sloup is running on your Newton\n";
	$mode = "install";
    }

} elsif ($ARGV[0] eq "-lpkg") {
    if ($ARGV[1] eq "") {
	printf "Exit - file name missing\n";
	unlink $PIDFILE;
	exit;
    } else {
	$LPackageFile = $ARGV[1];
	printf "Install Newton package $LPackageFile using lpkg\n";
	printf "Start the connection utility on your Newton\n";
	$mode = "lpkg";
    }

} elsif ($ARGV[0] eq "-input") {
    printf "Use PC keyboard for Newton input\n";
    printf "Put Sloup in inspect mode, finish input with '!!'\n";
    $mode = "input";

} elsif ($ARGV[0] eq "-soupinfo") {
    if ($ARGV[1] eq "") {
	printf "Exit - soup name missing\n";
	unlink $PIDFILE;
	exit;
    } else {
	$SoupName = $ARGV[1];
	printf "Make sure Sloup is running on your Newton\n";
	$mode = "soupinfo";
    }

} elsif ($ARGV[0] eq "-test") {
    printf "Newtonlink test mode activated\n";
    printf "Make sure Sloup is running on your Newton\n";
    $mode = "test";

} elsif ($ARGV[0] eq "") {
    $mode = "motif";

} elsif (($ARGV[0] eq "-help") || ($ARGV[0] eq "-h") || ($ARGV[0] eq "-?")) {
    printf "\nUsage   : newtonlink [options] [file]\n";
    printf "Options : -adr                       Get Newton cards for Addressbook file\n";
    printf "          -webadr                    Get Newton cards for WebAddress files\n";
    printf "          -ksendfax                  Get Newton cards for Ksendfax phonebook\n";
    printf "          -xrolo [file]              Get Newton notes for Xrolodex file\n";
    printf "          -kjots                     Get Newton notes for Kjots file\n";
    printf "          -plan                      Get Newton dates for Plan file\n";
    printf "          -pr [file]                 Get Newton outbox entrys for printing\n";
    printf "          -fax                       Get Newton outbox entrys for faxing\n";
    printf "          -mail                      Get Newton outbox entrys for email\n";
    printf "          -nsmail                    Get Newton outbox entrys for Netscape email\n";
    printf "          -nsadr                     Get Newton card entrys for Netscape email addressbook\n";
    printf "          -xfadr                     Get Newton card entrys for Xfmail addressbook\n";
    printf "          -getpkg [name] [file]      Get Newton package\n";
    printf "          -getpnam                   Get Newton package names\n";
    printf "          -send [file] [soup]        Send file to Newton\n";
    printf "          -inst [file]               Install Newton package\n";
    printf "          -lpkg [file]               Install Newton package using lpkg\n";
    printf "          -soupinfo [soupname]       Get informations about Newton soup\n";
    printf "          -input                     Use PC keyboard for Newton input\n";
    printf "          -help                      This help text\n";
    printf "          without param              Starting X-windows mode\n\n";
    unlink $PIDFILE;
    exit;

} elsif ($ARGV[0] eq "-onlinehelp") {

    printf "Starting $HTML_Browser for online help\n";
    if ($HTML_Browser eq "netscape") {
        system "$HTML_Browser -remote 'openUrl (file:S_NEWTONLINK_INSTALLDIR_S/doc/newtonlink-doc-en.html)' &";
    } else {
        system "$HTML_Browser S_NEWTONLINK_INSTALLDIR_S/doc/newtonlink-doc-en.html &";
    }
    unlink $PIDFILE;
    exit;

} else {
    printf "Command unknown !\n";
    unlink $PIDFILE;
    exit;
}

if ($mode eq "motif") {
    # start newtonlink Tk GUI
    system "$instdir/tknl.tcl &";
    unlink $PIDFILE;
    exit;
}

if ($mode eq "lpkg") {
    # install Newton package with lpkg
    &CallLpkg;
    unlink $PIDFILE;
    exit;
}

# try to connect to sloup and ask for several options
&GreetUser;
 
if (open (COMNEWTON, "+>$tty")) {
    printf "Newton connection opened\n";

    # set new baudrate
    printf "Switch baudrate to $SloupBaudrate\n";
    &SetSloupBaudrate;

    # It seems that IRIX needs first a CR to initialize the serial connection
    if ($SystemType eq "IRIX") { printf (COMNEWTON "\n"); }

    if ($mode eq "Addressbook") {
	# get cards from Newton
	&Get4Addressbook;

    } elsif ($mode eq "update") {
	# update all
	&GetPackageNames;
	$XrolodexFile = $XrolodexDefaultFile;
	&Get4Xrolodex;
	&Get4Kjots;
	&GetMeetings4Plan;
	&Get4Addressbook;
	&Get4KsendfaxPhonebook;
	&Get4WebAddress;
	&Get4NetscapeAddressbook;
	&Get4XfmailAddressbook;

    } elsif ($mode eq "webadr") {
	# get cards from Newton
	&Get4WebAddress;

    } elsif ($mode eq "ksendfax") {
	# get cards from Newton
	&Get4KsendfaxPhonebook;

    } elsif ($mode eq "Xrolodex") {
	# get notes from Newton
	&Get4Xrolodex;

    } elsif ($mode eq "kjots") {
	# get notes from Newton
	&Get4Kjots;

    } elsif ($mode eq "plan") {
	# get dates from Newton
	&GetMeetings4Plan;

    } elsif ($mode eq "pr") {
	# get outbox from Newton
	&GetPrintOutbox;

    } elsif ($mode eq "fax") {
	# get outbox from Newton
	&GetFaxOutbox;

    } elsif ($mode eq "mail") {
        # get outbox from Newton
        &Get4Mail;

    } elsif ($mode eq "nsmail") {
	# get outbox from Newton
	&Get4NetscapeMail;

    } elsif ($mode eq "nsadr") {
	# get cards from Newton
	&Get4NetscapeAddressbook;

    } elsif ($mode eq "xfadr") {
	# get cards from Newton
	&Get4XfmailAddressbook;

    } elsif ($mode eq "send") {
	# send file to Newton
	&SendFile2Newton;

    } elsif ($mode eq "getpnam") {
	# get Newton package names
	&GetPackageNames;

    } elsif ($mode eq "getpkg") {
	# get Newton package
	&GetPackage;

    } elsif ($mode eq "install") {
	# install Newton package
	&InstallPackage;

    } elsif ($mode eq "input") {
	# send keyboard to Newton
	&KeyboardInput;

    } elsif ($mode eq "soupinfo") {
	# get info about a Newton soup
	&GetSoupInfo;

    } elsif ($mode eq "test") {
	# activate Newtonlink test mode
	&SloupTest;

    }

    # close connection
    close COMNEWTON;
    printf "\nNewton connection closed\n";
    unlink $PIDFILE;
    exit;

} else {
    print STDERR "Can't open $tty\n";
    return;
}

















