#!/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.
##
##**************************************************************



# Generate a condor config files from stdin
while(1)
{
    chomp ( $cmd = <STDIN> );
    if( $cmd eq ".")
    {
	last;
    }

    ($var, $value) = split (/=/, $cmd);
    # What is the central manager of the pool for this node.  Doesn't apply
    # for central managers.
    if( $var eq "cm" )
    {
	$cm = $value;
    }
    # Where is the generic config file from which I can build the specific
    # config file.  This should be somewhere in the distribution
    elsif( $var eq "generic" )
    {
	$generic_file = $value;
    }
    # Where should the outputed config file be written to.
    elsif( $var eq "config" )
    {
	$config_file = $value;
    }
    # What is the local directory for log, spool, execute on every machine
    elsif( $var eq "local_dir" )
    {
	$local_dir_str = $value;
    }
    # Where are the machine specific local config files going to go?
    elsif( $var eq "local_config" )
    {
	$local_config_str = $value;
    }
    # What directory should be used for lock files.  Should be on a local
    # disk
    elsif( $var eq "lock" )
    {
	$lock = $value;
    }
    # What is the name of the schedd and master.  Only usefull for submit
    # only installs that must coexist with normal installs.
    elsif( $var eq "name" )
    {
	$name = $value;
    }
    # What is the path to the mail program
    elsif( $var eq "mail_path" )
    {
	$mail_path = $value;
    }
    # Whether we want a soft uid domain or not (True or False)
    elsif( $var eq "soft_uid")
    {
	$soft_uid = $value;
    }
    # The uid domain for this machine
    elsif( $var eq "uid_domain")
    {
	$uid_domain = $value;
    }
    # The filesystem domain
    elsif( $var eq "fs_domain")
    {
	$fs_domain = $value;
    }
    # The person who gets email.
    elsif( $var eq "condor_admin")
    {
	$condor_admin = $value;
    }
    # The place where binaries and libraries are install.  Known to GNU people
    # as prefix
    elsif( $var eq "release_dir")
    {
	$release_dir = $value;
    }
    else
    {
	print "Unknown variable: $var\n";
    }
}

local( $ckpt, $view );

open( CONFIG, ">$config_file" ) || 
    die "Can't open config file $config_file: $!\n";
open( GENERIC, "<$generic_file" ) || 
    die "Can't open generic config file $generic_file: $!\n";

if( $cm eq "condor.cs.wisc.edu" ) {
    $ckpt = "condor-ckpt.cs.wisc.edu";
    $view = "condor-view.cs.wisc.edu";
}

print "\nConfiguring global condor config file ... ";
while( <GENERIC> ) {
  SWITCH: {
      if( /^CONDOR_HOST.*$/ ) { $_ = "CONDOR_HOST\t\t= $cm\n"; last SWITCH; }
      if( /^RELEASE_DIR.*$/ ) { $_ = "RELEASE_DIR\t\t= $release_dir\n"; last SWITCH; }
      if( /^LOCAL_DIR.*$/ ) { $_ = "LOCAL_DIR\t\t= $local_dir_str\n"; last SWITCH; }
      if( /^LOCAL_CONFIG_FILE.*$/ ) { 
	  if( $local_config_str ) {
	      $_ = "LOCAL_CONFIG_FILE\t\t= $local_config_str\n";
	  } else {
	      $_ = "#LOCAL_CONFIG_FILE\t\t= \$(LOCAL_DIR)/condor_config.local\n";
	  }
	  last SWITCH;
      }  
      if( /^CONDOR_ADMIN.*$/ ) {
	  $_ = "CONDOR_ADMIN\t\t= $condor_admin\n";
	  last SWITCH;
      }
      if( /^MAIL.*$/ ) { $_ = "MAIL\t\t\t= $mail_path\n"; last SWITCH; }
      if( $lock && /^LOCK.*$/ ) { $_ = "LOCK\t\t\t= $lock\n"; last SWITCH; }
      if( $ckpt && /^#USE_CKPT_SERVER.*$/ ) { 
	 $_ = "USE_CKPT_SERVER\t\t= True\n"; last SWITCH; 
     }
      if( $ckpt && /^#CKPT_SERVER_HOST.*$/ ) {
	 $_ = "CKPT_SERVER_HOST\t= $ckpt\n"; last SWITCH; 
     }
      if( $name && /^#MASTER_NAME.*$/ ) { 
	 $_ = "MASTER_NAME\t= $name\@\$(FULL_HOSTNAME)\n"; last SWITCH; 
     }
      if( $name && /^#SCHEDD_NAME.*$/ ) { 
	 $_ = "SCHEDD_NAME\t= $name\@\$(FULL_HOSTNAME)\n"; last SWITCH; 
     }
      if( $view && /^#CONDOR_VIEW_HOST.*$/ ) {
	 $_ = "CONDOR_VIEW_HOST\t= $view\n"; last SWITCH; 	     
     }
      if( $soft_uid && /^#SOFT_UID_DOMAIN.*$/ ) {
	 $_ = "SOFT_UID_DOMAIN\t= $soft_uid\n"; last SWITCH; 	     
     }
      if( $uid_domain && /^UID_DOMAIN.*$/ ) {
	  $_ = "UID_DOMAIN\t= $uid_domain\n"; last SWITCH; 	     
      }
      if( $fs_domain && /^FILESYSTEM_DOMAIN.*$/ ) {
	  $_ = "FILESYSTEM_DOMAIN\t= $fs_domain\n"; last SWITCH; 	     
      }
  }
    print CONFIG "$_";
}
print "done.\n";
print "Created $config_file.\n";
close CONFIG;
close GENERIC;

