#!/bin/sh
# $Id: SetupChklogs,v 1.2.2 1997/09/28 19:54:22 grimaldo Rel $
#---------------------------------------------------------------------
# AUTHOR: Copyright 1997 D. Emilio Grimaldo Tunon
# DESCRIPTION
#		A Tcl/Tk based Installation program to setup and
#		mail the registration. Not completely perfect but
#		should do the job. If a parameter is provided (only one!)
#		it is assumed that our makefile exists in that directory.
#		i.e.   ./SetupChklogs.tk ../
#
# Now make a wish \
exec wish "$0" "$@"

#-----------------------------------------------
# myLib	      To find Register program
# myDoc	      To find the license.txt file
set myLib       "."
set myDoc       ".."
set dist_target "target_ck"
set inst_target "install"
#set dist_target "test_distrib"
#set inst_target "test_install"
set imgPath	"."
set cdkConf(cdk_home)	 "/usr/local/lib/chklogs"
set cdkConf(syslog_conf) "/etc/syslog.conf"
set cdkConf(pid_dir)     "/var/run"
set cdkConf(syslog_pid)	 "syslog.pid"
set cdkConf(cdk_admin)   "root"
set cdkConf(mailhost)    "localhost"
set cdkConf(perl_bin)	 "/usr/bin/perl"
set cdkConf(gzip_bin)	 "/bin/gzip"
set cdkConf(gzip_ext)	 "gz"

set txtTitle  "License"
set butA  "Accept"
set butB  "Quit"

lappend auto_path "../img"
lappend auto_path "img"

proc registerInfo { widget } {
    global txtTitle myLib
    global butA butB

    set txtTitle "Registration Card"
    set butA  "Mail registration"
    set butB  "Don't Mail"
    .bot.butA config -command { mail_registration "yes" }
    .bot.butB config -command { mail_registration "no" }

    $widget delete 0.0 end
    set f [open "|$myLib/Register --show" r]
    while {[gets $f line] >= 0} {
        set line [format "%s\n" $line]
	$widget insert end $line
    }
    close $f
    whyRegister
#    .bot.butX configure -state normal
}

proc whyRegister {} {
    set msg "1. Advance notification of future releases/updates\n \
            2. I have a better idea of the user base\n \
	    3. Better info on which platforms chklogs is installed\n \
	    5. I need your input\n"
    tk_messageBox -icon info -message $msg -parent . -title "Why Register?" \
        -type ok
}

proc showLicense { widget } {
    global myDoc

    $widget delete 0.0 end

    set f [open "$myDoc/license.txt" r]
    while {[gets $f line] >= 0} {
        set line [format "%s\n" $line]
	$widget insert end $line
    }
    close $f
}

proc mail_registration { auth } {
    global butA butB
    global txtTitle 
    global myLib argv0 myself

    .bot.butX configure -state normal
    # It is possible that we have changed to another directory
    # to execute the make...
    if { [file exists $myLib/Register] == 0} {
	# Hum... it isn't where we expected. It is supposed to be where
	# this setup program is located.
	set myLib [string range $argv0 0 [expr [string last / $argv0] - 1]]
	puts "Hummm...changing myLib to $myLib"
	if { [file exists $myLib/Register] == 0} {
	    set myLib [string range $myself 0 [expr [string last / $myself] - 1]]
	    puts "Hummm! that didn't work, last try is $myLib"
	}    
    }

    # Let's leave the registration data in the log window. If we
    # don't like that anymore then uncomment next line.
#    .mid.text.txt delete 0.0 end
    if { [string compare $auth "yes"] == 0 } {
	puts "mailing registration"
	set cmd "$myLib/Register"
	if [ catch { exec $cmd --mail --check } result ] {
	    .mid.text.txt insert end "Could not mail :-(, you will have\n"
	    .mid.text.txt insert end "to mail it yourself...\n"
	} else {
	    .mid.text.txt insert end "Registration mailed, Thank You."
	}
    } else {
	set cmd "$myLib/Register"
	if [ catch { exec $cmd --save } result ] { puts "failed to save register.txt $result"}
	.mid.text.txt insert end "ok, should you decide to send it your\n"
	.mid.text.txt insert end "registration later, mail the file named\n\n"
	.mid.text.txt insert end "      support/register.txt\n\n"
	.mid.text.txt insert end "to register@panama.iaehv.nl\n"
	.mid.text.txt insert end "or use the command\n"
	.mid.text.txt insert end "   support/Register\n\n"
	.mid.text.txt insert end "\n\nThank You."
    }
    set txtTitle "Thank You"
    set butA  "Install"
    set butB  "Finish"
    .bot.butA config -command { install_package }
    .bot.butB config -command { destroy . }
    .bot.butA configure -state disabled
}

