#!/usr/bin/env python
#
# c2007 by Paul Sherman <psherma1@rochester.rr.com> http://www.pcbypaul.com     #
#									        #
# This program is free software; you can redistribute it and/or			#
# modify it under the terms of the GNU General Public License			#
# as published by the Free Software Foundation; either version 2		#
# of the License, or (at your option) any later version.
#
import os, sys, string
import app_dirs

usage_message = "Usage: " + sys.argv[0] + " [-s] appdir [arguments]\n"

if len(sys.argv) <= 1:
	sys.stderr.write(usage_message)
	sys.exit(1)

if sys.argv[1] == '-s':
	case_sensitive = 1
	search_string = sys.argv[2]
else:
	case_sensitive = 0
	search_string = sys.argv[1]

# Get the APPS_PATH or set the default
apps_path = os.getenv("APPS_PATH")
if apps_path == None:
	home_path = os.getenv("HOME")
	apps_path = "/usr/apps:/usr/local/apps:" + home_path + "/Apps"

appdir = app_dirs.find_appdir(apps_path, search_string, case_sensitive)
if appdir != '':
	appdir = os.path.realpath(appdir + os.sep + "AppRun")
	os.execv(appdir, sys.argv[1:])

sys.stderr.write("AppDir " + search_string + " not found in " + apps_path + ".\n")
sys.exit(1)
