#!/usr/bin/perl

# Configure for ReDIF perl toolkit

##  Copyright (c) 1999 Ivan Kurmanov. 
##  Portions copyright (c) 1996, 1997 Graham Barr. 
##  All rights reserved.
##  This code is free software; you can redistribute it and/or modify it
##  under the same terms as Perl itself.

## portions taken from Configure of Libnet-1.0606 package.

# writes a tiny perl module file with a ReDIF-home dir value...

package ReDIF;

use ExtUtils::MakeMaker ;
use Cwd;


### Digest::MD5 checksum calculation module option:

use vars qw( $md5_checksum_option $length_checking_option );

eval { require Digest::MD5; } ;
if( $@ ) { 

    $md5_checksum_option = 0; 
    
    print "\nYou don't have Digest::MD5 module installed. MD5 checksum\n"
        , "calculation for templates is disabled.\n\n";
    
} else {
    $md5_checksum_option = 
        get_bool( "\nYour system has Digest::MD5 module installed.\n"
                  . "Do you want to enable MD5 checksum calculation for templates?", 'yes' ); 

}

$length_checking_option = get_bool( "\nDo you want ReDIF-perl to check attribute values\n"
                                    . "for a reasonable length? (early implementation)",
                                    0 );


###############################################
sub Prompt {
    my($prompt,$def) = @_;
    
    $def = "" unless defined $def;
    
    chomp($prompt);

    ### if default
    # print $prompt,," [",$def,"]\n";
    # return $def;

    ExtUtils::MakeMaker::prompt( $prompt, $def );
  
}

sub get_bool ($$) {
    my($prompt,$def) = @_;

    chomp($prompt);
    
    my $val = Prompt($prompt,$def ? "yes" : "no");
    
    $val =~ /^y/i ? 1 : 0;
}

###############################################

print <<MSG;

ReDIF home directory is where you store your ReDIF data, your
configuration files for ReDIF-related programs and other related
stuff.  Some programs require it for operation, but FOR ReDIF-perl
INSTALLATION IT IS NOT REQUIRED.  You can set it now and ReDIF-perl
will use it always BY DEFAULT.  Otherwise you will have to pass the
path to ReDIF home by other means to some programs (e.g. through
environment variable REDIFDIR).

MSG
    ;    

####  OLD instructions message

# This script will help you to configure ReDIF perl package 
# for your local installation. This means we will now define 
# default ReDIF home directory name, that will be used by ReDIF 
# tools to find it's components.
#
# If you do not know what does that mean, or if you have not yet
# prepared a directory to become ReDIF home, please answer NO now.
# 
# Please consult README file for more information.
#

   

my $EXISTING_SETTING; 
my $NEW_SETTING;

$CONF_FILENAME = "ReDIF/setup_conf.pm" ;
$OUT_CONF_FILENAME = "lib/ReDIF/setup_conf.pm" ;

# $OUT_CONF_FILENAME = "setup_conf.pm" ;

my $new;
my $t1 = eval {
            do ( $CONF_FILENAME );
#           require( $OUT_CONF_FILENAME );
        };

my $full_conf_filepathname = $INC{$CONF_FILENAME};

if( $ReDIF::CONFIG_candidate{redif_home} ) {
    $EXISTING_SETTING = $ReDIF::CONFIG_candidate{redif_home} ;
#    warn "EXISTING CONFIGURATION: $EXISTING_SETTING";
}

if( $@ ) {
#    warn "eval failed";
}

if( $EXISTING_SETTING ) {
    print "Your system is currently configured to use the following \ndirectory as ReDIF home by default:\n\n",
    "\t$EXISTING_SETTING\n\n(taken from $full_conf_filepathname)\n\n";
    
    my $t = get_bool ( "Do you wish to change that?", 0 );

    if( not $t ) {
        $NEW_SETTING = $EXISTING_SETTING; 
        write_config( $EXISTING_SETTING );
        return 1;

    } else {
        $NEW_SETTING = '';
    }
} else {
    my $t = get_bool ( "Do you wish to set default ReDIF home directory now?", 1 );
    if( $t ) {
        $NEW_SETTING = '';
    } else {
        write_config( 0 );
        return 1;
    }
}

