forked from informatici/openhospital-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oh.ps1
1856 lines (1660 loc) · 72.5 KB
/
oh.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
#!/usr/bin/pwsh
#
# Open Hospital (www.open-hospital.org)
# Copyright © 2006-2024 Informatici Senza Frontiere ([email protected])
#
# Open Hospital is a free and open source software for healthcare data management.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# https://www.gnu.org/licenses/gpl-3.0-standalone.html
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
<#
.SYNOPSIS
Open Hospital startup script - oh.ps1
.DESCRIPTION
The script is used to setup and launch Open Hospital in PORTABLE, CLIENT or SERVER mode or with Demo data.
It can also be used to perform some basic operations like saving or importing a database.
Open Hospital CLIENT | PORTABLE | SERVER
Usage: oh.ps1 [ -lang en|fr|it|es|pt|ar ] [default set to en]
[ -mode PORTABLE|CLIENT|SERVER ]
[ -loglevel INFO|DEBUG ] [default set to INFO]
[ -generate_config on|off ]
[ -interactive on|off ]
.EXAMPLE
./oh.ps1 -lang it -mode PORTABLE -loglevel DEBUG -interactive off -generate_config on
.NOTES
Developed by Informatici Senza Frontiere - 2022
.LINK
https://www.open-hospital.org
#>
#################### Script info and configuration - Do not edit #####################
######## set script DEBUG mode
# saner programming env: these switches turn some bugs into errors
#Set-PSDebug -Strict
# Clean all variables in IDE
#Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
######## set minimum PowerShell version
#Requires -Version 5.1
######## command line parameters
param ($lang, $mode, $loglevel, $generate_config, $interactive)
$script:OH_LANGUAGE=$lang
$script:OH_MODE=$mode
$script:LOG_LEVEL=$loglevel
$script:WRITE_CONFIG_FILES=$generate_config
$script:INTERACTIVE_MODE=$interactive
######## get script info
# determine script name and location for PowerShell
$script:SCRIPT_DIR = Split-Path $script:MyInvocation.MyCommand.Path
$script:SCRIPT_NAME = $MyInvocation.MyCommand.Name
$script:POWERSHELL_EXE = (get-command PowerShell.exe).Path
######## global preferences
# disable progress bar
$global:ProgressPreference= 'SilentlyContinue'
######################## Script configuration #######################
#
# Interactive mode
# set INTERACTIVE_MODE to "off" to launch oh.ps1 without calling the user
# interaction menu (script_menu). Useful if automatic startup of OH is needed.
# In order to use this mode, setup all the OH configuration variables in the script
# or pass arguments via command line.
#$script:INTERACTIVE_MODE="on"
#
# set WRITE_CONFIG_FILES=on "on" to force generation / overwriting of OH configuration files:
# data/conf/my.cnf and oh/rsc/*.properties files will be regenerated from the original .dist files
# with the settings defined in this script.
#
# Default is set to "off": configuration files will not be regenerated or overwritten if already present.
#
#$script:WRITE_CONFIG_FILES="off"
##################### OH general configuration ####################
# -> OH_PATH is the directory where Open Hospital files are located
# OH_PATH="c:\Users\OH\OpenHospital\oh"
# set OH mode to PORTABLE | CLIENT | SERVER - default set to PORTABLE
#$script:OH_MODE="PORTABLE"
# language setting - default set to en
$script:OH_LANGUAGE_LIST= @("ar","de","en","es","fr","it","pt","sq")
$script:OH_LANGUAGE_LIST_INFO=("Arabic","German","English","Spanish","French","Italian","Portuguese","Albanian")
#$script:OH_LANGUAGE="en" # default
# single / multiuser - set "yes" for single user configuration
$script:OH_SINGLE_USER="no"
# set log level to INFO | DEBUG - default set to INFO
#$script:LOG_LEVEL="INFO"
# set DEMO_DATA to on to enable demo database loading - default set to off
# ---> Warning <--- __requires deletion of all portable data__
$script:DEMO_DATA="off"
$script:DEMO_DATABASE="ohdemo"
# set JAVA_BIN
# Uncomment this if you want to use system wide JAVA
#$script:JAVA_BIN="C:\Program Files\JAVA\bin\java.exe"
##################### Database configuration #######################
$script:DATABASE_SERVER="127.0.0.1"
$script:DATABASE_PORT=3306
$script:DATABASE_ROOT_PW="tmp2021oh111"
$script:DATABASE_NAME="oh"
$script:DATABASE_USER="isf"
$script:DATABASE_PASSWORD="isf123"
####################### OH configuration #########################
# path and directories
$script:OH_DIR="."
$script:OH_DOC_DIR="doc"
$script:CONF_DIR="data/conf"
$script:DATA_DIR="data/db"
$script:PHOTO_DIR="data/photo"
$script:BACKUP_DIR="data/dump"
$script:LOG_DIR="data/log"
$script:SQL_DIR="sql"
$script:SQL_EXTRA_DIR="sql/extra"
$script:TMP_DIR="tmp"
# imaging / dicom
$script:DICOM_MAX_SIZE="4M"
$script:DICOM_STORAGE="FileSystemDicomManager" # SqlDicomManager
$script:DICOM_DIR="data/dicom_storage"
# logging
$script:LOG_FILE="startup.log"
$script:LOG_FILE_ERR="startup_error.log"
$script:OH_LOG_FILE="openhospital.log"
$script:API_LOG_FILE="api.log"
$script:API_ERR_LOG_FILE="api_error.log"
# SQL creation files
#$script:DB_CREATE_SQL="create_all_en.sql" # default to en
$script:DB_DEMO="create_all_demo.sql"
######################## Other settings ########################
# date format
$script:DATE= Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
# downloaded file extension
$script:EXT="zip"
# mysql configuration file
$script:MYSQL_CONF_FILE="my.cnf"
# OH configuration files
$script:OH_SETTINGS="settings.properties"
$script:DATABASE_SETTINGS="database.properties"
$script:EXAMINATION_SETTINGS="examination.properties"
$script:IMAGING_SETTINGS="dicom.properties"
$script:LOG4J_SETTINGS="log4j2-spring.properties"
$script:PRINTER_SETTINGS="txtPrinter.properties"
$script:SMS_SETTINGS="sms.properties"
$script:TELEMETRY_SETTINGS="telemetry.properties"
$script:XMPP_SETTINGS="xmpp.properties"
$script:API_SETTINGS="application.properties"
$script:CRED_SETTINGS="default_credentials.properties"
$script:DEMO_CRED_SETTINGS="default_demo_credentials.properties"
# OH jar bin files
$script:OH_GUI_JAR="OH-gui.jar"
$script:OH_API_JAR="openhospital-api-0.1.0.jar"
# help file
$script:HELP_FILE="OH-readme.txt"
# set default database name
$script:DEFAULT_DATABASE_NAME="$DATABASE_NAME"
# set default data base_dir
$script:DEFAULT_DATADIR="$DATA_DIR"
# default database admin/root user
$script:DATABASE_ROOT_USER="root"
# activate expert mode - set to "on" to enable advanced functions - use at your own risk!
$script:EXPERT_MODE="off"
$script:OH_UI_URL="http://localhost:8080"
$script:OH_API_PID="../tmp/oh-api.pid"
############## Architecture and external software ##############
######## MariaDB/MySQL Software
# MariaDB version
$script:MYSQL_VERSION="10.6.16"
$script:MYSQL32_VERSION="10.6.5"
######## define system and software architecture
$script:ARCH=$env:PROCESSOR_ARCHITECTURE
$32archarray=@("386","486","586","686","x86","i86pc")
$64archarray=@("amd64","AMD64","x86_64")
if ($64archarray -contains "$ARCH") {
$script:JAVA_ARCH=64;
$script:MYSQL_ARCH="x64";
$script:JAVA_PACKAGE_ARCH="x64";
}
elseif ($32archarray -contains "$ARCH") {
$script:JAVA_ARCH=32;
$script:MYSQL_ARCH=32;
$script:MYSQL_VERSION=$script:MYSQL32_VERSION;
$script:JAVA_PACKAGE_ARCH="i686";
}
else {
Write-Host "Unknown architecture: $ARCH. Exiting." -ForegroundColor Red
Read-Host; exit 1
}
# set MariaDB download URL / package
$script:MYSQL_URL="https://archive.mariadb.org/mariadb-$script:MYSQL_VERSION/win$script:MYSQL_ARCH-packages/"
$script:MYSQL_DIR="mariadb-$script:MYSQL_VERSION-win$script:MYSQL_ARCH"
$script:MYSQL_NAME="MariaDB" # For console output - MariaDB/MYSQL_NAME
######## JAVA Software
######## JAVA 64bit - default architecture
### JRE 11 - openjdk
#$script:JAVA_URL="https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/"
#$script:JAVA_DISTRO="OpenJDK11U-jre_x64_windows_hotspot_11.0.11_9"
#$script:JAVA_DIR="jdk-11.0.11+9-jre"
### JRE 17 - zulu distribution
#$script:JAVA_DISTRO="zulu11.68.17-ca-jre11.0.21-win_$JAVA_PACKAGE_ARCH"
$script:JAVA_DISTRO="zulu17.48.15-ca-jre17.0.10-win_$JAVA_PACKAGE_ARCH"
$script:JAVA_URL="https://cdn.azul.com/zulu/bin"
# workaround for JRE 11 - 32bit
# if ( $JAVA_ARCH -eq "32" ) {
# $script:JAVA_DISTRO="zulu11.58.25-ca-jre11.0.16.1-win_$JAVA_PACKAGE_ARCH"
#}
$script:JAVA_DIR=$JAVA_DISTRO
######################## DO NOT EDIT BELOW THIS LINE ########################
########################### Functions ###########################
###################################################################
function script_menu {
# show menu
# Clear-Host # clear console
Write-Host " -----------------------------------------------------------------"
Write-Host "| |"
Write-Host "| Open Hospital - $OH_VERSION |"
Write-Host "| |"
Write-Host " -----------------------------------------------------------------"
Write-Host " arch $ARCH | lang $OH_LANGUAGE | mode $OH_MODE | log level $LOG_LEVEL | Demo $DEMO_DATA"
Write-Host " -----------------------------------------------------------------"
if ( $EXPERT_MODE -eq "on" ) {
Write-Host " EXPERT MODE activated"
Write-Host " API server set to $API_SERVER"
Write-Host " -----------------------------------------------------------------"
}
Write-Host ""
Write-Host " C set OH in CLIENT mode"
Write-Host " P set OH in PORTABLE mode"
Write-Host " S set OH in SERVER mode (portable)"
Write-Host " l set language -> [ $OH_LANGUAGE_LIST ]"
Write-Host " E toggle EXPERT MODE - show advanced options"
Write-Host " h show help"
Write-Host " q quit"
Write-Host ""
if ( $EXPERT_MODE -eq "on" ) {
script_menu_advanced;
}
}
###################################################################
function script_menu_advanced {
# show menu
# Clear-Host # clear console
Write-Host " -------------------------------- "
Write-Host " EXPERT MODE - advanced options"
Write-Host ""
Write-Host " A toggle API server - EXPERT_MODE"
Write-Host " e export/save OH database"
Write-Host " r restore OH database"
Write-Host " d toggle log level INFO/DEBUG"
Write-Host " D initialize OH with Demo data"
Write-Host " G setup GSM"
Write-Host " i initialize/install OH database"
Write-Host " m configure database connection manually"
Write-Host " s save OH configuration"
Write-Host " t test database connection (CLIENT mode only)"
Write-Host " u create Desktop shortcut with current params"
Write-Host " v show configuration"
Write-Host " V check for latest OH version"
Write-Host " X clean/reset OH installation"
Write-Host ""
}
###################################################################
function get_confirmation ($arg) {
$choice = Read-Host -Prompt "(y/n)? "
switch ("$choice") {
"y" { "yes"; break }
"n" { "Exiting.";
Read-Host;
if ( $arg -eq 1 ) {
parse_user_input;
}
exit 0;
}
default { "Invalid choice. Exiting.";
Read-Host;
if ( $arg -eq 1 ) {
parse_user_input;
}
exit 1;
}
}
}
###################################################################
function set_path {
# get current directory
$script:CURRENT_DIR=Get-Location | select -ExpandProperty Path
# set OH_PATH if not defined
if ( ! $OH_PATH ) {
Write-Host "Info: OH_PATH not defined - setting to script path"
$script:OH_PATH=$PSScriptRoot
if ( !(Test-Path "$OH_PATH/$SCRIPT_NAME" -PathType leaf) ) {
Write-Host "Error - $SCRIPT_NAME not found in the current PATH. Please browse to the directory where Open Hospital was unzipped or set up OH_PATH properly." -ForegroundColor Yellow
Read-Host; exit 1
}
}
}
###################################################################
function read_settings {
# check and read OH version file
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/version.properties" -PathType leaf ) {
# read Open Hospital Version
Get-Content $OH_PATH\$OH_DIR\rsc\version.properties | Where-Object {$_.length -gt 0} | Where-Object {!$_.StartsWith("#")} | ForEach-Object {
$var = $_.Split('=',2).Trim()
New-Variable -Force -Scope Private -Name $var[0] -Value $var[1]
}
$script:OH_VERSION="$VER_MAJOR.$VER_MINOR.$VER_RELEASE"
}
else {
Write-Host "Error: Open Hospital not found. Exiting" -ForegroundColor Red
Read-Host; exit 1
}
# check for OH settings file and read values
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS" -PathType leaf ) {
Write-Host "Reading OH settings file..."
$oh_settings = [pscustomobject](Get-Content "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS" -Raw | ConvertFrom-StringData)
$script:OH_MODE=$oh_settings.MODE
$script:OH_LANGUAGE=$oh_settings.LANGUAGE
$script:OH_SINGLE_USER=$oh_settings.SINGLE_USER
$script:OH_DOC_DIR=$oh_settings.OH_DOC_DIR
$script:DEMO_DATA=$oh_settings.DEMODATA
$script:API_SERVER=$oh_settings.APISERVER
}
# check for database settings file and read values
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/$DATABASE_SETTINGS" -PathType leaf ) {
Write-Host "Reading database settings file..."
$db_settings = [pscustomobject](Get-Content "$OH_PATH/$OH_DIR/rsc/$DATABASE_SETTINGS" -Raw | ConvertFrom-StringData)
$DATABASE_URL=$db_settings."jdbc.url"
$DATABASE_URL=$DATABASE_URL.TrimStart("jdbc:mysql")
$script:DATABASE_SERVER=$DATABASE_URL.Split('/')[2].Split(':')[0]
$script:DATABASE_PORT=$DATABASE_URL.Split(":",2)[1].Split("/",2)[0]
$script:DATABASE_NAME=$DATABASE_URL.Split(":",2)[1].Split("/",2)[1]
$script:DATABASE_USER=$db_settings."jdbc.username"
$script:DATABASE_PASSWORD=$db_settings."jdbc.password"
}
else {
Write-Host "Warning: configuration file $DATABASE_SETTINGS not found." -ForegroundColor Yellow
}
}
###################################################################
function set_defaults {
# set default values for script variables
# interactive mode - set default to on
if ( [string]::IsNullOrEmpty($INTERACTIVE_MODE) ) {
$script:INTERACTIVE_MODE="on"
}
# config files generation - set default to off
if ( [string]::IsNullOrEmpty($WRITE_CONFIG_FILES) ) {
$script:WRITE_CONFIG_FILES="off"
}
# OH mode - set default to PORTABLE
if ( [string]::IsNullOrEmpty($OH_MODE) ) {
$script:OH_MODE="PORTABLE"
}
# OH DOC DIR - set default to "doc" (../doc for oh)
if ( [string]::IsNullOrEmpty($OH_DOC_DIR) ) {
$script:OH_DOC_DIR="doc"
}
# OH language - set default to en
if ( [string]::IsNullOrEmpty($OH_LANGUAGE) ) {
$script:OH_LANGUAGE="en"
}
# set database creation script in chosen language
if ( [string]::IsNullOrEmpty($DB_CREATE_SQL) ) {
$script:DB_CREATE_SQL="create_all_$OH_LANGUAGE.sql"
}
# log level - set default to INFO
if ( [string]::IsNullOrEmpty($LOG_LEVEL) ) {
$script:LOG_LEVEL="INFO"
}
# single / multiuser - set "yes" for single user configuration
if ( [string]::IsNullOrEmpty($OH_SINGLE_USER) ) {
$script:OH_SINGLE_USER="no"
}
# demo data - set default to off
if ( [string]::IsNullOrEmpty($DEMO_DATA) ) {
$script:DEMO_DATA="off"
}
# api server - set default to off
if ( [string]::IsNullOrEmpty($API_SERVER) ) {
$script:API_SERVER="off"
}
# UI interface - set default to off
if ( [string]::IsNullOrEmpty($UI_INTERFACE) ) {
$script:UI_INTERFACE="off"
}
# EXPERT_MODE features - set default to off
if ( [string]::IsNullOrEmpty($EXPERT_MODE) ) {
$script:EXPERT_MODE="off"
}
# set escaped path (/ in place of \)
$script:OH_PATH_SUBSTITUTE=$OH_PATH -replace "\\", "/"
}
###################################################################
function set_db_name {
# set DATA_DIR with db name
$script:DATA_DIR="$DEFAULT_DATADIR/$DATABASE_NAME"
#
# set escaped values (/ in place of \)
$script:DATA_DIR=$DATA_DIR -replace "\\", "/"
}
###################################################################
function set_oh_mode {
# if $OH_SETTINGS is present set OH mode
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS" -PathType leaf ) {
Write-Host "Configuring OH mode..."
######## $OH_SETTINGS language configuration
Write-Host "Setting OH mode to $OH_MODE in OH configuration files-> $OH_SETTINGS..."
(Get-Content "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS") -replace('^(MODE.+)',"MODE=$OH_MODE") | Set-Content "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS"
}
else {
Write-Host "Warning: $OH_SETTINGS file not found." -ForegroundColor Yellow
}
Write-Host "OH mode set to $OH_MODE." -ForeGroundcolor Green
}
###################################################################
function set_demo_data {
# set database name for demo data
switch -CaseSensitive( $script:DEMO_DATA ) {
"on" { #
$script:DATABASE_NAME=$DEMO_DATABASE
}
"off" { #
$script:DATABASE_NAME="$script:DEFAULT_DATABASE_NAME"
}
}
}
###################################################################
function check_language {
# check for valid language selection
foreach ($lang in $OH_LANGUAGE_LIST) {
if ($script:OH_LANGUAGE_LIST -contains "$OH_LANGUAGE") {
Write-Host ""
Write-Host "Language set to $OH_LANGUAGE"
return;
}
Write-Host ""
Write-Host "Invalid language option [$OH_LANGUAGE]: setting to default [en]" -ForegroundColor Yellow
$script:OH_LANGUAGE="en"
Read-Host;
}
}
###################################################################
function set_language {
# set localized database creation script
$script:DB_CREATE_SQL="create_all_$OH_LANGUAGE.sql"
# if $OH_SETTINGS is present set language
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS" -PathType leaf ) {
Write-Host "Configuring OH language..."
######## $OH_SETTINGS language configuration
Write-Host "Setting language to $OH_LANGUAGE in OH configuration files-> $OH_SETTINGS..."
(Get-Content "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS") -replace('^(LANGUAGE.+)',"LANGUAGE=$OH_LANGUAGE") | Set-Content "$OH_PATH/$OH_DIR/rsc/$OH_SETTINGS"
Write-Host "Language set to $OH_LANGUAGE."
}
else {
Write-Host "Warning: $OH_SETTINGS file not found." -ForegroundColor Yellow
}
}
###################################################################
function set_log_level {
if ( Test-Path "$OH_PATH/$OH_DIR/rsc/$LOG4J_SETTINGS" -PathType leaf ) {
######## $LOG4J_SETTINGS log_level configuration
Write-Host "Setting log level in OH configuration file -> $LOG4J_SETTINGS..."
switch -CaseSensitive( $script:LOG_LEVEL ) {
###################################################
"INFO" {
(Get-Content "$OH_PATH/$OH_DIR/rsc/$LOG4J_SETTINGS").replace("DEBUG","$LOG_LEVEL") | Set-Content "$OH_PATH/$OH_DIR/rsc/$LOG4J_SETTINGS"
break;
}
"DEBUG" {
(Get-Content "$OH_PATH/$OH_DIR/rsc/$LOG4J_SETTINGS").replace("INFO","$LOG_LEVEL") | Set-Content "$OH_PATH/$OH_DIR/rsc/$LOG4J_SETTINGS"
}
default {
Write-Host "Invalid log level option: $LOG_LEVEL." -ForegroundColor Red
exit 2;
}
}
Write-Host "Log level set to $script:LOG_LEVEL" -ForeGroundcolor Green
}
else {
Write-Host "Warning: $LOG4J_SETTINGS file not found." -ForegroundColor Yellow
}
}
###################################################################
function initialize_dir_structure {
# create directory structure
[System.IO.Directory]::CreateDirectory("$OH_PATH/$TMP_DIR") > $null
[System.IO.Directory]::CreateDirectory("$OH_PATH/$LOG_DIR") > $null
[System.IO.Directory]::CreateDirectory("$OH_PATH/$DICOM_DIR") > $null
[System.IO.Directory]::CreateDirectory("$OH_PATH/$PHOTO_DIR") > $null
[System.IO.Directory]::CreateDirectory("$OH_PATH/$BACKUP_DIR") > $null
}
###################################################################
function download_file ($download_url,$download_file) {
Write-Host "Downloading $download_file from $download_url..."
try {
$wc = new-object System.Net.WebClient
$wc.DownloadFile("$download_url\$download_file","$OH_PATH\$download_file")
}
catch [System.Net.WebException],[System.IO.IOException] {
Write-Host "Unable to download $download_file from $download_url" -ForegroundColor Red
Read-Host; exit 1;
}
catch {
Write-Host "An error occurred. Exiting." -ForegroundColor Red
Read-Host; exit 1;
}
}
###################################################################
function create_desktop_shortcut {
Write-Host "Creating/updating OH shortcut on Desktop..."
$WshShell = New-Object -comObject WScript.Shell
#$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\OpenHospital.lnk")
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\OpenHospital.lnk")
$Shortcut.TargetPath = "$POWERSHELL_EXE" # $SCRIPT_DIR\$SCRIPT_NAME"
$Shortcut.Arguments = "-ExecutionPolicy Bypass $SCRIPT_DIR\$SCRIPT_NAME -interactive off -mode $OH_MODE -lang $OH_LANGUAGE"
$Shortcut.WorkingDirectory = "$OH_PATH"
$ShortCut.IconLocation = "$OH_PATH\oh.ico"
$Shortcut.Save()
Write-Host "Done!"
}
###################################################################
function java_lib_setup {
# NATIVE LIB setup
switch ( "$JAVA_ARCH" ) {
"64" { $script:NATIVE_LIB_PATH="$OH_PATH\$OH_DIR\lib\native\Win64" }
"32" { $script:NATIVE_LIB_PATH="$OH_PATH\$OH_DIR\lib\native\Windows" }
}
# CLASSPATH setup
# include OH jar file
$script:OH_CLASSPATH="$OH_PATH\$OH_DIR\bin\$OH_GUI_JAR"
# include all needed directories
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\bundle\"
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rpt_base\"
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rpt_extra\"
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rpt_stat\"
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rsc\"
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rsc\images"
#$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\rsc\icons" # hardcoded
$script:OH_CLASSPATH="$OH_CLASSPATH;$OH_PATH\$OH_DIR\lib\"
# include all jar files under lib\
$script:jarlist= Get-ChildItem "$OH_PATH\$OH_DIR\lib" -Filter *.jar | % { $_.FullName }
ForEach( $n in $jarlist ) {
$script:OH_CLASSPATH="$n;$OH_CLASSPATH"
}
}
###################################################################
function java_check {
# check if JAVA_BIN is already set and it exists
if ( !( $JAVA_BIN ) -or !(Test-Path $JAVA_BIN -PathType leaf ) ) {
# set default
Write-Host "Setting default JAVA..."
$script:JAVA_BIN="$OH_PATH\$JAVA_DIR\bin\java.exe"
}
# if JAVA_BIN is not found download JRE
if ( !(Test-Path $JAVA_BIN -PathType leaf ) ) {
if ( !(Test-Path "$OH_PATH/$JAVA_DISTRO.$EXT" -PathType leaf ) ) {
Write-Host "Warning - JAVA not found. Do you want to download it?" -ForegroundColor Yellow
get_confirmation;
# Download java binaries
download_file "$JAVA_URL" "$JAVA_DISTRO.$EXT"
}
Write-Host "Unpacking $JAVA_DISTRO..."
try {
Expand-Archive "$OH_PATH\$JAVA_DISTRO.$EXT" -DestinationPath "$OH_PATH\" -Force
}
catch {
Write-Host "Error unpacking Java. Exiting." -ForegroundColor Red
Read-Host; exit 1
}
Write-Host "Java unpacked successfully!"
Write-Host "Removing downloaded file..."
Remove-Item "$OH_PATH\$JAVA_DISTRO.$EXT"
Write-Host "Done!"
}
Write-Host "JAVA found!"
Write-Host "Using $JAVA_BIN"
}
###################################################################
function mysql_check {
if ( !(Test-Path "$OH_PATH/$MYSQL_DIR") ) {
if ( !(Test-Path "$OH_PATH/$MYSQL_DIR.$EXT" -PathType leaf) ) {
Write-Host "Warning - $MYSQL_NAME not found. Do you want to download it?" -ForegroundColor Yellow
get_confirmation;
# Downloading mysql binary
download_file "$MYSQL_URL" "$MYSQL_DIR.$EXT"
}
Write-Host "Unpacking $MYSQL_DIR..."
try {
Expand-Archive "$OH_PATH\$MYSQL_DIR.$EXT" -DestinationPath "$OH_PATH\" -Force
}
catch {
Write-Host "Error unpacking $MYSQL_NAME. Exiting." -ForegroundColor Red
Read-Host; exit 1
}
Write-Host "$MYSQL_NAME unpacked successfully!"
Write-Host "Removing downloaded file..."
Remove-Item "$OH_PATH\$MYSQL_DIR.$EXT"
Write-Host "Done!"
}
# check for mysqld binary
if (Test-Path "$OH_PATH/$MYSQL_DIR/bin/mysqld.exe" -PathType leaf) {
Write-Host "$MYSQL_NAME found!"
Write-Host "Using $MYSQL_DIR"
}
else {
Write-Host "Error: $MYSQL_NAME not found. Exiting." -ForegroundColor Red
Read-Host; exit 1
}
}
###################################################################
function config_database {
Write-Host "Checking for $MYSQL_NAME config file..."
if ( ($script:WRITE_CONFIG_FILES -eq "on") -or !(Test-Path "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE" -PathType leaf) ) {
if (Test-Path "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE" -PathType leaf) { mv -Force "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE" "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE.old" }
# find a free TCP port to run MariaDB/MySQL starting from the default port
Write-Host "Looking for a free TCP port for $MYSQL_NAME database..."
$ProgressPreference = 'SilentlyContinue'
### windows 10 only ####
#while ( Test-NetConnection $script:DATABASE_SERVER -Port $DATABASE_PORT -InformationLevel Quiet -ErrorAction SilentlyContinue -WarningAction SilentlyContinue ){
# Write-Host "Testing TCP port $DATABASE_PORT...."
# $script:DATABASE_PORT++
#}
### end windows 10 only ###
# convert port to integer
$script:DATABASE_PORT=[int]$DATABASE_PORT
### windows 7/10 ###
do {
$socktest = (New-Object System.Net.Sockets.TcpClient).ConnectAsync("$DATABASE_SERVER", $DATABASE_PORT).Wait(1000)
Write-Host "Testing TCP port $DATABASE_PORT...."
$script:DATABASE_PORT++
}
while ( $socktest )
$script:DATABASE_PORT--
### end windows 7/10 ###
Write-Host "Found TCP port $DATABASE_PORT!"
Write-Host "Writing $MYSQL_NAME config files..."
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE.dist").replace("DICOM_SIZE","$DICOM_MAX_SIZE") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("OH_PATH_SUBSTITUTE","$OH_PATH_SUBSTITUTE") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("DATABASE_SERVER","$DATABASE_SERVER") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("DATABASE_PORT","$DATABASE_PORT") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("MYSQL_DISTRO","$MYSQL_DIR") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("DATA_DIR","$DATA_DIR") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("TMP_DIR","$TMP_DIR") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
(Get-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE").replace("LOG_DIR","$LOG_DIR") | Set-Content "$OH_PATH/$CONF_DIR/$MYSQL_CONF_FILE"
}
}
###################################################################
function initialize_database {
# create data directory
[System.IO.Directory]::CreateDirectory("$OH_PATH/$DATA_DIR") > $null
# inizialize MariaDB/MySQL
Write-Host "Initializing $MYSQL_NAME database on port $DATABASE_PORT..."
switch -Regex ( $MYSQL_DIR ) {
"mariadb" {
try {
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysql_install_db.exe" -ArgumentList ("--datadir=`"$OH_PATH\$DATA_DIR`" --password=$DATABASE_ROOT_PW") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
catch {
Write-Host "Error: $MYSQL_NAME initialization failed! Exiting." -ForegroundColor Red
Read-Host; exit 2
}
}
"mysql" {
try {
Start-Process "$OH_PATH\$MYSQL_DIR\bin\mysqld.exe" -ArgumentList ("--initialize-insecure --basedir=`"$OH_PATH\$MYSQL_DIR`" --datadir=`"$OH_PATH\$DATA_DIR`" ") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR";
}
catch {
Write-Host "Error: $MYSQL_NAME initialization failed! Exiting." -ForegroundColor Red
Read-Host; exit 2
}
}
}
}
###################################################################
function start_database {
Write-Host "Checking if $MYSQL_NAME is running..."
if ( ( Test-Path "$OH_PATH/$TMP_DIR/mysql.sock" ) -or ( Test-Path "$OH_PATH/$TMP_DIR/mysql.pid" ) ) {
Write-Host "$MYSQL_NAME already running ! Exiting."
exit 1
}
Write-Host "Starting $MYSQL_NAME server... "
try {
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysqld.exe" -ArgumentList ("--defaults-file=`"$OH_PATH\$CONF_DIR\$MYSQL_CONF_FILE`" --tmpdir=`"$OH_PATH\$TMP_DIR`" --standalone") -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
Start-Sleep -Seconds 2
}
catch {
Write-Host "Error: $MYSQL_NAME server not started! Exiting." -ForegroundColor Red
Read-Host; exit 2
}
# wait till the MariaDB/MySQL socket file is created -> TO BE IMPLEMENTED
# while ( -e $OH_PATH/$MYSQL_SOCKET ); do sleep 1; done
# # Wait till the MariaDB/MySQL tcp port is open
# until nc -z $DATABASE_SERVER $DATABASE_PORT; do sleep 1; done
Write-Host "$MYSQL_NAME server started! "
}
###################################################################
function set_database_root_pw {
# if using MySQL root password need to be set
switch -Regex ( $MYSQL_DIR ) {
"mysql" {
Write-Host "Setting $MYSQL_NAME $DATABASE_ROOT_USER password..."
$SQLCOMMAND=@"
-u $DATABASE_ROOT_USER --skip-password -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp -e "ALTER USER '$DATABASE_ROOT_USER'@'$DATABASE_SERVER' IDENTIFIED BY '$DATABASE_ROOT_PW';"
"@
try {
Start-Process -FilePath "$OH_PATH/$MYSQL_DIR/bin/mysql.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
catch {
Write-Host "Error: $MYSQL_NAME root password not set! Try resetting installation with option [X]. Exiting." -ForegroundColor Red
shutdown_database;
Read-Host; exit 2
}
}
}
}
###################################################################
function create_database_user {
Write-Host "Creating database user [$DATABASE_USER]..."
# create database user
$SQLCOMMAND=@"
-u $DATABASE_ROOT_USER -p$DATABASE_ROOT_PW -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp -e "CREATE USER '$DATABASE_USER'@'$DATABASE_SERVER' IDENTIFIED BY '$DATABASE_PASSWORD'; CREATE USER '$DATABASE_USER'@'%' IDENTIFIED BY '$DATABASE_PASSWORD';"
"@
try {
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysql.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
catch {
Write-Host "Error: Database user creation failed! Exiting." -ForeGroundColor Red
shutdown_database;
Read-Host; exit 2
}
}
###################################################################
function create_database {
Write-Host "Creating database [$DATABASE_NAME]..."
# create OH database
$SQLCOMMAND=@"
-u $DATABASE_ROOT_USER -p$DATABASE_ROOT_PW -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp -e "CREATE DATABASE $DATABASE_NAME CHARACTER SET utf8; GRANT ALL PRIVILEGES ON $DATABASE_NAME.* TO '$DATABASE_USER'@'$DATABASE_SERVER'; GRANT ALL PRIVILEGES ON $DATABASE_NAME.* TO '$DATABASE_USER'@'%';"
"@
try {
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysql.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
catch {
Write-Host "Error: Database creation failed! Exiting." -ForeGroundColor Red
shutdown_database;
Read-Host; exit 2
}
}
###################################################################
function import_database {
Write-Host "Checking for SQL creation script..."
# check for database creation script
if (Test-Path "$OH_PATH/$SQL_DIR/$DB_CREATE_SQL" -PathType leaf) {
Write-Host "Using SQL file $SQL_DIR\$DB_CREATE_SQL..."
}
else {
Write-Host "Error: No SQL file found! Exiting." -ForeGroundColor Red
shutdown_database;
Read-Host; exit 2
}
# create OH database structure
Write-Host "Importing database [$DATABASE_NAME] with user [$DATABASE_USER@$DATABASE_SERVER]..."
cd "./$SQL_DIR"
$SQLCOMMAND=@"
--local-infile=1 -u $DATABASE_USER -p$DATABASE_PASSWORD -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp $DATABASE_NAME -e "source ./$DB_CREATE_SQL"
"@
try {
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysql.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
catch {
Write-Host "Error: Database not imported! Exiting." -ForeGroundColor Red
shutdown_database;
cd "$CURRENT_DIR"
Read-Host; exit 2
}
# EXPERIMENTAL ONLY
# workaround for hard coded password limit - execute extra sql script
# not needed anymore - see OP-1078
# if ( ($API_SERVER -eq "On") ){
# Write-Host "Setting admin password..."
# cd "$OH_PATH/$SQL_EXTRA_DIR/"
#
# $SQLCOMMAND=@"
# --local-infile=1 -u $DATABASE_ROOT_USER -p$DATABASE_ROOT_PW -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp $DATABASE_NAME -e "source ./reset_admin_password_strong.sql"
#"@
# try {
# Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysql.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
# }
# catch {
# Write-Host "Error! Exiting." -ForeGroundColor Red
# shutdown_database;
# cd "$CURRENT_DIR"
# Read-Host; exit 2
# }
# }
# end
cd "$OH_PATH"
}
###################################################################
function dump_database {
# save OH database if existing
if (Test-Path "$OH_PATH/$MYSQL_DIR/bin/mysqldump.exe" -PathType leaf) {
[System.IO.Directory]::CreateDirectory("$OH_PATH/$BACKUP_DIR") > $null
Write-Host "Dumping $MYSQL_NAME database..."
$SQLCOMMAND=@"
--skip-extended-insert -u $DATABASE_USER --password=$DATABASE_PASSWORD -h $DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp $DATABASE_NAME
"@
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysqldump.exe" -ArgumentList ("$SQLCOMMAND") -Wait -NoNewWindow -RedirectStandardOutput "$OH_PATH\$BACKUP_DIR\mysqldump_$DATE.sql" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
}
else {
Write-Host "Error: No mysqldump utility found! Exiting." -ForegroundColor Red
shutdown_database;
cd "$CURRENT_DIR"
Read-Host; exit 2
}
Write-Host "$MYSQL_NAME dump file $BACKUP_DIR/mysqldump_$DATE.sql completed!" -ForegroundColor Green
}
###################################################################
function shutdown_database {
if ( !( $OH_MODE -eq "CLIENT" ) ) {
Write-Host "Shutting down $MYSQL_NAME..."
Start-Process -FilePath "$OH_PATH\$MYSQL_DIR\bin\mysqladmin.exe" -ArgumentList ("-u $DATABASE_ROOT_USER -p$DATABASE_ROOT_PW --host=$DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp shutdown") -Wait -NoNewWindow -RedirectStandardOutput "$LOG_DIR/$LOG_FILE" -RedirectStandardError "$LOG_DIR/$LOG_FILE_ERR"
# wait till the $MYSQL_NAME socket file is removed -> TO BE IMPLEMENTED
# while ( -e $OH_PATH/$MYSQL_SOCKET ); do sleep 1; done
Start-Sleep -Seconds 2
Write-Host "$MYSQL_NAME stopped!"
}
else { # do nothing
}
}
###################################################################
function test_database_connection {
# test if mysql client is available
if (Test-Path "$OH_PATH/$MYSQL_DIR/bin/mysql.exe" -PathType leaf) {
# test connection to the OH MariaDB/MySQL database
Write-Host "Testing database connection..."
try {
Start-Process -FilePath ("$OH_PATH\$MYSQL_DIR\bin\mysql.exe") -ArgumentList ("--user=$DATABASE_USER --password=$DATABASE_PASSWORD --host=$DATABASE_SERVER --port=$DATABASE_PORT --protocol=tcp -e $([char]34)USE $DATABASE_NAME$([char]34) " ) -Wait -NoNewWindow
}
catch {
Write-Host "Error: can't connect to database! Exiting." -ForegroundColor Red
Read-Host; exit 2
}
# temporary disabled - catch not working
# Write-Host "Database connection successfully established!"
}
else {
Write-Host "Can't test database connection..."
}
}
###################################################################
function write_api_config_file {
######## application.properties setup - OH API server
if ( ($script:WRITE_CONFIG_FILES -eq "on") -or !(Test-Path "$OH_PATH/$OH_DIR/rsc/$API_SETTINGS" -PathType leaf) ) {
if (Test-Path "$OH_PATH/$OH_DIR/rsc/$API_SETTINGS" -PathType leaf) { mv -Force $OH_PATH/$OH_DIR/rsc/$API_SETTINGS $OH_PATH/$OH_DIR/rsc/$API_SETTINGS.old }
# generate OH API token and save to settings file
$JWT_TOKEN_SECRET=( -join ($(for($i=0; $i -lt 64; $i++) { ((65..90)+(97..122)+(".")+("!")+("?")+("&") | Get-Random | % {[char]$_}) })) )
Write-Host "Writing OH API configuration file -> $API_SETTINGS..."
(Get-Content "$OH_PATH/$OH_DIR/rsc/$API_SETTINGS.dist") `
-replace "JWT_TOKEN_SECRET", "$JWT_TOKEN_SECRET" `
-replace "OH_API_PID", "$OH_API_PID" `
-replace "API_HOST:API_PORT", "localhost:8080" `
| Set-Content "$OH_PATH/$OH_DIR/rsc/$API_SETTINGS"
}
}
###################################################################
function copy_config_file ($arg) {
# function to copy a single configuration file with backup
# usage: copy_config_file [file_name]
if ( ($script:WRITE_CONFIG_FILES -eq "on") -or !(Test-Path "$OH_PATH/$OH_DIR/rsc/$arg" -PathType leaf) ) {
if (Test-Path "$OH_PATH/$OH_DIR/rsc/$arg" -PathType leaf) { mv -Force $OH_PATH/$OH_DIR/rsc/$arg $OH_PATH/$OH_DIR/rsc/$arg.old }