#!/usr/local/bin/wish
global env gv

set gv(version)		"Visual Vacation (Version 1.6 07oct96)"
set gv(program)		vvac
set gv(iconname)	[string toupper $gv(program)]
set gv(top)		.$gv(program)
set gv(author)		"John van Gulik"

proc errmess { pw {msg ""} } {
	global env gv
	pack [label .errmess -text $msg] -side top -expand true -fill both
	pack [button .ok -text "OK" -command "destroy .ok"] -side top
	tkwait window .ok
}

##############################################################################
#####	Set the required environment variables
##############################################################################

if {[info exists env(HOME)] && $env(HOME)=="/"} {set env(HOME) "/root"}

if {$tcl_platform(platform)=="windows"} {
	if {![info exists env(TKAPPS)]} {
		set env(TKAPPS) /TKAPPS
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} elseif {$tcl_platform(platform)=="unix"} {
	if {![info exists env(TKAPPS)]} {
		if {[file isdirectory $env(HOME)/TKAPPS]} {
			set env(TKAPPS) $env(HOME)/TKAPPS
		} elseif {[file isdirectory /opt/TKAPPS]} {
			set env(TKAPPS) /opt/TKAPPS
		} elseif {[file isdirectory /apps/TKAPPS]} {
			set env(TKAPPS) /apps/TKAPPS
		} else {
			set env(TKAPPS) /usr/local/TKAPPS
		}
	}
	if {![file isdirectory $env(TKAPPS)]} {
		errmess . "Directory $env(TKAPPS) not found"
		exit 1
	}
} else {
	errmess . "This application is not supported under $tcl_platform(platform)"
	exit 1
}

set auto_path [linsert $auto_path 0 $env(TKAPPS)/lib]

#wm withdraw .

standard_startup $gv(program) $gv(version)

##############################################################################
#####	Set required variables and options
##############################################################################

if {[file exists /usr/bin/vacation]} {
	set gv(vacation) /usr/bin/vacation
} elseif {[file exists /usr/ucb/vacation]} {
	set gv(vacation) /usr/ucb/vacation
} else {
	errmess . "Could not find the vacation program"
	exit 1
}

##############################################################################
#####	SUBROUTINES
##############################################################################

proc toggle_keep { } {
	global env gv

	if {$gv(keep)=="true"} {
		set gv(keep) "false"
		$gv(top).keep.label configure -text "A copy of your mail will NOT be kept" -fg black -bg red
	} else {
		set gv(keep) "true"
		$gv(top).keep.label configure -text "A copy of your mail WILL be kept" -fg black -bg green
	}

	if {$gv(on)=="true"} {
		vacation_start
	} else {
		vacation_stop
	}
}

proc toggle_vacation { } {
	global env gv

	if {$gv(on)=="true"} {
		vacation_stop
	} else {
		vacation_start
	}
}

proc whoto_verify { } {
	global env gv

	regsub -all {[;,	]+} $gv(whoto) { } gv(whoto)
	regsub -all {[ ]+}  $gv(whoto) { } gv(whoto)
	set gv(whoto) [string trim $gv(whoto)]

	set badadds [sendmail_verify $gv(whoto)]
	if {[llength $badadds]>0} {
		errmess $gv(top) "Please check your additional addresses:\n[join $badadds "\n"]"
		return $badadds
	}

	writefiles

	return
}


proc vacation_start { } {
	global env gv

	if {[whoto_verify]!=""} { return }

	set gv(on) "true"

	writefiles

	$gv(top).on.label configure -text "Vacation is turned ON" -fg black -bg green
}

proc vacation_stop { } {
	global env gv

	if {[whoto_verify]!=""} { return }

	set gv(on) "false"

	writefiles

	$gv(top).on.label configure -text "Vacation is turned OFF" -fg black -bg red

}

proc writemsg { } {
	global env gv

	#
	#	Write the vacation message
	#
	set fp [open $env(HOME)/.vacation.msg w]
	puts $fp "X-Mailer: vacation"
	puts -nonewline $fp "[$gv(top).mess.val get 1.0 end]"
	close $fp

	f_chmod 644 $env(HOME)/.vacation.msg
}


proc writefiles { } {
	global env gv

	if {$gv(on)=="true"} {
		set forwardfile $env(HOME)/.forward
	} else {
		if {[file exists $env(HOME)/.forward]} {
			f_delete $env(HOME)/.forward
		}
		set forwardfile $env(HOME)/.forward.save
	}

	writemsg

	# initialize the vacation database
	catch "exec $gv(vacation) -I" res

	#
	#	Write the forward file
	#
	set command ""
	if {$gv(keep)=="true"} { append command "\\$env(USER)," }
	foreach address [split $gv(whoto)] { append command "$address," }
	append command "\"| $gv(vacation) $env(USER)\""

	set fp [open $forwardfile w]
	puts $fp "$command"
	close $fp

	f_chmod 644 $forwardfile

}

##############################################################################
#####	MAIN PROGRAM
##############################################################################

toplevel	$gv(top)
wm iconname	$gv(top) $gv(iconname)
wm iconbitmap	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).xbm
wm iconmask	$gv(top) @$env(TKAPPS)/bitmaps/$gv(program).msk
wm title	$gv(top) $gv(version)
wm minsize	$gv(top) 100 100
wm protocol	$gv(top) WM_DELETE_WINDOW "writemsg ; exit"