proc label_entry { parent widget lbl ent } {
    global cdkConf
    

    set w "$parent.$widget"
    frame $w -borderwidth 1 -relief raised
        label $w.lbl -text $lbl -anchor w 
	entry $w.ent -width 20 -textvariable cdkConf($ent) -relief sunken
	pack $w -anchor e -side top
	pack $w.lbl -anchor center -expand 0 -padx 2 -pady 2 -side left
	pack $w.ent -anchor center -expand 1 -padx 2 -pady 2 -side right \
	    -fill x
}

proc configure_package {} {
    global cdkConf

    toplevel .cfg
    	wm title .cfg "Configuration"
	wm iconname .cfg "ChkLogs"
        label_entry .cfg home    "Install Dir:" "cdk_home"
        label_entry .cfg syslCnf "Syslog config path/file:" "syslog_conf"
        label_entry .cfg syslPid "Syslog PId file:" "syslog_pid"
        label_entry .cfg pidDir "PId directory:" "pid_dir"
        label_entry .cfg admin  "Admin user:" "cdk_admin"
        label_entry .cfg perl   "Perl binary:" "perl_bin"
        label_entry .cfg gzipP   "Zipper/Compressor:" "gzip_bin"
        label_entry .cfg gzipE   "Compressed file extension:" "gzip_ext"

	frame .cfg.but
	    button .cfg.but.ok -text "OK" -command "perform_config; destroy .cfg"
	    button .cfg.but.no -text "Cancel" -command "destroy .cfg"
	    pack .cfg.but.ok .cfg.but.no -side left
	    pack .cfg.but -side bottom 

    bind .cfg.home.ent <FocusOut> { isDir  home "cdk_home" }
    bind .cfg.pidDir.ent <FocusOut> { isDir pidDir "pid_dir" }
    bind .cfg.perl.ent <FocusOut> { isExecutable  perl "perl_bin" }
    bind .cfg.gzipP.ent <FocusOut> { isExecutable gzipP "gzip_bin" }

    check_pkg_configuration    
}

proc check_pkg_configuration {} {
    global cdkConf

    isDir  home "cdk_home"
    isFile syslCnf "syslog_conf"
    isDir  pidDir "pid_dir"
    isExecutable perl "perl_bin"
    isExecutable gzipP "gzip_bin"

    set sf [format "%s/%s" $cdkConf(pid_dir) $cdkConf(syslog_pid)]
    if {[ file exists $sf ] == 0} {
        .cfg.syslPid.ent configure -foreground red
    } else {
        .cfg.syslPid.ent configure -foreground black
    }
}

proc isDir { sw v } {
    global cdkConf

    if {[ file isdirectory $cdkConf($v) ] == 0} {
        .cfg.$sw.ent configure -foreground red
    } else {
        .cfg.$sw.ent configure -foreground black
    }
}

proc isFile { sw v } {
    global cdkConf

    if {[ file exists $cdkConf($v) ] == 0} {
        .cfg.$sw.ent configure -foreground red
    } else {
        .cfg.$sw.ent configure -foreground black
    }
}

