
my mylaunch("")


on open names
	
	tell application "Finder"
		set farg to ""
		repeat with i in names
			set farg to farg & " \"" & (the POSIX path of (i as string)) & "\""
		end repeat
	end tell
	my mylaunch(farg)
end open

on mylaunch(farg)
	tell application "Finder"
		set myname to name of (path to me)
		set cutpos to (offset of ".app" in myname) - 1
		set basename to text 1 thru cutpos of myname
		set appdir to (container of (path to me) as string)
		set neuronhome to the POSIX path of appdir
		
		set myhome to path to current user folder
		set fname to (the POSIX path of (myhome as string)) & ".nrn_as2sh"
		set nl to ASCII character 10
		--display dialog fname
		set mycmd to "./" & basename & ".sh " & farg
		
		set thefile to open for access (fname as POSIX file) with write permission
		set eof of thefile to 0
		write "#!/usr/bin/env bash" & nl to thefile
		write "NRNHOME=\"" & neuronhome & "\"" & nl to thefile
		write "NEURONHOME=\"${NRNHOME}/share/nrn\"" & nl to thefile
		write "PATH=\"${NRNHOME}/bin:${PATH}\"" & nl to thefile
		write "export NRNHOME" & nl to thefile
		write "export NEURONHOME" & nl to thefile
		write "export PATH" & nl to thefile
		write "cd \"${NRNHOME}/bin\"" & nl to thefile
		write "NRNHOME_EXEC=\"${NRNHOME}\"" & nl to thefile
		write "export NRNHOME_EXEC" & nl to thefile
		write (mycmd as string) & nl to thefile
		close access thefile
		
		set termfile to appdir & "bin:" & "macnrn.term"
		tell application "Terminal" to open file termfile
		
	end tell
	
end mylaunch


