#!/usr/bin/env pwsh
# save args and first parameter
-$ARGS = $args
-$ARG1 = $args[0]
+$myArgs = $args.Clone()
+$myArg1 = $args[0]
+
+# setup for powershell version < 6.0
+[bool] $myIsWindows = 0
+[bool] $myIsMacOS = 0
+if ( $IsWindows -eq $null ) {
+ # for powershell version < 6.0 let's assume Windows
+ $myIsWindows = 1
+ $myIsMacOS = 0
+} else {
+ $myIsWindows = $IsWindows
+ $myIsMacOS = $IsMacOS
+}
+
# parent dir of this script (should be the getdown app dir). Follow symlinks.
$TARGET = ( Get-Item $MyInvocation.MyCommand.Path ).Target
-$DIR = ( $TARGET -eq $null ) ? ( Split-Path $MyInvocation.MyCommand.Path -Parent ) : ( Split-Path $TARGET -Parent )
+$DIR = If ( $TARGET -eq $null -or $TARGET.LinkType -ne "SymbolicLink" ) { Split-Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path $TARGET -Parent }
-# insert an "-open" parameter to Jalview's ARGS if ARG1 is non-zero-length, and not "open" or starts with a "-"
-if ( $ARG1.length -gt 0 -and ( -not $ARG1.StartsWith("-") ) -and $ARG1 -cne "open" ) {
- $ARGS = "-open " + $ARGS
+# set the "-open" parameter if myArg1 is non-zero-length, and not "open" or starts with a "-"
+$OPEN = ""
+if ( $myArg1.length -gt 0 -and ( -not $myArg1.StartsWith("-") ) -and $myArg1 -cne "open" ) {
+ $OPEN = "-open"
}
$APPDIR = $DIR
-$JAVAEXE = ( $IsWindows ? "java.exe" : "java" )
-$JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + ( $IsMacOs ? "Contents/Home/" : "" ) + "bin/${JAVAEXE}" )
+$JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" }
+$JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" )
$GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt"
# look for getdown.txt -- needed to create classpath
$JAVA = $JAVAEXE
}
-$CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join ( $IsWindows ? ";" : ":" )
+$CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
-Invoke-Expression -Command "${JAVA} -cp '${CLASSPATH}' jalview.bin.Launcher $ARGS"
+# quote the args and the command (in case of spaces) with escape chars (`) and precede with & to indicate command not string
+$myArgsString = '"' + $($myArgs -join '" "') + '"'
+Invoke-Expression -Command "& `"${JAVA}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher ${OPEN} ${myArgsString}"