forked from ESGF/esgf-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esg-installarg
89 lines (78 loc) · 2.2 KB
/
esg-installarg
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
#!/bin/bash
#
# "Hello, I want to have an argument."
# "No you don't."
# "Yes, I do!"
#
# Sourceable code for handling options in ESGF install scripts.
#
# Source this from the main installer file at the point where argument
# parsing needs to happen. For this to work, the main script MUST
# have two variables set at the top of the file:
# VERSION=xxx
# INSTALLPATH=xxx
#
# The following environment variables will be set for use by the
# sourcing script:
# PRINT_NEW_VERSION
# PRINT_OLD_VERSION
# INSTALLPATH (only if option specified)
#
# If PRINT_NEW_VERSION or PRINT_OLD_VERSION is set to true, this
# fragment will print the detected version and immediately exit.
#
#
# Written by Zed Pobre <[email protected]> in 2015 as a work-for-hire
# for the US Federal Government. This code is in the Public Domain.
#
# First stage of option parsing
# (This doesn't actually set the variables until the eval below)
PARSED_OPTIONS=$(getopt -n "$0" -o '' --long "newversion,oldversion,installpath:" -- "$@")
#Bad arguments, something has gone wrong with the getopt command.
if [ $? -ne 0 ];
then
echo "Option parsing failed!"
exit 1
fi
# This actually converts the valid options back into arguments at the
# front of the list
eval set -- "$PARSED_OPTIONS"
# Now we go through all of those until we get '--'
# This will loop infinitely if the argument list isn't prepared
# properly, so don't forget to check that getopt was actually
# successfull first.
while [ -n "$1" ]; do
case "$1" in
--newversion)
PRINT_NEW_VERSION=1
shift;;
--oldversion)
PRINT_OLD_VERSION=1
shift;;
--installpath)
# We need to actually check for an argument for this
if [ -n "$2" ]; then
INSTALLPATH=$2
else
echo "--installpath needs an argument"
exit 2
fi
shift 2;;
--)
shift
break;;
esac
done
if [ $PRINT_NEW_VERSION ] ; then
echo $VERSION
exit 0
fi
if [ $PRINT_OLD_VERSION ] ; then
INSTALLARG_FILE=`basename $0`
if [ -f "${INSTALLPATH}/${INSTALLARG_FILE}" ] ; then
grep '^VERSION=' "${INSTALLPATH}/${INSTALLARG_FILE}" | cut -d= -f2
else
exit 1
fi
exit 0
fi