-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathgmtswitch
executable file
·182 lines (169 loc) · 5.61 KB
/
gmtswitch
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
#!/usr/bin/env bash
#
# Copyright (c) 1991-2025 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
# See LICENSE.TXT file for copying and redistribution conditions.
#
# This program 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; version 3 or any later version.
#
# 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 Lesser General Public License for more details.
#
# Contact info: www.generic-mapping-tools.org
#-------------------------------------------------------------------------------
# gmtswitch - switch between several installed GMT versions
#
is_numeric()
{ # Make sure argument is an integer
if [[ "$1" != *[!0-9]* ]]; then
return 0 # numeric
fi
return 1 # has a non-digit somewhere in it
}
exists()
{
if ! [ -d "${1}/bin" ]; then
# directory does not exist
echo "ERROR: ${1}/bin does not exist" >&2
exit 1
fi
}
help=0
if [ $# -gt 1 ]; then
help=1
fi
if [ $# -eq 1 ] && [ "X$1" = "X-help" ]; then
help=1
fi
if [ $help -eq 1 ]; then
cat << EOF >&2
usage: gmtswitch [version]
version, if present, is a unique text pattern identifying a GMT version,
e.g., GMT 5.1.1. If none are given then we list available versions and
let the user choose one. If the version is given as "init" OR it is the
very first time gmtswitch is run we will initialize the list of versions.
EOF
exit
fi
#--------------------------------------------------------------------------------
# Get the functionality of echo -n
#--------------------------------------------------------------------------------
if [ x$(echo -n) = x ]; then # echo -n works
echon()
{
echo -n "$*"
}
elif [ x$(echo -e) = x ]; then # echo -e works
echon()
{
echo -e "$*"'\c'
}
else # echo with escapes better work
echon()
{
echo "$*"'\c'
}
fi
cd ~
home=$(pwd)
if [ ! -f "$home/.gmtversions" ]; then # No .gmtversions exists yet, first do that part
cat << EOF >&2
GMTSWITCH
gmtswitch helps you modify your environment to allow for the switching back and
forth between several GMT versions, in particular GMT 5 and previous GMT
versions such as GMT 4. First, we try to determine all the GMT versions
currently installed on this platform. If you have not already installed the
latest GMT 4 please enter q at the prompt and do that install first.
Otherwise, hit RETURN and please be patient while we search your system.
Note: Searching from / can take hours on systems with lots of slow network
drives. If that is your case, type q at the prompt to quit and instead
manually create the file ~/.gmtversions from your knowledge of what versions
are installed. Each line in that file should be the full directory path to
a top-level GMT directory (i.e., one that contains share, bin, lib). Or,
you can type d and then be asked for what top level dir to start the search
from, e.g., /home/mydir
EOF
echon "==> Press RETURN to continue (or q RETURN to quit, or d RETURN to set top dir): " >&2
read s
if [ "x$s" = "xq" ]; then
exit
elif [ "x$s" = "x" ]; then
top=/
else
echon "==> Enter top dir: " >&2
read top
fi
echon "--> Searching..." >&2
( find "$top" -type d -name 'GMT[345].*' -print > "$home/.gmtversions" ) 2> /dev/null
n=$(cat "$home/.gmtversions" | wc -l | awk '{print $1}')
cat << EOF >&2
There are $n GMT versions currently installed on your system.
Before we can allow for switching you must prepare your environment.
1. CSH OR TCSH USERS
In your .cshrc or .tcshrc file:
Change your path statement. Make sure it includes $home/this_gmt/bin
If $home contains spaces places the above within double quotes.
2. BASH USERS
(re)Place these lines in your .profile file:
Change your PATH statement. Make sure it includes $home/this_gmt/bin
If $home contains spaces places the above within double quotes.
3. GitHub USERS:
You will have to manually add the path to the GMT directory since
it is not considered an official release.
After making these edits you must logout and back in again.
The next time you run gmtswitch you may do the switching.
EOF
exit
fi
if [ "X$1" = "XD" ]; then # First entry is default
line=$(head -1 "$home/.gmtversions")
exists "$line"
rm -f "$home/this_gmt"
ln -sf "$line" "$home/this_gmt"
elif [ $# -eq 1 ]; then
line=$(grep $1 "$home/.gmtversions")
nl=$(grep $1 "$home/.gmtversions" | wc -l)
if [ $nl -eq 0 ]; then
echo "Version $1 is not listed among recognized GMT versions" >&2
echo "Run gmtswitch -help for more information" >&2
exit
fi
if [ $nl -ne 1 ]; then
echo "Version $1 is not unique among recognized GMT versions" >&2
echo "Run gmtswitch without argument and choose from a menu" >&2
exit
fi
exists "$line"
rm -f "$home/this_gmt"
ln -sf "$line" "$home/this_gmt"
else
n=$(cat "$home/.gmtversions" | wc -l | awk '{print $1}')
i=0
while [ $i -lt $n ]; do
i=$(expr $i + 1)
line=$(sed -n 's/#.*//;'${i}p "$home/.gmtversions")
comment=$(sed -n ${i}'s/.*# *\(.*\)/ (\1)/p' "$home/.gmtversions")
version=$(basename $line)
echo "$i. Select ${line}" >&2
done
echo "" >&2
v=-1
while [ $v -lt 0 ] || [ $v -gt $n ]; do
echon "Please choose a GMT version (1-$n) [1]: " >&2
read v
if [ "X$v" = "X" ]; then
v=1
fi
if ! is_numeric $v ; then
echo "ERROR: You must give an integer from 1-$n" >&2
v=-1
fi
done
line=$(sed -n 's/#.*//;'${v}p "$home/.gmtversions")
exists "$line"
rm -f "$home/this_gmt"
ln -sf "$line" "$home/this_gmt"
fi