proc isExecutable { sw v } {
    global cdkConf

    if {[ file executable $cdkConf($v) ] == 0} {
        .cfg.$sw.ent configure -foreground red
    } else {
        .cfg.$sw.ent configure -foreground black
    }
}

proc create_resource_file { where } {
    global cdkConf

#    set fileId [ open [format "%s/chklogsrc" $cdkConf(cdk_home)] w ]
    set fileId [ open [format "%s/chklogsrc" $where] w ]
#puts [format "My file id %s for %s" $fileId $cdkConf(cdk_home)]
    puts $fileId "#"
    puts $fileId "# Global Resource File for Chklogs 2.x"
    puts $fileId "#"
    puts $fileId "# -- ChklogsConf:"
    puts $fileId "#       Location of configuration file. Don't change."
    puts $fileId "set ChklogsConf  /etc/chklogs.conf"

    puts $fileId "# -- ChklogsDb:"
    puts $fileId "#       Location of history/age file."
    puts $fileId [format "set ChklogsDb    %s/.chklogsdb" $cdkConf(cdk_home)]

    puts $fileId "# -- VarRun:"
    puts $fileId "#       Directory with the *.pid files, here we"
    puts $fileId "#       find syslog.pid and chklogs.pid"
    puts $fileId [format "set VarRun       %s" $cdkConf(pid_dir)]

    puts $fileId "# -- RelativePath:"
    puts $fileId "#       Directory name (basename) for archived logs"
    puts $fileId "#       when -Options local is selected"
    puts $fileId "set RelativePath OldLogs"

    puts $fileId "# -- Admin:"
    puts $fileId "#       Email address of recipients of Chklogs"
    puts $fileId "#       activity summary"
    puts $fileId [format "set Admin %s" $cdkConf(cdk_admin)]

    puts $fileId "# -- MailHost:"
    puts $fileId "#       Our SMTP host"
    puts $fileId [format "set MailHost %s" $cdkConf(mailhost)]

    puts $fileId "# -- SyslogConf:"
    puts $fileId "#       Full path/filename of the Syslogd"
    puts $fileId "#       daemon configuration file"
    puts $fileId [format "set SyslogConf %s" $cdkConf(syslog_conf)]

    puts $fileId "# -- Ignore Control"
    puts $fileId "#       Anything between `ignore on' and `ignore off'"
    puts $fileId "#       will *not* modify the configuration variable"
    puts $fileId "#       in question."
    puts $fileId "mode ignore on"

    puts $fileId "# -- MiniMail:"
    puts $fileId "#       yes/no. Only use `no' as a fallback when"
    puts $fileId "#       you don't have SMTP server. In that case"
    puts $fileId "#       you must have Sendmail and configure the"
    puts $fileId "#       mailer on chklogs. Default is 'yes'"
    puts $fileId "set MiniMail     no"

    puts $fileId "# -- NOT IMPLEMENTED YET --"
    puts $fileId "#set Zipper     /bin/compress"
    puts $fileId "#set ZipExt     Z"
    close $fileId
}

proc sed_command { filenr from to } {
    puts $filenr [format "s?%s?%s?" $from $to]
}

proc sed_scripts {} {
    global cdkConf

    # chklogs script
    puts "Generating sed script for chklogs"
    set fileId [ open "sed.cmd" w ]
    sed_command $fileId  "/usr/bin/perl"  $cdkConf(perl_bin) 
    sed_command $fileId  "/usr/local/lib/chklogs"  $cdkConf(cdk_home) 
    sed_command $fileId  "/bin/gzip"  $cdkConf(gzip_bin) 
    sed_command $fileId  "syslog.pid"  $cdkConf(syslog_pid) 
    close $fileId

    exec sed -f sed.cmd bin/chklogs-dist > bin/chklogs

    # chklogsadm script
    puts "Generating sed script for chklogsadm"
    set fileId [ open "sed.cmd" w ]
    sed_command $fileId  "/usr/bin/perl"  $cdkConf(perl_bin) 
    sed_command $fileId  "/usr/local/lib/chklogs"  $cdkConf(cdk_home) 
    close $fileId

    exec sed -f sed.cmd bin/chklogsadm-dist > bin/chklogsadm
}

