#!/usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************


# customize
#
# Perl script that performs platform-specific customization of the
# various files here and creates the proper versions in the
# target directory specified on the command line.
#
# Written by Derek Wright <wright@cs.wisc.edu> on 2/27/98
#

$target = $ARGV[0];
-d $target || die "$target is not a directory.\n";

&get_platform_defaults();
&customize_config_file( "condor_config.generic",
		       "$target/etc/examples/condor_config.generic" );
&customize_config_file( "condor_config.submit.generic",
		       "$target/etc/examples/condor_config.submit.generic" );
&customize_config_file( "condor_config.local.central.manager",
	       "$target/etc/examples/condor_config.local.central.manager" );
&customize_boot_script();


sub get_platform_defaults {
    local( $mach, $os );
    chop($mach = `uname -m`);
    chop($os = `uname`);
    $console_devs="";
    $needs_kbd=0;
    $special_arch_string="";

    $_ = $os;
  SWITCH: {
      if(/^Linux/) { 
	  $mail_path="/usr/bin/mail";
	  $ps_path="/bin/ps auwx";
	  $console_devs="mouse, console";
	  last SWITCH;
      }
      if(/^Darwin/) { 
	  $mail_path="/usr/bin/mail";
	  $ps_path="/bin/ps auwx";
	  $console_devs="";
	  last SWITCH;
      }
      if(/^HP-UX/) {
	  $ps_path="/bin/ps -ef";
	  $console_devs="ps2mouse, ps2kbd, hil1, hil2, hil3, hil4, hil5, hil6, hil7";
	  $mail_path="/bin/mailx";
	  last SWITCH;
      }
  }
}


sub customize_config_file {
    local( $generic, $target ) = @_;
    
    -f $target && unlink( $target );
    
    if( $generic =~ /.*submit.*/ ) {
	$wants_kbd = 0;
    } else {
	$wants_kbd = $needs_kbd;
    }

    open( TARGET, ">$target" ) ||
	die "Can't open $target: $!\n";
    open( GENERIC, "<$generic" ) || 
	die "Can't open $generic: $!\n";
    
    print "Configuring $generic ... ";
  LINE: while( <GENERIC> ) {
    SWITCH: {
	if( /^MAIL.*$/ ) { $_ = "MAIL\t\t\t= $mail_path\n"; last SWITCH; }
	if( /^#CONSOLE_DEVICES.*$/ ) {
	   if( $console_devs ) {
	       $_ = "CONSOLE_DEVICES\t= $console_devs\n";
	   }
	   last SWITCH;
        }
	if( /^DAEMON_LIST.*$/ ) {
	    if( $wants_kbd ) {
		chop($_);
		$_ = "$_, KBDD\n";
	    } 
	    last SWITCH;
	}
    }
      print TARGET $_;

  }
    print "done.\n";
    close TARGET;
    close GENERIC;
}


sub customize_boot_script {
    open( GENERIC, "<condor.boot.generic" ) || 
	die "Can't open condor.boot.generic: $!\n";
    open( TARGET, ">$target/etc/examples/condor.boot" ) ||
	die "Can't open target condor.boot: $!\n";

    print "Configuring condor.boot.generic ... ";
    while( <GENERIC> ) {
	if( /^PS.*/ ) {
	    $_ = "PS=\"$ps_path\"\n";
	}
	print TARGET $_;
    }
    print "done.\n";
    close TARGET;
    close GENERIC;
    chmod( 0755, "$target/etc/examples/condor.boot" );
}    
