JAL-3830 Moved wrappers to bin/jalview.* and make a symlink bin/jalview. Install4j...
[jalview.git] / utils / getdown / bin / jalview.ps1
diff --git a/utils/getdown/bin/jalview.ps1 b/utils/getdown/bin/jalview.ps1
new file mode 100755 (executable)
index 0000000..b84e275
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/env pwsh
+
+# 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 -Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path -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 = If ( ( Split-Path -Path $DIR -Leaf ) -eq "bin" ) { Split-Path -Path $DIR -Parent } Else { $DIR }
+$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
+if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
+  throw "Cannot find getdown.txt"
+}
+
+# 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 { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
+
+# 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}"
+