proc perform_config {} {
    global dist_target makefilePath cdkConf

    if { [string compare $makefilePath "."] != 0} {
        puts "changing $makefilePath"
	set cwd	[pwd]
        cd $makefilePath
    }
    set myvars [format "DEGT_LIB=%s PERL_BIN=%s SYSLOG_PID=%s GZIP_BIN=%s" \
    			$cdkConf(cdk_home) \
    		        $cdkConf(perl_bin) \
    		        $cdkConf(syslog_pid) \
			$cdkConf(gzip_bin)]
    create_resource_file "."
    # Added for 2.0-2 as it creates error during configuration :-(
    if {[file isdirectory $cdkConf(cdk_home)] == 0} {
	if [ catch { [file mkdir $cdkConf(cdk_home)] } result ] {
	    puts "Sorry: could not create install directory $result"
	} else {
	    puts "Created $cdkConf(cdk_home)"
	}
    }

    set targFile [format "%s/chklogsrc" $cdkConf(cdk_home)]
    set backupFile  [format "%s/chklogsrc.bak" $cdkConf(cdk_home)]
    if {[file exists $targFile] == 1} {
    	# Keep a backup 
	file rename -force $targFile $backupFile
    }

    file copy "./chklogsrc" [format "%s/chklogsrc" $cdkConf(cdk_home)]

    sed_scripts
    cd $cwd
    # Now we can allow user to install
    .bot.butA configure -state normal
}

proc install_package {} {
    global inst_target makefilePath cdkConf

    if {[file isdirectory $cdkConf(cdk_home)] == 0} {
	if [ catch { [file mkdir $cdkConf(cdk_home)] } result ] {
	    puts "Sorry: could not create install directory $result"
	} else {
	}
    }

    if { [string compare $makefilePath "."] != 0} {
        puts "changing $makefilePath"
	set cwd	[pwd]
        cd $makefilePath
    }
    #
    # Now install the modules, executables and other files in the
    # right place
    #
    set myvars [format "DEGT_LIB=%s" $cdkConf(cdk_home)]
    exec make $inst_target $myvars
    cd $cwd
}

proc create_setup_screen {} {
    global txtTitle imgPath

    wm title . "Chklogs Setup"
    wm iconname  . "Setup"

    image create photo setupImg
    setupImg configure -file "chklogs1.gif"
    label .image -image setupImg
    pack  .image -side top 

    set w .mid
    frame $w -borderwidth 1 -relief groove
	label $w.lbl -textvariable txtTitle
	frame $w.text
	    text $w.text.txt -width 60 -height 10 -bd 2 -relief sunken \
		-yscrollcommand "$w.text.yscr set" -setgrid true \
		-xscrollcommand "$w.text.xscr set" -wrap none
	    scrollbar $w.text.xscr -orient horizontal \
	        -command "$w.text.txt xview"
	    scrollbar $w.text.yscr -orient vertical \
	        -command "$w.text.txt yview"

	pack $w.lbl -side top -anchor center
	pack $w.text -anchor center -side top
	pack $w.text.xscr -side bottom -fill x -anchor e
	pack $w.text.txt -side left -anchor w
	pack $w.text.yscr -side right -fill y -anchor n
	pack $w -side top

    set w .bot
    frame $w
        button $w.butA -textvariable butA -command "registerInfo .mid.text.txt"
        button $w.butX -text "Configure" -command "configure_package" \
	    -state disabled
	button $w.butB -textvariable butB -command "destroy ."
	pack $w.butA $w.butX $w.butB -side left
        pack $w -side top
}

if { $argc == 1 } {
    set makefilePath [lindex $argv 0]
} else {
    set makefilePath "."
}
set myself [pwd]
create_setup_screen
showLicense .mid.text.txt
