#!/bin/sh
# ident "@(#)Setup 1.8     98/02/04 Bharat Mediratta"
#
# Configure PilotManager for the user's environment.
#
# 1. Find a version of Perl 5.  Warn the user if it is not
#    up to date.  Update PilotMgr.pm to use this version.
#
# 2. Run a series of tests on the user's environment to determine
#    whether or not PilotManager is going to be effective 
#

cat <<EOF

Welcome to the PilotManager setup utility.  Setup will be invoked
automatically the first time you run PilotManager.  After that, if you
wish to reconfigure PilotManager, you can run Setup by hand.

Setup will configure your installation of PilotManager.  It will
attempt to locate an appropriate version of Perl and configure
PilotManager to use that version.

Then, it will run a series of tests on your environment to determine
whether or not you will be able to run PilotManager without
difficulty.

Tips:
	- If you see a "relocation error" on any perl symbols then most
	  likely the binary packages you are using were not compiled for
	  your version of perl.  Return to the PilotManager web page and
	  make sure you retrieve the right binary packages.

EOF

echo "Press <RETURN> to continue";
read foo

VERS="5.003"
PERL=

echo "Locating Perl 5..."
for cmd in perl5 perl
do
	for dir in `echo $PATH | sed -e 's/:/ /g'`
	do
		if [ -n "$dir" -a -x "$dir/$cmd" ]
		then
			MAYBE=`$dir/$cmd -e 'print $^X'`
	
			if [ "$MAYBE" != "" ]
			then
				`$MAYBE -e "eval{require $VERS}; \
					    exit 0 if \\$@; exit 1"`
			fi

			if [ $? -eq 1 ]
			then
				PERL=$MAYBE
				break 2
			fi
		fi
	done
done

# Now, either we've found the right version of Perl and we 
# can continue, or we haven't and we should fail.
#

if [ -z "$PERL" ]
then
	cat<<EOF
Setup was unable to locate an appropriate version of
Perl from your path.  You need to have Perl v5.003 or
later in your path for PilotManager to work.
EOF
	exit 1;
fi

echo "Perl 5 found [at $PERL]"
echo "Please wait..."

cat > /tmp/pmgr-setup.pl <<EOF

use Config;

BEGIN
{
    use Cwd;
    my (\$PWD);
    \$PWD = Cwd::cwd();

    unshift(@INC, "\$PWD/lib/perl5/\$Config::Config{archname}/\$]");
    unshift(@INC, "\$PWD/lib/perl5/\$Config::Config{archname}");
    unshift(@INC, "\$PWD/lib/perl5");
    unshift(@INC, "\$PWD");
}

require 5.003;

package Setup;

$| = 1;

sub complain
{
    my (\$mod, \$err) = @_;

    print qq|
	You do not have the correct binary of the \$mod module
	installed in either your PilotManager distribution or your
	perl installation.

	Please read README.porting for information about how to 
        download and install this module.

        Error details:
-----------------------------------------------------------------------------
\$err-----------------------------------------------------------------------------
	    \n|;
    exit;
}

print "Configuring PilotManager...";

eval "use Tk;";
&complain("Tk", \$@) if (\$@);

eval "use PDA::Pilot;";
&complain("PDA::Pilot", \$@) if (\$@);

eval "use Data::Dumper;";
&complain("Data::Dumper", \$@) if (\$@);

if (! -f "PilotMgr.pm.$$")
{
    unless(rename("PilotMgr.pm", "PilotMgr.pm.$$"))
    {
	print "Unable to rename PilotMgr.pm\n";
	exit;
    }
}

if (open(IFD, "<PilotMgr.pm.$$"))
{
    if (open(OFD, ">PilotMgr.pm"))
    {
	# Replace the top line with our new perl exec binary
	#
	<IFD>;
	print OFD "#!$PERL\n";
	print OFD <IFD>;
	close(OFD);
    }
    else
    {
	print "\nError: Unable to write to PilotMgr.pm\n";
	rename("PilotMgr.pm.$$", "PilotMgr.pm");
	exit;
    }
    close(IFD);
}
else
{
    print "\nError: Unable to read from PilotMgr.pm.$$\n";
    exit;
}
chmod(0755, "PilotMgr.pm");
unlink("PilotMgr.pm.$$");

\$SIG{'INT'} = bail;

print "done.\n";
print qq|
PilotManager will now test your system to make sure
that all your conduits will function properly.

Press ^C at any time to abort the tests.

Press <RETURN> to continue.
|;
<STDIN>;

foreach \$file (<lib/test/*.t>)
{
    (\$name = \$file) =~ s|lib/test/(.*)\.t$|\$1|;

    print ">> \$name <<\n";

    do \$file;
}

sub bail
{
    print "Aborting tests\n";
    print "Your installation is complete, but not fully tested.\n";

    &done;
}

&done; 

sub done
{
    print qq|
Your PilotManager installation is complete.  You can invoke it 
by typing 'PilotManager' at your shell prompt.

Please file all bugs and feedback via the 'Feedback'
menu in PilotManager.
|; 
}

EOF

$PERL /tmp/pmgr-setup.pl

