#!/usr/local/bin/wish -f
#
# Requires Tcl 7.4 and Tk 4.0 (may work with earlier
# versions but you'll probably have to tweak it).
#
# Script assumes wish is located in /usr/local/bin
# If it is located elsewhere change the first line
# of this script.
#
# Provided to install TkBabyNames 4.1
# Written by Tyson Bigler
# Please read the disclaimer provided under the 
# HELP feature in TkBabyNames.
################################################################

set VERSION 4.1
set RCFILE "$env(HOME)/.babynamesrc"
set TkBabyNamesPath "$env(HOME)/TkBaby"
set DeleteSource 0

if {[file exists $RCFILE]} {
	tk_dialog .dialog "FYI" "TkBabyNames $VERSION found prior installation.\
		Will use old files as necessary.  You may wish to install to\
		the same directory to insure complete update." info 0 Okay
	source $RCFILE
	set WifeName $WIFE
	set HusbandName $HUSBAND
	set Surname $SURNAME
	set MyFont $MYFONT
	set Backups $BACKUP
	set MainGeom +0+0
	set CopyOldFiles 1
	set GetOldFiles $NAMESPATH
} else {
	set WifeName "Lucy"
	set HusbandName "Ricky"
	set Surname "Ricardo"
	set MyFont "-adobe-helvetica-medium-r-normal--16-160-75-*-p-*-iso8859-1"
	set Backups 1
	set MainGeom +0+0
	set CopyOldFiles 0
	exec touch $RCFILE
}

wm title . "Install TkBabyNames $VERSION"
wm geometry . +285+135

################################################################
# Create menu bar
################################################################
frame .mbar -relief raised -bd 2
frame .dummy -width 10c -height 5m
pack .mbar .dummy -side top -fill x

menubutton .mbar.install -text Install -underline 0 -menu .mbar.install.menu
menubutton .mbar.help -text Help -underline 0 -menu .mbar.help.menu
pack .mbar.install -side left
pack .mbar.help -side right

menu .mbar.install.menu
	.mbar.install.menu add command -label Install -command InstallBaby
	.mbar.install.menu add separator
	.mbar.install.menu add command -label Abort -command {exit}

menu .mbar.help.menu
	.mbar.help.menu add command -label Help -command HelpFeature

tk_menuBar .mbar .mbar.install .mbar.help

