JAL-3830 Allow paths to start with '~'. Make path check more succinct.
[jalview.git] / utils / getdown / jalviewc.ps1
index 300bd9e..0a5802b 100755 (executable)
@@ -1,30 +1,50 @@
 #!/usr/bin/env pwsh
 
-# save first parameter
-$ARGS = $args
-$ARG1 = $args[0]
-# parent dir of this script (should be the getdown app dir)
-$DIR = Split-Path $MyInvocation.MyCommand.Path -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 -ne "open" ) {
-  $ARGS = "-open " + $ARGS
+# save args and first parameter
+$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 = If ( $TARGET -eq $null -or $TARGET.LinkType -ne "SymbolicLink" ) { Split-Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path $TARGET -Parent }
+
+# 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
-$JAVA = "${APPDIR}\jre\bin\java.exe"
-$GETDOWNTXT = "${APPDIR}\getdown.txt"
+$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"
 
-if (-not (Test-Path -Path "${GETDOWNTXT}")) {
+# look for getdown.txt -- needed to create classpath
+if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
   throw "Cannot find getdown.txt"
 }
 
-if (-not (Test-Path -Path "${JAVA}")) {
-  Write-Host "Cannot find bundled java.exe. Using system and hoping for the best!"
-  $JAVA = "java.exe"
+# look for bundled JRE. Might not be there if unix installer used in which case just invoke "java"
+if ( -not ( Test-Path -Path "${JAVA}" ) ) {
+  Write-Host "Cannot find bundled ${JAVAEXE}. Using system ${JAVAEXE} and hoping for the best!"
+  $JAVA = $JAVAEXE
 }
 
-$CLASSPATH = (Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { "$DIR\$($_.Matches.Groups[1].Value -replace "/", "\")" }) -join ";"
+$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}"