#!/usr/bin/perl

use strict;
use warnings;


###########################################################################
##                 modified by baktcheev ivan in 2002                     #
###########################################################################


###  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 ARDB-home dir value...

my $existing_setting; 
my $new_setting;

my $dir;

my $conf_filename = "ARDB/LocalConfig.pm";
my $config_module = "ARDB::LocalConfig";

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

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

HERE

  if ( $dir )
   {
    print OUT "\$local_path = '$dir';\n\n  1;\n";
   }
  else
   {
    print OUT "\n1;\n";
   }

  close OUT;
  print "\nSaved lib/$conf_filename. Current configuration:\n\n";
  undef $ARDB::LocalConfig::local_path;
  eval { do ( 'lib/'.$conf_filename ); };
  display_config();
  print "\n";
}


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

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

sub display_config 
 {    
  print "\tARDB data store directory: ";
    
  if (defined $ARDB::LocalConfig::local_path)
   {
    print $ARDB::LocalConfig::local_path;
   }
  else
   {
    print "not set";
   }
  print "\n";
  return 1;
}


#package ARDB;

use ExtUtils::MakeMaker;
use Cwd;


print <<MSG;

ARDB data store directory is where you store your ARDB data, your
configuration files for ARDB-related programs and other related
stuff.  Some programs require it for operation. You MUST set it now
and ARDB will use it always BY DEFAULT.

MSG



eval { require ARDB::LocalConfig; };

my $full_conf_filepathname = $INC{$conf_filename};

if( $@ ) {
#  warn "eval failed, no LocalConfig probably";
}


if( defined $ARDB::LocalConfig::local_path ) {
  $existing_setting = $ARDB::LocalConfig::local_path;
#    warn "existing configuration: $existing_setting";
}


if( $existing_setting ) {

  print 
qq!Your system is currently configured to use the following
directory as ARDB home by default:

   $existing_setting

taken from $full_conf_filepathname)

!;
  
  if( not get_bool ( "Do you wish to change that?", 0 ) ) {
    $new_setting = $existing_setting;
    write_config( $existing_setting );
    return 1;
    
  } else  {
    $new_setting = '';
  }

} else {

  if( get_bool ( "Do you wish to set default ARDB data store directory now?", 1 ) ) {
    $new_setting = '';

  } else {
    write_config( 0 );
    return 1;
  }
}

if ( defined $new_setting )
 {
  $dir = ExtUtils::MakeMaker::prompt ( "What directory to use as the default ARDB data store?", 
                  "enter for not setting it now" );
  if( $dir eq "enter for not setting it now" or not $dir )
   {
    write_config ( 0 ) ;
    return 1;
   }
  else
   { 
    print $dir;
    write_config ( $dir ) ;
    return 1;
   }

 }



1;
