-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegdb
executable file
·129 lines (91 loc) · 2.86 KB
/
megdb
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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
# this might cause problems e.g. using read to read a heredoc cause read to always return non-zero
# set -o errexit
# Treat unset variables as an error when substituting.
set -o nounset
# 1. section: global constants (all words upper case separated by underscore)
declare TMP_DIR=${TMPDIR:-/tmp}/prog.$$.d
declare TMP_FILE_PREFIX=${TMPDIR:-/tmp}/prog.$$
# 1 = do clean else don't
declare -r DO_CLEAN=0
function cleanup() {
[[ "${DO_CLEAN}" -eq 1 ]] && {
echo "cleaning";
rm -f ${TMP_FILE_PREFIX}.* ;
}
}
function check_file() {
local file;
if [ -z "${1}" ]; then
echo "ERROR: empty argument to ${FUNCNAME}" >&2
return 100
else
file="$1"
fi
if [ ! -r ${file} ]; then
_exit "Error: File ${file} does not exist or is not readable." 100
fi
}
function _check_required_programs() {
# Required program(s)
req_progs=(readlink)
for p in ${req_progs[@]}; do
hash "${p}" 2>&- || \
{ echo >&2 " Required program \"${p}\" not installed or in search PATH.";
exit 1;
}
done
}
function render_template() {
eval "echo \"$(<$1)\""
}
# Single function
function main() {
#the optional paramters string
local -r OPTS=':f:'
while builtin getopts ${OPTS} opt "${@}"; do
case $opt in
f) declare -rg TEMPLATE_FILE=$(readlink -f ${OPTARG} )
check_file ${TEMPLATE_FILE}
;;
\?)
echo ${opt} ${OPTIND} 'is an invalid option'; usage; exit ${INVALID_OPTION}
;;
:)
echo 'required argument not found for option -'${OPTARG}; usage; exit ${INVALID_OPTION}
;;
*) echo "Too many options. Can not happen actually :)"
;;
esac
done
shift $((OPTIND-1))
local -r OUTPUT_DIR=${1:-${TMP_DIR}}
mkdir -pv ${OUTPUT_DIR} || {
echo "Could not create ${OUTPUT_DIR}";
exit 10;
}
echo "Writing results to ${OUTPUT_DIR}"
# depending on what files get generatetd by the template sql declare
# all but only cp the real generated one
local sample_file=${OUTPUT_DIR}/ena_sample.xml
local experiment_file=${OUTPUT_DIR}/ena_experiment.xml
local run_file=${OUTPUT_DIR}/ena_run.xml
local analysis_file=${OUTPUT_DIR}/ena_analysis.xml
PSQL_SOURCE_FILE=${TMP_FILE_PREFIX}.generated.sql
render_template ${TEMPLATE_FILE} > ${PSQL_SOURCE_FILE}
psql \
-e \
--set ON_ERROR_STOP=true \
--no-align \
--tuples-only \
-h localhost -U renzo -p 5491 -d megdb_r8 \
-f ${PSQL_SOURCE_FILE} || exit 10
cleanup
exit 0
}
# Always check return values and give informative return values.
# see https://google-styleguide.googlecode.com/svn/trunk/shell.xml?showone=Checking_Return_Values#Checking_Return_Values
trap "cleanup; exit 1" 1 2 3 13 15
# this is the main executable function at end of script
main "$@"