Merge branch 'releases/Release_2_11_4_Branch'
[jalview.git] / utils / getdown / bin / jalview.ps1
1 #!/usr/bin/env pwsh
2
3 # save args and first parameter
4 $myArgs = $args.Clone()
5
6 # setup for powershell version < 6.0
7 [bool] $myIsWindows = 0
8 [bool] $myIsMacOS = 0
9 if ( $IsWindows -eq $null ) {
10   # for powershell version < 6.0 let's assume Windows
11   $myIsWindows = 1
12   $myIsMacOS = 0
13 } else {
14   $myIsWindows = $IsWindows
15   $myIsMacOS = $IsMacOS
16 }
17
18 [bool] $headless = 0
19 [bool] $gui = 0
20 [bool] $help = 0
21 [bool] $debug = 0
22 Switch -Regex ($args) {
23   "--help|--help-|--version|-h" {
24     $help = 1
25     $headless = 1
26     Continue
27   }
28   "--gui" {
29     $gui = 1
30     Continue
31   }
32   "--headless" {
33     $headless = 1
34     Continue
35   }
36   "--debug" {
37     $debug = 1
38     Continue
39   }
40 }
41 if ( $help ) {
42   # --help takes precedence
43   $gui = 0
44 } elseif ( $gui ) {
45   # --gui takes precedence over --headless
46   $headless = 0
47 }
48
49 # actual path of this script (which should be the getdown appdir/bin). Follow all symlinks.  Like GNU readlink -f
50 function Readlink-f {
51   Param(
52     [Parameter(mandatory=$true, ValueFromPipeline=$true)]$Link,
53     [Parameter()]$iteration_count = 1
54   )
55   if ( $iteration_count -ge 100 ) {
56     Write-Error "Readlink-f iterated 100 times"
57     return $Link
58   }
59   if ( $Link -eq "" -or $Link -eq $null ) {
60     return $null
61   }
62   $path_components = @()
63   $dir = Get-Item $Link
64   while ( $dir -ne $null ) {
65     while ( $dir.Target -ne $null ) {
66       # [System.IO.Path]::Combine caters for a multitude of sins that it's almost impossible to deal with with Join-Path
67       $dir = Get-Item ([System.IO.Path]::GetFullPath( [System.IO.Path]::Combine( (Split-Path $dir -Parent), $dir.Target )))
68     }
69     $parent = Split-Path -Path $dir -Parent
70     $path_components = @( (Split-Path -Path $dir -Leaf) ) + $path_components
71     $dir = Readlink-f $parent ($iteration_count + 1)
72   }
73   $real = Get-Item "/"
74   foreach ( $component in $path_components) {
75     # [System.IO.Path]::Combine caters for a multitude of sins that it's almost impossible to deal with with Join-Path
76     $real = Get-Item ([System.IO.Path]::GetFullPath( [System.IO.Path]::Combine( $real, $component )))
77   }
78   $real
79 }
80
81 # Avert problem with unix version of powershell and tell user the reason (Windows must always have .ps1 extension)
82 if ( $MyInvocation.MyCommand.Path -eq $null ) {
83   throw "Script or link to script must have extension .ps1"
84 }
85
86 # args for the JVM
87 $JVMARGS = @()
88
89 $CMDPATH = ( Get-Item $MyInvocation.MyCommand.Path )
90 $SCRIPTPATH = Readlink-f -Link $CMDPATH
91 $SCRIPT = $SCRIPTPATH
92 $DIR = Split-Path -Path $SCRIPTPATH -Parent
93 $APPDIR = If ( ( Split-Path -Path $DIR -Leaf ) -eq "bin" ) { Split-Path -Path $DIR -Parent } Else { $DIR }
94 $JAVAEXE = If ( $myIsWindows ) { "java.exe" } Else { "java" }
95 $JAVA = Join-Path -Path $APPDIR -ChildPath ( "jre/" + $( If ( $myIsMacOS ) { "Contents/Home/" } Else { "" } ) + "bin/${JAVAEXE}" )
96
97 if ( $headless ) {
98   # not setting java.awt.headless in java invocation of running jalview due to problem with Jmol
99   if ( $help ) {
100     $JVMARGS += "-Djava.awt.headless=true"
101   }
102   # this suppresses the Java icon appearing in the macOS Dock
103   if ( $myIsMacOS ) {
104     $JVMARGS += "-Dapple.awt.UIElement=true"
105   }
106 }
107
108 $GETDOWNTXT = Join-Path -Path $APPDIR -ChildPath "getdown.txt"
109
110 # look for getdown.txt -- needed to create classpath
111 if ( -not ( Test-Path -Path "${GETDOWNTXT}" ) ) {
112   throw "Cannot find ${GETDOWNTXT}"
113 }
114
115 # launching Jalview with jalview.bin.Launcher instead of getdown-launcher.jar
116 $CLASS = "jalview.bin.Launcher"
117
118 # get CLASSPATH from the code= entries in getdown.txt
119 $CLASSPATH = ( Select-String -Path "${GETDOWNTXT}" -AllMatches -Pattern "code\s*=\s*(.*)$" | foreach { Join-Path -Path $APPDIR -ChildPath $($_.Matches.Groups[1].Value ) } ) -join $( If ( $myIsWindows ) { ";" } Else { ":" } )
120
121 # get console width
122 $COLUMNS = $Host.UI.RawUI.WindowSize.Width
123 $JVMARGS += "-DCONSOLEWIDTH=${COLUMNS}"
124 $JVMARGS += "-Dgetdownappdir=${APPDIR}"
125 $JVMARGS += "-Dinstaller.appdir=${APPDIR}"
126 $JVMARGS += "-Dlauncher.script=${SCRIPT}"
127
128 # look for bundled JRE. Might not be there if unix installer used in which case just invoke "java"
129 if ( -not ( Test-Path -Path "${JAVA}" ) ) {
130   Write-Host "Cannot find bundled ${JAVA}. Using system ${JAVAEXE} and hoping for the best!"
131   $JAVA = $JAVAEXE
132 }
133
134 # we should always have at least one JVMARGS so this is okay
135 $myJvmArgsString = '"' + $($JVMARGS -join '" "') + '"'
136
137 # quote the args and the command (in case of spaces) with escape chars (`) and precede with & to indicate a command not string
138 if ( $myArgs.count -eq 0 ) {
139   $COMMAND = "& `"${JAVA}`" ${myJvmArgsString} -cp `"${CLASSPATH}`" $CLASS"
140 } else {
141   $myArgsString = '"' + $($myArgs -join '" "') + '"'
142   $COMMAND = "& `"${JAVA}`" ${myJvmArgsString} -cp `"${CLASSPATH}`" $CLASS ${myArgsString}"
143 }
144
145 if ( $debug -or $help ) {
146   Write-Error -Message "Shell running: ${COMMAND}"
147 }
148
149 Invoke-Expression -Command ${COMMAND}
150