#!/bin/sh  
# \
exec tclsh "$0" "$@"

#First check that /local exists on this computer. 
#Otherwize this will not work.
if {![file exists /local]} {
    puts "/local is not available on this computer"
    exec firefox -no-remote &
    exit
}

#Check for standard .mozilla directory
#If it does not already exist create an empty one.
#Normally I would use teh "file exists" command but
#that will not work in this case because it could be
#a symbolic link without a target.
if {[catch {
    set type [file type $env(HOME)/.mozilla]
} results]} {
    file mkdir $env(HOME)/.mozilla
}

#If this is the first time this has been run then we 
#will copy the .mozilla directory to .mozilla.orig
if {[file type $env(HOME)/.mozilla] != "link"} {
    if {[file isdirectory $env(HOME)/.mozilla]} {
	if {[file exists $env(HOME)/.mozilla.orig]} {
	    file delete -force $env(HOME)/.mozilla.orig
	}
	file rename -force $env(HOME)/.mozilla $env(HOME)/.mozilla.orig
    }
}

#Check if workstation specific .mozilla.${hostname} directory exists.
#If not then copy .mozilla.orig over.
set hostname [exec hostname]
if {![file exists $env(HOME)/.mozilla.${hostname}]} {
    file copy -force $env(HOME)/.mozilla.orig $env(HOME)/.mozilla.${hostname}
}

#Check if /local/$env(USER)/.mozilla points to $env(HOME)/.mozilla.${hostname}
if {![file isdirectory /local/$env(USER)]} {
    file mkdir /local/$env(USER)
}
exec ln -sf $env(HOME)/.mozilla.${hostname} /local/$env(USER)/.mozilla

#Check if $env(HOME)/.mozilla points to /local/$env(USER)/.mozilla
exec ln -sf /local/$env(USER)/.mozilla $env(HOME)/.mozilla

exec firefox -no-remote &
