#!/usr/bin/wish

# OpenVerse execution script for Debian Gnu/Linux
# This is a tcl script that executes the main OpenVerse module found in
# /usr/lib/OpenVerse name OpenVerse.tcl
# This script was written to allow the command line arguments of -v (--version)
# and -h (--help).
#
# Written by Brandon Griffith <brandon@openverse.com>
#
# CHANGES
# Jan 21 2001
# * Created this script for Debian package of OpenVerse
#
# Aug 19 2001
# * Changed this script a bit to keep it from passing OpenVerse.tcl to the
#   background. Some people did not like this idea, and frankly neither did I.
#
# Nov 18 2001
# * Changed script to call wish instead of tclsh. This allows us to source
#   the main openverse module instead of using "eval exec".
# * Removed some useless junk and cleaned code a bit.
#
# Nov 19 2001
# * Set up a proc{} so that a user has the option to install the debian-swirl
#   avatar by issuing openverse --debian
# * Made a few more changes on how OpenVerse.tcl is sourced. Now the $argv
#   gets set as $argv0, this is the same as calling OpenVerse.tcl with $argv
#   and puts an end to that mess of not being able to supply the app arg.
# * A little more code cleanup, though it still looks as dirty as hell.
#
# Dec 09 2006
# * Changed script to obey FSH rules by pulling the Debian Swirl icon
#   from /usr/share/OpenVerse instead of /usr/lib/OpenVerse
# * Fixed a minor typo: detecting detection ;)

set OV(Dir) "/usr/lib/OpenVerse"
set OV(homedir) "~/.OpenVerse"
set OV(sharedir) "/usr/share/OpenVerse"
set OV(oversion) "0.8.7"

proc Help {} {   
        global OV
        
        puts "OpenVerse Usage:"
        puts "-h <--help>: Print this message"
        puts "-v <--version>: Print Debianized OpenVerse version"
		puts "--debian: To install the Debian Swirl avatar"
		puts "or just type openverse to start the client"
        exit
}

proc Version {} {
        global OV
        puts "OpenVerse Release: $OV(oversion)"
        exit
}

proc InstallDebAv {} {
	global OV
	if {![file exists "$OV(homedir)"]} {
		puts "Error detectng $OV(homedir)"
		puts "You need to run openverse at least once first"
		exit
	}
	if [file exists "$OV(homedir)/anims/debian-swirl.av"] {
		puts "You seem to already have it installed"
		exit
	}
	puts "Installing debian-swirl avatar"
	file copy "$OV(sharedir)/images/debian-swirl.gif" "$OV(homedir)/images/"
	file copy "$OV(Dir)/anims/debian-swirl.av" "$OV(homedir)/anims/"
	exit
}


# I could have shortened this a bit
# But this makes it easier to read for some
if {![string compare $argv "-h"]} {
	Help
}

if {![string compare $argv "--help"]} {
	Help
}

if {![string compare $argv "-v"]} {
	Version
}

if {![string compare $argv "--version"]} {
	Version
}

if {![string compare $argv "--debian"]} {
	InstallDebAv
}


if [file exists  "$OV(Dir)/OpenVerse.tcl"] {
	# we do this to pass the $argv to OpenVerse.tcl
	set argv0 $argv
	source "$OV(Dir)/OpenVerse.tcl"
} else {
	puts "Error executing $OV(Dir)/OpenVerse.tcl"
	puts "File does not exist"
	puts ""
	puts "Please make sure you have openverse installed correctly and that"
	puts "the modules are all in place located in $OV(Dir)"
}
