#!/usr/local/bin/perl
#  install.pl - the cbb install utility
#              This script installs cbb
#
#  Written by Curtis Olson.  Started October 11, 1994.
#
#  Copyright (C) 1994  Curtis L. Olson  - curt@sledge.mn.org
#
#  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.

# $Id: install.pl,v 1.3 1994/10/14 19:06:42 clolson Exp $
# (Log is kept at end of this file)

# default paths
$def_perlpath = "/usr/local/bin/perl";
$def_wishxpath = "/usr/local/bin/wishx";
$def_binpath = "/usr/local/bin";
$def_libpath = "/usr/local/lib/cbb";
$def_cbb = "cbb";
$def_engine = "engine.pl";
$def_cat = "default.cat";
$def_version = "Version";
$def_xbm = "author.xbm";


print "\nInstalling and Configuring cbb:\n";
print "Press ^C at any time to abort.\n\n";
print "WARNING:  This script hasn't been extensively tested.  Please let me\n";
print "          know if you find a problem with it.  <curt@sledge.mn.org>\n\n";


# Ok, first lets find out the current version

open( VERSION, "$def_version" );
$version = <VERSION>;
chop($version);

print "Current version is \"$version\"\n\n";

# Now, make sure we have the prerequisites.

$perlpath = "";
while ( $perlpath eq "" ) {
    print "Where is perl located? [$def_perlpath] ";
    $perlpath = <STDIN>;
    chop($perlpath);

    if ( $perlpath eq "" ) {
	$perlpath = $def_perlpath;
    }

    if ( ! -e $perlpath ) {
	$perlpath = "";
    }
}
print "    Using $perlpath\n";

$wishxpath = "";
while ( $wishxpath eq "" ) {
    print "Where is wishx located? [$def_wishxpath] ";
    $wishxpath = <STDIN>;
    chop($wishxpath);

    if ( $wishxpath eq "" ) {
	$wishxpath = $def_wishxpath;
    }

    if ( ! -e $wishxpath ) {
	$wishxpath = "";
    }

    if ( $wishxpath =~ m/wish$/ ) {
	print "    Warning:  cbb cannot run with wish, it requires wishx.\n";
	print "    See the README file for more information.\n";
	$wishxpath = "";
    }
}
print "    Using $wishxpath\n";


# Now, where do we want to install things ...

$binpath = "";
while ( $binpath eq "" ) {
    print "Where should the binaries be installed? [$def_binpath] ";
    $binpath = <STDIN>;
    chop($binpath);

    if ( $binpath eq "" ) {
	$binpath = $def_binpath;
    }

    if ( ! -e $binpath || ! -d $binpath ) {
	print "    $binpath does not exist.  Should I create it? [y] ";
	$result = <STDIN>;
	chop($result);
	if ( ($result =~ /^[Yy]/) || ($result eq "") ) {
	    $status = system( "mkdir $binpath" );
	    if ( $status ) {
		$code = $status / 256;
		print"\'mkdir $binpath\' failed with an exit code of ";
		print"$code.\n";
		exit( $code );
	    }
	} else {
	    print "    Very well ... but you better do it yourself. ;)\n";
	}
    }

    if ( $binpath =~ m/wish$/ ) {
	print "    Warning:  cbb cannot run with wish, it requires bin.\n";
	print "    See the README file for more information.\n";
	$binpath = "";
    }
}
print "    Using $binpath\n";

$libpath = "";
while ( $libpath eq "" ) {
    print "Where should the support files be installed? [$def_libpath] ";
    $libpath = <STDIN>;
    chop($libpath);

    if ( $libpath eq "" ) {
	$libpath = $def_libpath;
    }

    if ( ! -e $libpath || ! -d $libpath ) {
	print "    $libpath does not exist.  Should I create it? [y] ";
	$result = <STDIN>;
	chop($result);
	if ( ($result =~ /^[Yy]/) || ($result eq "") ) {
	    $status = system( "mkdir $libpath" );
	    if ( $status ) {
		$code = $status / 256;
		print"\'mkdir $libpath\' failed with an exit code of ";
		print"$code.\n";
		exit( $code );
	    }
	} else {
	    print "    Very well ... but you better do it yourself. ;)\n";
	}
    }

    if ( $libpath =~ m/wish$/ ) {
	print "    Warning:  cbb cannot run with wish, it requires lib.\n";
	print "    See the README file for more information.\n";
	$libpath = "";
    }
}
print "    Using $libpath\n";


# install cbb

print ( "Creating $binpath/$def_cbb from ./$def_cbb\n" );

open( CBBIN, "<./$def_cbb" ) || die "Cannot open ./$def_cbb";
open( CBBOUT, ">$binpath/$def_cbb" ) || die "Cannot create $binpath/$def_cbb";

$junk = <CBBIN>;	# ignore first line
print( CBBOUT "#!$wishxpath -f\n" );

while ( <CBBIN> ) {
    s/^ *set def_cat_path.*$/set def_cat_path $libpath/;
    s/\"Version.*\"/\"$version\"/;
    s/^ *set author_xbm.*$/set author_xbm \"$libpath\/author.xbm\"/;
    print( CBBOUT $_ );
}

close( CBBIN );
close( CBBOUT );

chmod(0755, "$binpath/$def_cbb") || die "Cannot chmod $binpath/$def_cbb";


# install engine.pl

print ( "Creating $binpath/$def_engine from ./$def_engine\n" );

open( ENGINEIN, "<./$def_engine" ) || die "Cannot open ./$def_engine";
open( ENGINEOUT, ">$binpath/$def_engine" ) 
	|| die "Cannot create $binpath/$def_engine";

$junk = <ENGINEIN>;	# ignore first line
print( ENGINEOUT "#!$perlpath\n" );

while ( <ENGINEIN> ) {
    print( ENGINEOUT $_ );
}

close( ENGINEIN );
close( ENGINEOUT );

chmod(0755, "$binpath/$def_engine") || die "Cannot chmod $binpath/$def_engine";


# install default.cat

print ( "Creating $libpath/$def_cat from ./$def_cat\n" );

open( CATIN, "<./$def_cat" ) || die "Cannot open ./$def_cat";
open( CATOUT, ">$libpath/$def_cat" ) 
	|| die "Cannot create $libpath/$def_cat";

while ( <CATIN> ) {
    print( CATOUT $_ );
}

close( CATIN );
close( CATOUT );

chmod(0644, "$libpath/$def_cat") || die "Cannot chmod $libpath/$def_cat";


# install author.xbm

print ( "Creating $libpath/$def_xbm from ./$def_xbm\n" );

open( XBMIN, "<./$def_xbm" ) || die "Cannot open ./$def_xbm";
open( XBMOUT, ">$libpath/$def_xbm" ) || die "Cannot create $libpath/$def_xbm";

while ( <XBMIN> ) {
    print( XBMOUT $_ );
}

close( XBMIN );
close( XBMOUT );

chmod(0644, "$libpath/$def_xbm") || die "Cannot chmod $libpath/$def_xmb";

# ----------------------------------------------------------------------------
# $Log: install.pl,v $
# Revision 1.3  1994/10/14  19:06:42  clolson
# Fixed a problem:  didn't add -f to the end of wishx in the installed cbb.
#
# Revision 1.2  1994/10/14  03:04:49  curt
# First working version.
#
# Revision 1.1  1994/10/11  21:21:17  clolson
# Initial add to repository
#
