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

#####################################################################
#
# File : 	hcsv.tcl
# 
# Purpose :	Remove emails from names
#
# Author :	Ing. Hans-Michael Haschek
#
# Copyright : 	(c) Case Software Technologies 1999, Austria
#
#####################################################################

#################################
# GLOBAL VARIABLES
#
global widget; 
catch {package require Tk}
#################################
# USER DEFINED PROCEDURES
#

# checks, if address is already existent
proc check {email email1} {
	global INPUT

	set path [file dirname $INPUT]
	if {![file exists $path/plaxo_ab_outlook.csv]} {return 0}; # no check
	catch {destroy .top17.buffer}
	text .top17.buffer
	set fd [open $path/plaxo_ab_outlook.csv RDONLY]
	.top17.buffer insert end [read $fd]
	close $fd
	if {[.top17.buffer search $email 1.0] == "" && [.top17.buffer search $email1 1.0] == ""} {
		return 0
	} else {
		return 1
	}
}

proc convert {} {
	global INPUT OUTPUT NLAST

	if {$INPUT == "" || $OUTPUT == ""} {
		tk_messageBox -icon error -message "Filename empty" -type ok
		return
	}
	if {$INPUT == $OUTPUT} {
		tk_messageBox -icon error -message "Filenames should not be identical" -type ok
		return
	}
	set fd [open $INPUT RDONLY]
	set fdo [open $OUTPUT w+]
	set li [gets $fd]; #jump over 1st line
	puts $fdo $li; # just a copy
	while {![eof $fd]} {
		set li [gets $fd]; if {[eof $fd]} break
		set li1 [split $li ","]
		set first [lindex $li1 1]; set last [lindex $li1 3]; # get names
		set email [lindex $li1 5]; # pick up email-address
		set email1 [string tolower [lindex $li1 5]]; # pick up email-address
		if {[check $email $email1]} continue; # entry already exists
#		puts -nonewline "$first | $last"; # trace
		# remove emails
		if {[string first "@" $last] != -1} {
			regsub -all "\"" $last "" last; # first remove delimiters
			set p [string first "@" $last]
			while {$p != -1} {
				if {[string index $last $p] != " "} {incr p -1; continue}
				break
			}

			if {$p == -1} {
				regsub -all "\"" $first "" first
				set last [lindex $first 1]
				set first "\"[lindex $first 0]\""
			} else {
				incr p -1
				set last [string range $last 0 $p]
			}
			regsub -all {\[} $last "" last
			regsub -all {\(} $last "" last
			regsub -all {\{} $last "" last
			# fix case where blank within brackets
			if {$last == "" && [string first " " $first] != -1} {
				regsub -all "\"" $first "" first
				set last [lindex $first 1]
				set first "\"[lindex $first 0]\""
			}
			if {$last == ""} {
				nl:window "$first | [lindex $li1 3] | [lindex $li1 5]"
				tkwait window .nl
				set last $NLAST
			}
			# remove training blanks
			set done 0
			while {!$done} {
				if {[string index $last end] != " "} break
				set last [string range $last 0 end-1]
			}
			set last "\"$last\""
		}
		# remove webpages
		if {[string first "." $last] != -1 && [string first ":" $last] != -1} {
			regsub -all "\"" $last "" last; # first remove delimiters
			set p [string first ":" $last]
			while {$p != -1} {
				if {[string index $last $p] != " "} {incr p -1; continue}
				break
			}

			if {$p == -1} {
				regsub -all "\"" $first "" first
				set last [lindex $first 1]
				set first "\"[lindex $first 0]\""
			} else {
				incr p -1
				set last [string range $last 0 $p]
			}
			regsub -all {\[} $last "" last
			regsub -all {\(} $last "" last
			regsub -all {\{} $last "" last
			if {$last == ""} {
				nl:window "$first | [lindex $li1 3] | [lindex $li1 5]"
				tkwait window .nl
				set last $NLAST
			}
			# remove training blanks
			set done 0
			while {!$done} {
				if {[string index $last end] != " "} break
				set last [string range $last 0 end-1]
			}
			set last "\"$last\""
		}
#		puts " >> $first | $last"; # trace
		set li1 [lreplace $li1 1 1 $first]
		set li1 [lreplace $li1 3 3 $last]
		set lin [join $li1 ","]
		puts $fdo $lin
	}
	close $fd
	close $fdo
	tk_messageBox -title "Conversion done" -type ok -icon info -message "Conversion done"
	exit
}

proc nl:window {val} {
	global NLAST

	set NLAST ""
	set base .nl

	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	###################
	# CREATING WIDGETS
	###################
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 327x117+150+124
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "No last name found"

	label $base.lab18 -borderwidth 1 -text {Lastname is empty for} 
	label $base.lab19 -borderwidth 1 -relief raised -text $val 
	frame $base.cpd20 -borderwidth 1 -relief raised -height 30 -width 30 
	label $base.cpd20.01 -anchor w -text {New lastname} 
	entry $base.cpd20.02 -background white -textvariable NLAST 
	bind $base.cpd20.02 <Return> {.nl.but21 invoke}
	button $base.but21 -text Save -command {destroy .nl}
	button $base.but22 -text Cancel -command {destroy .nl}
	###################
	# SETTING GEOMETRY
	###################
	place $base.lab18 -x 5 -y 5 -anchor nw -bordermode ignore 
	place $base.lab19 -x 10 -y 25 -anchor nw -bordermode ignore 
	place $base.cpd20 \
        -in .nl -x 5 -y 50 -width 310 -height 25 -anchor nw \
        -bordermode ignore 
	pack $base.cpd20.01 \
        -in .nl.cpd20 -anchor center -expand 0 -fill none -padx 2 -pady 2 \
        -side left 
	pack $base.cpd20.02 \
        -in .nl.cpd20 -anchor center -expand 1 -fill x -padx 2 -pady 2 \
        -side right 
	place $base.but21 \
        -in .nl -x 5 -y 85 -anchor nw -bordermode ignore 
	place $base.but22 \
        -in .nl -x 255 -y 85 -anchor nw -bordermode ignore 
	focus -force $base.cpd20.02
}

proc browse:input {} {
	global INPUT OUTPUT

	set types {
	{{Outlook}		{.csv}	}
	{{Text Files}		{.txt}  }
	{{All files}		*}
	}
	set f [tk_getOpenFile -defaultextension ".csv" -title "File to convert" -filetypes $types]
	if {$f != ""} {set INPUT $f; set OUTPUT $f}
}

proc browse:output {} {
	global OUTPUT

	set types {
	{{Outlook}		{.csv}	}
	{{Text Files}		{.txt}  }
	{{All files}		*}
	}
	set f [tk_getSaveFile -defaultextension ".csv" -title "File to convert" -filetypes $types]
	if {$f != ""} {set OUTPUT $f}
}

# displays helptext
proc help {} {
	global PATH

	set base .help

	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	###################
	# CREATING WIDGETS
	###################
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 555x340+181+140
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Help"

	frame $base.cpd18 \
        -borderwidth 1 -relief raised -height 30 -width 30 
	scrollbar $base.cpd18.01 \
        -background grey77 -borderwidth 1 -command {.help.cpd18.03 xview} \
        -orient horizontal -width 10 
	scrollbar $base.cpd18.02 \
        -background grey77 -borderwidth 1 -command {.help.cpd18.03 yview} \
        -width 10 
	text $base.cpd18.03 \
        -background white \
        -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* -height 1 \
        -width 8 -xscrollcommand {.help.cpd18.01 set} \
        -yscrollcommand {.help.cpd18.02 set} 
	button $base.but19 -text Close -command {destroy .help}
	###################
	# SETTING GEOMETRY
	###################
	pack $base.cpd18 -side top -fill both -expand 1
	grid columnconf $base.cpd18 0 -weight 1
	grid rowconf $base.cpd18 0 -weight 1
	grid $base.cpd18.01 \
        -in .help.cpd18 -column 0 -row 1 -columnspan 1 -rowspan 1 -sticky ew 
	grid $base.cpd18.02 \
        -in .help.cpd18 -column 1 -row 0 -columnspan 1 -rowspan 1 -sticky ns 
	grid $base.cpd18.03 \
        -in .help.cpd18 -column 0 -row 0 -columnspan 1 -rowspan 1 \
        -sticky nesw 
	pack $base.but19 -side bottom
	$base.cpd18.03 insert end "Outlook CSV conversion utility\n\n"
	$base.cpd18.03 insert end "This piece of software copies address-files from LinkedIn (in Outlook-CSV-format)\n"
	$base.cpd18.03 insert end "to a new file by removing email-addresses from the namefield.\n"
	$base.cpd18.03 insert end "In the case that the lastname is then empty, the last name in firstname is taken and inserted\n"
	$base.cpd18.03 insert end "into the lastname.\n\n"

	$base.cpd18.03 insert end "Nearly all addresses can be handled.\n\n"

	$base.cpd18.03 insert end "NOTE: this is free software and provided as is. I am not liable for any errors/problems occour,\n"
	$base.cpd18.03 insert end "but I appreciate any feedback.\n\n"

	$base.cpd18.03 insert end "This software is copyrighted by Michael Haschek, CST Austria, but can be freely distributed.\n"
}


proc Window {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set newname [lindex $args 2]
    set rest [lrange $args 3 end]
    if {$name == "" || $cmd == ""} {return}
    if {$newname == ""} {
        set newname $name
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {base} {
	if {$base == ""} {
		set base .
	}
	###################
	# CREATING WIDGETS
	###################
	wm focusmodel $base passive
	wm geometry $base 1x1+0+0
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm withdraw $base
	wm title $base "vt.tcl"
	###################
	# SETTING GEOMETRY
	###################
}

proc vTclWindow.top17 {base} {
	global INPUT OUTPUT

	set base .top17

	if {[winfo exists $base]} {
		wm deiconify $base; return
	}
	###################
	# CREATING WIDGETS
	###################
	toplevel $base -class Toplevel
	wm focusmodel $base passive
	wm geometry $base 453x175+193+172
	wm maxsize $base 1009 738
	wm minsize $base 1 1
	wm overrideredirect $base 0
	wm resizable $base 1 1
	wm deiconify $base
	wm title $base "Cleanup CSV"

	frame $base.cpd18 -borderwidth 1 -relief raised -height 30 -width 30 
	label $base.cpd18.01 -anchor w -text Input -width 6 
	entry $base.cpd18.02 -background white -textvariable INPUT 
	button $base.but19 -text Browse -command browse:input
	frame $base.cpd20 -borderwidth 1 -relief raised -height 30 -width 30 
	label $base.cpd20.01 -anchor w -text Output -width 6 
	entry $base.cpd20.02 -background white -textvariable OUTPUT 
	button $base.but21 -text Browse -command browse:output
	button $base.but22 -text Convert -command convert
	button $base.but23 -command exit -text Cancel 
	button $base.help -text Help -command help
	label $base.lab24 \
        -borderwidth 1 -text {This free software is provided as is} 
	label $base.lab25 \
        -borderwidth 1 \
        -text {Nobody should claim me for any problems occouring!} 
	label $base.lab26 \
        -borderwidth 1 -text {Copyright (c) Michael Haschek / CST Austria} 
	###################
	# SETTING GEOMETRY
	###################
	place $base.cpd18 \
        -in .top17 -x 10 -y 10 -width 345 -height 25 -anchor nw \
        -bordermode ignore 
	pack $base.cpd18.01 \
        -in .top17.cpd18 -anchor center -expand 0 -fill none -padx 2 -pady 2 \
        -side left 
	pack $base.cpd18.02 \
        -in .top17.cpd18 -anchor center -expand 1 -fill x -padx 2 -pady 2 \
        -side right 
	place $base.but19 \
        -in .top17 -x 365 -y 9 -anchor nw -bordermode ignore 
	place $base.cpd20 \
        -in .top17 -x 10 -y 35 -width 345 -height 25 -anchor nw \
        -bordermode ignore 
	pack $base.cpd20.01 \
        -in .top17.cpd20 -anchor center -expand 0 -fill none -padx 2 -pady 2 \
        -side left 
	pack $base.cpd20.02 \
        -in .top17.cpd20 -anchor center -expand 1 -fill x -padx 2 -pady 2 \
        -side right 
	place $base.but21 \
        -in .top17 -x 365 -y 35 -anchor nw -bordermode ignore 
	place $base.but22 -in .top17 -x 10 -y 65 -anchor nw -bordermode ignore 
	place $base.help -in .top17 -x 200 -y 65 -anchor nw -bordermode ignore 
	place $base.but23 -in .top17 -x 370 -y 65 -anchor nw -bordermode ignore 
	place $base.lab24 \
        -in .top17 -x 10 -y 105 -anchor nw -bordermode ignore 
	place $base.lab25 \
        -in .top17 -x 10 -y 125 -anchor nw -bordermode ignore 
	place $base.lab26 \
        -in .top17 -x 10 -y 145 -anchor nw -bordermode ignore 
}

Window show .
Window show .top17

