#!/bin/bash
# the next line restarts using wish \
exec wish "$0" "$@"

# This program configures DOSEmu. 
#
# This script is (c) Copyright 1997 Alistair MacDonald 
#                                   <alistair@slitesys.demon.co.uk>
#
# 980613 changes by Hans Lermen <lermen@fgan.de>
#
# It is made available to the DOSEmu project under the GPL Version 2, or at
# your option any later version. Details of this Licence can be found in the
# file 'COPYING' in the distribution's top level directory.
#
# Version 0.1
#  Initial Version.
#

# source our browse functions
source browse.tk

#A Few settings
set temp "$env(HOME)/.dosemu/tmp/compiletime_setup."
append temp [ pid ]

if {[info exists env(TOPDIR)]} {
    set conf_name [ set env(TOPDIR) ]
} else {
    set conf_name ".."
}
append conf_name "/compiletime-settings"


# Housekeeping ...

proc tkerror { errorstring } {
    toplevel .error -class Dialog
    wm title .error "Error"

    message .error.message -text $errorstring
    button .error.exit -text \"OK\" -command { destroy .error }
    pack .error.message .error.exit 
}

proc clean_up {} {
    global temp

    catch { exec rm $temp }

    exit
}

proc positionWindow w {
  wm geometry $w +100+100
}

proc positionWindowRelative {w masterw dx dy} {
  set xx [split [wm geometry $masterw] +]
  wm geometry $w "+[expr [lindex $xx 1] + $dx]+[expr [lindex $xx 2] + $dy]"
}

proc positionSubWindow {w} {
  positionWindowRelative $w . 0 +45
}

proc load_config {} {
    global temp
    global conf_name

    .status configure -text "Reading Compile-time Configuration ..."
    pack .status
    update idletasks

    catch { exec awk -f parse-config -f parse-misc -f parse-config-tk \
		$conf_name > $temp }
    source $temp
}

proc load_menus {} {
    global temp

    catch { 
	.status configure -text "Loading Menus ..."
	pack .status
	update idletasks
    }

    catch { exec awk -f parse-menu -f parse-misc -f parse-menu-tk \
		compiletime_setup.menu > $temp }
    source $temp
}

proc do_menus {} {
    .status configure -text "Running Menus ..."
    pack .status
    update idletasks

    start
}

proc handle_help { help_item } {
     global helptx helpcount
     show_readme_text helptx $helpcount {compiletime-settings help} ""
#    display_help "Help - $help_item" "Sorry - Help is not available at the moment"
}

proc display_help { help_title help_text } {
    toplevel .help -class Dialog
    positionSubWindow .help
    wm title .help $help_title

    message .help.message -text $help_text
    pack .help.message

    button .help.exit -text "Exit" -command { destroy .help }
    pack .help.exit
}

proc get_value { var_name } {
    global var_array
    
    # Initial dummy ...
    set temp_value ""

    set config_name "config_$var_name"

    if {[ info exists var_array($var_name) ]} {
	set temp_value [ set var_array($var_name) ]
    } else {
	catch {
	    set temp_value [ set var_array($config_name) ]
	}
    }

    return $temp_value
}

proc show_error { error_message } {
    catch [ toplevel .error -class Dialog ]
    positionSubWindow .error
    wm title .error "Error"

    message .error.message -text $error_message

    button .error.exit -text "OK" -command { destroy .error }

    pack .error.message .error.exit
}

proc Main_Menu_exit {} {
    handle_Configure_Action
}

proc init_system {} {
    global var_array

    positionWindow .
    wm title . "DOSEMU Compile-Time Configuration - Status" 
    label .description -text "Status:" 
    label .status -text "Choose a Configuration File" -relief sunken
    pack .description .status -side left
    update idletasks

    set var_array(config_configure) "abort"
}

proc init_help {} {
    global helptx helpcount readme
    set helptx ""
    set helpcount 0
    set readme "../compiletime-settings.help"
    readin_readme $readme helptx helpcount {The options have} ""
}

proc Configure_Action_exit {} {
    write_out
    clean_up
    exit
}

proc write_pairs {  option_group  item_list } {
    global tmp_file

    puts -nonewline $tmp_file "config\[\"$option_group\"\] = \"$option_group {\\n"

    foreach  this_item  $item_list  {

	set item_value [ get_value $this_item ]

	if { [string length $item_value] > 0 } then {
	    puts -nonewline $tmp_file "  $this_item $item_value\\n"
	}
    }

    puts $tmp_file "}\""
}

proc write_out {} {
    global tmp_file
    global temp
    global conf_name

    .status configure -text "Checking Action ..."
    pack .status
    update idletasks

    set action [ get_value "configure" ]

    .status configure -text "Action is $action ..."
    pack .status
    update idletasks

    if { ([ string compare $action save ] == 0) ||
	 ([ string compare $action configure ] == 0) } then {
	.status configure -text "Writing Results ..."
	pack .status
	update idletasks

	set tmp_file [ open $temp w ]

	puts $tmp_file "function setup_config () {"

	write_pairs  config  { "experimental" "sbemu" "mitshm" \
				   "x" "net" "dodebug" "slangforce" \
				   "runasroot" "linkstatic" \
				   "newint" \
				   "aspi" }

	puts $tmp_file "}"
	close $tmp_file

	.status configure -text "Writing Configuration ..."
	pack .status
	update idletasks

	catch { exec cp -f $conf_name $conf_name~ 2> /dev/null }
	catch { exec touch $conf_name }
	catch { exec awk -f parse-misc -f write-config -f $temp \
		    $conf_name > $conf_name.tmp }
	catch { exec mv $conf_name.tmp $conf_name }
    }

    if { ([ string compare $action configure ] == 0) } then {
	if [file exists ../Makefile.conf] {
	    .status configure -text "Doing 'make pristine'  ..."
	    pack .status
	    update idletasks
	    catch { exec /bin/bash -c "cd .. && make pristine" }
	}
	.status configure -text "Running Configure ..."
	pack .status
	update idletasks
        set result ""
	catch {exec cp -f $conf_name $temp  2> /dev/null }
	catch {set result [exec /bin/bash -c "cd .. && ./default-configure $temp 2>&1"]}
	if {[display_output "configure results" "Exit" "Compile Dosemu" 1 result]} {
	    .status configure -text "Compiling Dosemu  ..."
	    pack .status
	    update idletasks
	    catch {set result [exec /bin/bash -c "cd .. && make WAIT= 2>&1"]}

	    display_output "Compile results" "Ok" "" 1 result
	}
    }	

    .status configure -text "Done ..."
    pack .status
    update idletasks
}

init_help
init_system
load_config
load_menus
do_menus

