a5c035403393864e9f6e8987c82670c3d9010943
[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 actual script (which should be the getdown appdir/bin). Follow all symlinks.  Like GNU readlink -f
20 function Readlink-f {
21   Param($Link)
22   $Return = $null
23   $c = 0
24   $max = 100 # just in case we end up in a loop
25   [bool] $found = 0
26   $file = Get-Item -Path $Link
27   $prevfile = $null
28   While ( $c -lt $max -and "${file}" -ne "${prevfile}" -and -not $found ) {
29     $prevfile = $file
30     [string] $target = ( $file ).Target
31     If ( $target -eq $null -or ( $file ).LinkType -ne "SymbolicLink" ) {
32       $Return = $file
33       $found = 1
34     } Else {
35       If ( $( Split-Path -Path $target -IsAbsolute ) ) {
36         $file = Get-Item -Path $target
37       } Else {
38 # symbolic link is relative: combine previous link parent dir with the link target and resolve
39         $file = Get-Item -Path ( Join-Path -Path ( Split-Path -Path $prevfile -Parent ) -ChildPath $target -Resolve )
40       }
41     }
42     $c++
43   }
44   if ( -not $found ) {
45     throw "Could not determine path to actual file $( Split-Path -Path $Link -Leaf )"
46   }
47   $Return
48 }
49
50 # Avert problem with unix version of powershell and tell user the reason (Windows must always have .ps1 extension)
51 if ( $MyInvocation.MyCommand.Path -eq $null ) {
52   throw "Script or link to script must have extension .ps1"
53 }
54
55
56 $CMDPATH = ( Get-Item $MyInvocation.MyCommand.Path )
57 $SCRIPTPATH = Readlink-f -Link $CMDPATH
58 $DIR = Split-Path -Path $SCRIPTPATH -Parent
59
60 $APPDIR = If ( ( Split-Path -Path $DIR -Leaf ) -eq "bin" ) { Split-Path -Path $DIR -Parent } Else { $DIR }
61 $JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" }
62 $JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" )
63 $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt"
64
65 # look for getdown.txt -- needed to create classpath
66 if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
67   throw "Cannot find ${GETDOWNTXT}"
68 }
69
70 # look for bundled JRE. Might not be there if unix installer used in which case just invoke "java"
71 if ( -not ( Test-Path -Path "${JAVA}" ) ) {
72   Write-Host "Cannot find bundled ${JAVAEXE}. Using system ${JAVAEXE} and hoping for the best!"
73   $JAVA = $JAVAEXE
74 }
75
76 $CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
77
78 # get console width
79 $CONSOLEWIDTH = $Host.UI.RawUI.WindowSize.Width
80
81 # quote the args and the command (in case of spaces) with escape chars (`) and precede with & to indicate command not string
82 $myArgsString = '"' + $($myArgs -join '" "') + '"'
83 Invoke-Expression -Command "& `"${JAVA}`" `"-DCONSOLEWIDTH=${CONSOLEWIDTH}`" -cp `"${CLASSPATH}`" jalview.bin.Launcher ${myArgsString}"
84