-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprepare_ngas_root.sh
executable file
·174 lines (154 loc) · 4.52 KB
/
prepare_ngas_root.sh
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
#!/bin/bash
#
# Utility script to create a new NGAS root
#
# ICRAR - International Centre for Radio Astronomy Research
# (c) UWA - The University of Western Australia, 2018
# Copyright by UWA (in the framework of the ICRAR)
# All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
function print_usage {
echo "Creates and prepares a new NGAS root directory"
echo
echo "$0 [-h | -?] [-f] [-D] [-C] <NGAS root directory>"
echo
echo "-h, -?: Show this help"
echo "-f: Force creation of NGAS root, even if directory exists"
echo "-C: Do *not* create a configuration file"
echo "-D: Do *not* create an SQLite3 database"
echo
}
error() {
retcode=1
if [ $# = 2 ]
then
retcode=$2
fi
echo "ERROR: $1" 1>&2
exit $retcode
}
warning() {
echo "WARNING: $1" 1>&2
}
# Identify our system
platform=`uname`
# How we tell sed we want extended regular expressions
extended_regexp="-r"
if [ "${platform}" = "Darwin" ]
then
extended_regexp="-E"
fi
# Command-line option parsing
FORCE=
CREATE_DB=yes
CREATE_CFG=yes
while getopts "fDCh?" opt
do
case "$opt" in
f)
FORCE=yes
;;
D)
CREATE_DB=no
;;
C)
CREATE_CFG=no
;;
[h?])
print_usage
exit 0
;;
:)
print_usage
exit 1
esac
done
# One optional argument is required
if [ $(($# - $OPTIND)) -lt 0 ]
then
print_usage 1>&2
exit 1
fi
root_dir="${@:$OPTIND:1}"
if [ -d "${root_dir}" -a "$FORCE" != 'yes' ]
then
error "${root_dir} already exists, will not overwrite (use -f for that)" 2
fi
# Make sure we're standing where alongside this script
# in order to properly execute the rest of the stuff
this=$0
if [ -h $0 ]
then
this=$(readlink -f $0)
fi
cd "$(dirname $this)"
# Get the absolute name of root_dir, which is what will go
# to the configuration file
head=$(dirname "$root_dir")
head=$(cd "$head" && pwd)
root_dir=$head/$(basename "${root_dir}")
# Calculate now the rest of the files that depend on root_dir
cfg_file="${root_dir}/cfg/ngamsServer.conf"
db_file="${root_dir}/ngas.sqlite"
# Create and populate the root directory
echo "Creating NGAS root directory"
mkdir -p "${root_dir}" || error "Failed to create NGAS root directory"
cp -R NGAS/* "${root_dir}" || error "Failed to populate NGAS root with initial contents"
# Copy sample configuration file and adjust it to use an sqlite3 database
# located in the NGAS root directory (if required)
if [ "${CREATE_CFG}" = "yes" ]
then
echo "Creating and preparing initial configuration file"
cp cfg/sample_server_config.xml "${cfg_file}" || error "Failed to create initial configuration file"
sed ${extended_regexp} -i -e "s@RootDirectory=\"[^\"]+\"@RootDirectory=\"${root_dir}\"@g" "${cfg_file}" || error "Failed to set RootDirectory setting"
if [ "${CREATE_DB}" = "yes" ]
then
sed ${extended_regexp} -i -e "s@database=\"[^\"]+\"@database=\"${db_file}\"@g" "${cfg_file}" || error "Failed to set Db.database setting"
fi
fi
# Initialize the SQlite database
if [ "${CREATE_DB}" = "yes" ]
then
echo "Creating initial database"
sqlite3 "${db_file}" < src/ngamsCore/ngamsSql/ngamsCreateTables-SQLite.sql || error "Failed to create SQLite database file"
fi
echo
echo
echo "----------------------------------------------------------------------------"
echo "Successfully setup ${root_dir} as an NGAS root directory"
echo
if [ "${CREATE_CFG}" = "yes" ]
then
echo "A working configuration file has been created at ${cfg_file}."
if [ "${CREATE_DB}" = "yes" ]
then
echo "The configuration points to a working SQLite3 database created at ${db_file}."
fi
echo
elif [ "${CREATE_DB}" = "yes" ]
then
echo "A working SQLite3 database has been created at ${db_file}."
echo
fi
echo "To try out your new NGAS root, you can start an NGAS server with the following command:"
echo
echo "$> ngamsServer -cfg ${cfg_file} -v 4 -autoonline"
echo
echo "Then, hit Ctrl-C to stop the server."
echo "----------------------------------------------------------------------------"
echo