if ( defined $NEW_SETTING ) {

    $dir = prompt ( "What directory to use as the default ReDIF home?", 
                    "enter for not setting it now" );
    
    if( $dir eq "enter for not setting it now" 
        or not $dir ) {
        write_config ( 0 ) ;
        return 1;

    } else { 
        write_config ( $dir ) ;
        return 1;
    }

}


sub write_config {
    my $dir = shift;
    
    open( OUT, ">$OUT_CONF_FILENAME" )
        or die "Cannot open $OUT_CONF_FILENAME for writing"; 

    print OUT <<SETUP_CONF_START;
    
# Please do not modify this unless you know what you are doing.
# created by Configure of the ReDIF perl package.
    
package ReDIF;

###  Digest::MD5 checksum calculation option

BEGIN { 
    package ReDIF::Parser;
    use constant calculate_md5_checksum => $md5_checksum_option;
    use constant value_length_checking  => $length_checking_option;
}


SETUP_CONF_START
    ;

    if( $dir ) {
        
    print OUT <<SETUP_CONF_END;
    
\$CONFIG_candidate{redif_home} = \'$dir\';

# \$CONFIG_candidate{conf_path} = '$dir/etc';
# \$CONFIG_candidate{spec_path} = '$dir/spec';
# \$CONFIG_candidate{data_path} = '$dir/data';


1;

SETUP_CONF_END

} else {

    print OUT <<SETUP_CONF_END;
    
    1;
SETUP_CONF_END
    
}

    close OUT;

    print "\nSaved $OUT_CONF_FILENAME. Current configuration:\n\n";

    undef $ReDIF::CONFIG_candidate{redif_home} ;

    my $t2 = eval {
        do ( $OUT_CONF_FILENAME );
    };
    
    display_config();
    
    print "\n";
}




############################

#===================
sub display_config {
#===================
    
    print "\tReDIF home directory: ";
    
    if (defined $CONFIG_candidate{redif_home}) {
        print $CONFIG_candidate{redif_home};
    } else {
        print "not set";
    }
    print "\n";
    return 1;
}

1;


__END__

if (not $@) {
        print <<MSG;
Found an existing configuration:
MSG
    display_config();
    print "\n";
    $new = "(new) ";
}

my $t = get_bool ( "Do you want to set ${new}ReDIF home dir now?", 'yes' );
if ( not $t ) {
    return 1;
}


my $cur_dir = getcwd ();

$dir = prompt ( "What directory to use as the default ReDIF home?", $cur_dir );

print "\n";

## if ( $dir =~ /^\s+$/ ) {
##   print "Empty (whitespace) value received, no configuration saved.\n";
##   return 1;
## }

if ( -e $dir and -d _ ) {
     print "Good, directory exists.\n";
} else {
     print "<$dir> dir doesn't exists. ";
     my $t = get_bool ( "Do you want me to create it?", 'yes' );
     if ( $t ) {
         mkdir $dir, 0755;
         if (-e $dir) {
             print "Created succesfully.\n";
         } else {
             print "Failed ($!).\n"; 
   #         return 0;
         }
     }
    if ( not -e $dir ) {
       print "Please create the directory yourself later.\n";
    }
}

open( OUT, ">$OUT_CONF_FILENAME" )
     or die "Cannot open $OUT_CONF_FILENAME for writing"; 

print OUT <<SETUP_CONF_END;

# Please do not modify this unless you know what you are doing.
# created by Configure of the ReDIF perl package.

package ReDIF;

\$CONFIG_candidate{redif_home} = \'$dir\';

# \$CONFIG_candidate{conf_path} = '$dir/etc';
# \$CONFIG_candidate{spec_path} = '$dir/spec';
# \$CONFIG_candidate{data_path} = '$dir/data';

1;

SETUP_CONF_END


close OUT;

print "\nSaved $OUT_CONF_FILENAME. Current configuration:\n";
        my $t2 = eval {
            do ( $OUT_CONF_FILENAME );
        };

display_config()
  or print "\n...Error..."; 

print "\n";

1;












