b84e275554b073e1113c8846429159c245464157
[jalview.git] / utils / getdown / bin / jalview.ps1
1 #!/usr/bin/env pwsh
2
3 # save args and first parameter
4 $myArgs = $args.Clone()
5 $myArg1 = $args[0]
6
7 # setup for powershell version < 6.0
8 [bool] $myIsWindows = 0
9 [bool] $myIsMacOS = 0
10 if ( $IsWindows -eq $null ) {
11   # for powershell version < 6.0 let's assume Windows
12   $myIsWindows = 1
13   $myIsMacOS = 0
14 } else {
15   $myIsWindows = $IsWindows
16   $myIsMacOS = $IsMacOS
17 }
18
19 # parent dir of this script (should be the getdown app dir). Follow symlinks.
20 $TARGET = ( Get-Item $MyInvocation.MyCommand.Path ).Target
21 $DIR = If ( $TARGET -eq $null -or $TARGET.LinkType -ne "SymbolicLink" ) { Split-Path -Path $MyInvocation.MyCommand.Path -Parent } Else { Split-Path -Path $TARGET -Parent }
22
23 # set the "-open" parameter if myArg1 is non-zero-length, and not "open" or starts with a "-"
24 $OPEN = ""
25 if ( $myArg1.length -gt 0 -and ( -not $myArg1.StartsWith("-") ) -and $myArg1 -cne "open" ) {
26   $OPEN = "-open"
27 }
28
29 $APPDIR = If ( ( Split-Path -Path $DIR -Leaf ) -eq "bin" ) { Split-Path -Path $DIR -Parent } Else { $DIR }
30 $JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" }
31 $JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" )
32 $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt"
33
34 # look for getdown.txt -- needed to create classpath
35 if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
36   throw "Cannot find getdown.txt"
37 }
38
39 # look for bundled JRE. Might not be there if unix installer used in which case just invoke "java"
40 if ( -not ( Test-Path -Path "${JAVA}" ) ) {
41   Write-Host "Cannot find bundled ${JAVAEXE}. Using system ${JAVAEXE} and hoping for the best!"
42   $JAVA = $JAVAEXE
43 }
44
45 $CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
46
47 # quote the args and the command (in case of spaces) with escape chars (`) and precede with & to indicate command not string
48 $myArgsString = '"' + $($myArgs -join '" "') + '"'
49 Invoke-Expression -Command "& `"${JAVA}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher ${OPEN} ${myArgsString}"
50