pack [label $gv(top).help] \
	-side bottom -fill x
bind_help $gv(top).help $gv(top).help \
	[lang "Help text" "Texte d'aide"]

pack [frame $gv(top).b1] \
        -side top -fill x
pack [menubutton $gv(top).b1.file -menu $gv(top).b1.file.m \
	-text [lang "File..." "Fichier..."] ] \
        -side left -fill y
pack [label $gv(top).b1.status -textvariable gv(status) ] \
        -side left -expand true -fill x
pack [button $gv(top).b1.iconify -text [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"] \
        -side left
pack [button $gv(top).b1.exit -text [lang "Save and Exit" "Sauveguardez et quittez"] \
	-command "writemsg ; exit"] \
        -side left

bind_help $gv(top).b1.file $gv(top).help \
        [lang "Standard file menu" "Menu standard du fichier"]
bind_help $gv(top).b1.status $gv(top).help \
        [lang "Status messages" "Messages d'tat"]
bind_help $gv(top).b1.iconify $gv(top).help \
        [lang "Iconify window" "Minimiser cette fentre"]
bind_help $gv(top).b1.exit $gv(top).help \
        [lang "Close this window" "Fermer cette fentre"]

menu $gv(top).b1.file.m
$gv(top).b1.file.m add command -label [lang "Help" "Aide"] \
	-command "help $env(TKAPPS)/help/$gv(program)"
$gv(top).b1.file.m add command -label [lang "Fonts/Colors" "Fontes/Couleurs"] \
	-command "background tkcustom $gv(program)"
$gv(top).b1.file.m add command -label [lang "Iconify" "Minimiser"] \
	-command "wm iconify $gv(top)"
$gv(top).b1.file.m add command -label [lang "Save and Exit" "Sauveguardez et Quittez"] \
	-command "writemsg ; exit"

pack [frame $gv(top).on] \
	-side top -fill x
pack [button $gv(top).on.button -width 20 -text "ON / OFF" \
	-command "toggle_vacation"] \
	-side left
pack [label $gv(top).on.label] \
	-side left -expand true -fill x

pack [frame $gv(top).keep] \
	-side top -fill x
pack [button $gv(top).keep.button -width 20 -text "KEEP / NOKEEP" \
	-command "toggle_keep"] \
	-side left
pack [label $gv(top).keep.label] \
	-side left -expand true -fill x

pack [frame $gv(top).whoto] \
	-side top -expand true -fill x
pack [button $gv(top).whoto.button -width 20 -text "ADD ADDRESSES" \
	-command "whoto_verify"] \
	-side left
pack [label $gv(top).whoto.lab -text "Additional copies to :" ] \
	-side left
pack [entry $gv(top).whoto.ent -relief sunken -textvariable gv(whoto)] \
	-side left -expand true -fill x

bind $gv(top).whoto.ent <Return> "whoto_verify"

pack [frame $gv(top).mess] \
	-side top -expand true -fill both
pack [scrollbar $gv(top).mess.yscroll -command "$gv(top).mess.val yview" ] \
	-side left -fill y
pack [text $gv(top).mess.val -yscroll "$gv(top).mess.yscroll set" -relief sunken ] \
	-side left -expand true -fill both

if {![file readable $env(HOME)/.vacation.msg]} {
	$gv(top).mess.val delete 1.0 end
	$gv(top).mess.val insert end "From: $env(USER)\n"
	$gv(top).mess.val insert end "Subject: I am on vacation\n"
	$gv(top).mess.val insert end "Classification: UNCLASSIFIED\n"
	$gv(top).mess.val insert end "\n"
	$gv(top).mess.val insert end "I am away on vacation.  I will read your mail when I return.\n"
	$gv(top).mess.val insert end "\n"
	$gv(top).mess.val insert end "In my absence you might wish to contact the following person(s) :\n"
	$gv(top).mess.val insert end "\n"
	$gv(top).mess.val insert end "\n"
	if {[file exists $env(HOME)/.signature] && [file readable $env(HOME)/.signature]} {
		$gv(top).mess.val insert end [f_read $env(HOME)/.signature]
	}
	# output the vacation message
	set fp [open $env(HOME)/.vacation.msg w]
	puts $fp "X-Mailer: vacation"
	puts $fp "[$gv(top).mess.val get 1.0 end]"
	close $fp
} else {
	$gv(top).mess.val delete 1.0 end
	$gv(top).mess.val insert end [join [f_grep {!^X-Mailer:} $env(HOME)/.vacation.msg] "\n"]
}

f_chmod 644 $env(HOME)/.vacation.msg

# set opposite defaults and then toggle them
if {[file exists $env(HOME)/.forward] && [file readable $env(HOME)/.forward]} {
	set gv(keep) "true"
	set gv(on) "true"
	set res [join [split [f_read $env(HOME)/.forward] "\n"] ","]
	foreach address [split $res ","] {
		regsub -all {\\} $address {} address
		set address [string trim $address]
		set address [string trim $address {"}]
		set address [string trim $address]
		if {$address==$env(USER)} {
			set gv(keep) "false"
		} elseif {[lindex $address 0]=="|" && [file tail [lindex $address 1]]=="vacation"} {
			set gv(on) "false"
		} else {
			append gv(whoto) " $address"
		}
	}
	set gv(whoto) [string trim $gv(whoto)]
} elseif {[file exists $env(HOME)/.forward.save] && [file readable $env(HOME)/.forward.save]} {
	set gv(keep) "true"
	set gv(on) "true"
	set res [join [split [f_read $env(HOME)/.forward.save] "\n"] ","]
	foreach address [split $res ","] {
		regsub -all {\\} $address {} address
		set address [string trim $address]
		set address [string trim $address {"}]
		set address [string trim $address]
		if {$address==$env(USER)} {
			set gv(keep) "false"
		} elseif {[lindex $address 0]=="|" && [file tail [lindex $address 1]]=="vacation"} {
			set gv(on) "true"
		} else {
			append gv(whoto) " $address"
		}
	}
	set gv(whoto) [string trim $gv(whoto)]
} else {
	set gv(keep) "false"
	set gv(on) "true"
}
toggle_vacation

toggle_keep

set gv(status) "Please note that changes will take place immediately"

wm withdraw .
if {[winfo exists .startup]} {destroy .startup}
if {[info exists gv(startup.id)]} {image delete $gv(startup.id)}