################################################################
# Help
################################################################
proc HelpFeature {} {
	global VERSION
	toplevel .help
	wm title .help "TkBabyNames $VERSION Install Help"

	frame .help.main
	text .help.main.text -width 80 -height 20 -relief raised -bd 2 \
		-yscrollcommand ".help.main.scroll set" -wrap word
	scrollbar .help.main.scroll -command ".help.main.text yview"
	button .help.main.dismiss -text Dismiss -command "destroy .help"
	pack .help.main.scroll -side right -fill y
	pack .help.main.text -expand 1 -fill x
	pack .help.main.dismiss -expand 1 -fill x
	pack .help.main

	.help.main.text insert end {
This script will assist you in installing TkBabyNames.  It will also\
look for any previous version's and deal with them accordingly.\
It will save the files with your list of names and incorporate\
them into the new directory stucture.

Enter the path where you want TkBabyNames and the files that will\
contain your list of names to be installed.

Enter your name, your wife's name, your surname, and the font you wish\
to use.  You may also specify a window geometry for the main TkBabyNames\
window.  These variables will be placed in your rc file.

To maintain backup files for the files containing your list of names\
once TkBabyNames is installed check yes.

If you would like the installation program to clean up after\
itself, check yes for delete source.  This will remove the installation\
program, the readme file, and the source script.  It will not,\
however, delete the tar file in case you would like to save that one.

----------------
FROM THE README:
----------------
If you have previously installed TkBabyNames (ie, version 4.0)
the install program for 4.01 will overwrite TkBabyNames (provided
you install it to the same directory), but will save the old files
containing your preferred names.  However, these files (previously
called something like girlnames-Lucy) will be renamed and placed
in a specific directory.  The directory stucture after installation
should look like this (assuming you installed to TkBaby):

        ../TkBaby
        ../TkBaby/TkBabyNames
        ../TkBaby/Boys
        ../TkBaby/Boys/(your wife's name)
        ../TkBaby/Boys/(your name)
        ../TkBaby/Girls
        ../TkBaby/Girls/(your wife's name)
        ../TkBaby/Girls/(your name)

In addition, "starter" files will be placed in the Boys and Girls directories.
These files contain names to get you started.  See the help feature in
TkBabyNames for more information on these files.

Your RC file will also be altered to adjust to the minor structure changes.

--------------------
STANDARD DISCLAIMER:
--------------------
TkBabyNames and the accompanying files are freeware, though the author
does request that you send a message stating that you have used the
program, whether you like it or not, and any complaints or ideas for
improvement.  I've written a lot of Tickle scripts for my
own personal use but this is the first one I've put out to the public.
Go easy on me.  ;-D

You may distribute these files provided the following:
        1.  You may not charge for the use of this software
        2.  You may not modify any of the files in any way
        3.  You include the entire archive (to include this README)

Additionally, you are obviously free to look at the programs themselves
to see what they're doing.  There is nothing fancy to any of these
scripts, and chances are it will be viewed as poorly written.  I guess
I have my own ways of doing things.  If you have ideas of how to
optimize anything in the scripts, drop me a line.

Feedback is encouraged!  If you like the program and have ideas on
how to improve it I'm open to suggestions.

-------------
INSTALLATION:
-------------
Please run ./INSTALL to install the programs and create the rc file
and name files necessary to run TkBabyNames.  You may have to change the
first line of the INSTALL script to reflect the proper path to the wish
interpreter (you also need to change it in TkBabyNames.tcl prior to
installing it).

-------------
PARTING SHOT:
-------------
I have to close with the standard disclaimer:  This software is provided
as is and the author neither makes nor implies any warranties of any
kind.  Use of this software constitutes your acceptance of responsibility
for any and all damages, loss of data, etc.

Enjoy!

Tyson
}
}

################################################################
# Create frame for options
################################################################
frame .info
frame .info.labels
label .info.labels.l1 -relief flat -width 20 -text {TkBabyNames path:}
label .info.labels.l3 -relief flat -width 20 -text {Wife's name:}
label .info.labels.l4 -relief flat -width 20 -text {Husband's name:}
label .info.labels.l5 -relief flat -width 20 -text {Surname:}
label .info.labels.l6 -relief flat -width 20 -text {Font:}
label .info.labels.l7 -relief flat -width 20 -text {Geometry:}
pack .info.labels.l1 .info.labels.l3 .info.labels.l4\
	.info.labels.l5 .info.labels.l6 .info.labels.l7 -pady 1.6m
pack .info.labels -side left
frame .info.entries
entry .info.entries.e1 -relief sunken -width 40 -textvariable TkBabyNamesPath
entry .info.entries.e3 -relief sunken -width 40 -textvariable WifeName
entry .info.entries.e4 -relief sunken -width 40 -textvariable HusbandName
entry .info.entries.e5 -relief sunken -width 40 -textvariable Surname
entry .info.entries.e6 -relief sunken -width 40 -textvariable MyFont
entry .info.entries.e7 -relief sunken -width 40 -textvariable MainGeom
pack .info.entries.e1 .info.entries.e3 .info.entries.e4 .info.entries.e5\
	.info.entries.e6 .info.entries.e7 -pady 1m 
pack .info.entries -side right

frame .backups
label .backups.label -relief flat -width 20 -text {Backup Name Files?}
radiobutton .backups.yes -width 16 -text Yes -variable Backups -value 1
radiobutton .backups.no -width 16 -text No -variable Backups -value 0
pack .backups.label -side left
pack .backups.yes .backups.no -side right 

frame .delsrc
label .delsrc.label -relief flat -width 20 -text {Delete source files?}
radiobutton .delsrc.yes -width 16 -text Yes -variable DeleteSource -value 1
radiobutton .delsrc.no -width 16 -text No -variable DeleteSource -value 0
pack .delsrc.label -side left
pack .delsrc.yes .delsrc.no -side right

################################################################
# Create main window
################################################################
pack .info
pack .backups
pack .delsrc -pady 2m

################################################################
# Install programs
################################################################
proc InstallBaby {} {
	global TkBabyNamesPath WifeName HusbandName Surname\
		MyFont Backups RCFILE DeleteSource CopyOldFiles\
		GetOldFiles MainGeom VERSION

	set File [open "$RCFILE" w]
	puts $File "set WIFE $WifeName"
	puts $File "set HUSBAND $HusbandName"
	puts $File "set SURNAME $Surname"
	puts $File "set MYFONT $MyFont"
	puts $File "set BACKUP $Backups"
	puts $File "set MAINGEOM $MainGeom"
	puts $File "set TkBabyNamesBin $TkBabyNamesPath"
	close $File

	if {[file exists $TkBabyNamesPath]} {
		set SOURCEDIR [pwd]
		cd $TkBabyNamesPath
		exec cp $SOURCEDIR/TkBabyNames.tcl ./TkBabyNames
		exec cp $SOURCEDIR/tkboynames.tar .
		exec cp $SOURCEDIR/tkgirlnames.tar .
		exec tar -xf tkboynames.tar
		exec tar -xf tkgirlnames.tar
		exec rm tkboynames.tar
		exec rm tkgirlnames.tar
		exec touch Boys/$WifeName
		exec touch Boys/$WifeName-combo
		exec touch Girls/$WifeName
		exec touch Girls/$WifeName-combo
		exec touch Boys/$HusbandName
		exec touch Boys/$HusbandName-combo
		exec touch Girls/$HusbandName
		exec touch Girls/$HusbandName-combo
		cd $SOURCEDIR
	} else {
		set SOURCEDIR [pwd]
		exec mkdir $TkBabyNamesPath
		cd $TkBabyNamesPath
		exec cp $SOURCEDIR/TkBabyNames.tcl TkBabyNames
		exec cp $SOURCEDIR/tkboynames.tar .
		exec cp $SOURCEDIR/tkgirlnames.tar .
		exec tar -xf tkboynames.tar
		exec tar -xf tkgirlnames.tar
		exec rm tkboynames.tar
		exec rm tkgirlnames.tar
		exec touch Boys/$WifeName
		exec touch Boys/$WifeName-combo
		exec touch Boys/$HusbandName
		exec touch Boys/$HusbandName-combo
		exec touch Girls/$WifeName
		exec touch Girls/$WifeName-combo
		exec touch Girls/$HusbandName
		exec touch Girls/$HusbandName-combo
		cd $SOURCEDIR
	}

	if {$CopyOldFiles == 1} {
		if {[file exists $GetOldFiles/girlnames-$HusbandName]} {
			exec mv $GetOldFiles/girlnames-$HusbandName \
			$TkBabyNamesPath/Girls/$HusbandName
		}
		if {[file exists $GetOldFiles/girlnames-$WifeName]} {
			exec mv $GetOldFiles/girlnames-$WifeName \
			$TkBabyNamesPath/Girls/$WifeName
		}
		if {[file exists $GetOldFiles/boynames-$HusbandName]} {
			exec mv $GetOldFiles/boynames-$HusbandName \
			$TkBabyNamesPath/Boys/$HusbandName
		}
		if {[file exists $GetOldFiles/boynames-$WifeName]} {
			exec mv $GetOldFiles/boynames-$WifeName \
			$TkBabyNamesPath/Boys/$WifeName
		}
		tk_dialog .dialog "FYI" {Detected previous version of\
			TkBabyNames.  Moved your files containing your\
			preferred names to work with new directory/file scheme.}\
			info 0 Okay
	}

	tk_dialog .dialog "FYI" "TkBabyNames $VERSION installation complete"\
		info 0 Okay

	if {$DeleteSource == 1} {
		exec rm -f TkBabyNames.tcl
		exec rm -f INSTALL
		exec rm -f README
		exec rm -f tkboynames.tar
		exec rm -f tkgirlnames.tar
		exit
	} else {
		exit
	}
}
