Debian Codegraph @

src/gmtswitch

raw symbols SCIP
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 1991-2026 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
4 # See LICENSE.TXT file for copying and redistribution conditions.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; version 3 or any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # Contact info: www.generic-mapping-tools.org
16 #-------------------------------------------------------------------------------
17 # gmtswitch - switch between several installed GMT versions
18 #
19 is_numeric()
20 { # Make sure argument is an integer
21 if [[ "$1" != *[!0-9]* ]]; then
22 return 0 # numeric
23 fi
24 return 1 # has a non-digit somewhere in it
25 }
26 exists()
27 {
28 if ! [ -d "${1}/bin" ]; then
29 # directory does not exist
30 echo "ERROR: ${1}/bin does not exist" >&2
31 exit 1
32 fi
33 }
34 help=0
35 if [ $# -gt 1 ]; then
36 help=1
37 fi
38 if [ $# -eq 1 ] && [ "X$1" = "X-help" ]; then
39 help=1
40 fi
41 if [ $help -eq 1 ]; then
42 cat << EOF >&2
43 usage: gmtswitch [version]
44
45 version, if present, is a unique text pattern identifying a GMT version,
46 e.g., GMT 5.1.1. If none are given then we list available versions and
47 let the user choose one. If the version is given as "init" OR it is the
48 very first time gmtswitch is run we will initialize the list of versions.
49 EOF
50 exit
51 fi
52 #--------------------------------------------------------------------------------
53 # Get the functionality of echo -n
54 #--------------------------------------------------------------------------------
55 if [ x$(echo -n) = x ]; then # echo -n works
56 echon()
57 {
58 echo -n "$*"
59 }
60 elif [ x$(echo -e) = x ]; then # echo -e works
61 echon()
62 {
63 echo -e "$*"'\c'
64 }
65 else # echo with escapes better work
66 echon()
67 {
68 echo "$*"'\c'
69 }
70 fi
71
72 cd ~
73 home=$(pwd)
74 if [ ! -f "$home/.gmtversions" ]; then # No .gmtversions exists yet, first do that part
75 cat << EOF >&2
76
77 GMTSWITCH
78
79 gmtswitch helps you modify your environment to allow for the switching back and
80 forth between several GMT versions, in particular GMT 5 and previous GMT
81 versions such as GMT 4. First, we try to determine all the GMT versions
82 currently installed on this platform. If you have not already installed the
83 latest GMT 4 please enter q at the prompt and do that install first.
84 Otherwise, hit RETURN and please be patient while we search your system.
85 Note: Searching from / can take hours on systems with lots of slow network
86 drives. If that is your case, type q at the prompt to quit and instead
87 manually create the file ~/.gmtversions from your knowledge of what versions
88 are installed. Each line in that file should be the full directory path to
89 a top-level GMT directory (i.e., one that contains share, bin, lib). Or,
90 you can type d and then be asked for what top level dir to start the search
91 from, e.g., /home/mydir
92
93 EOF
94 echon "==> Press RETURN to continue (or q RETURN to quit, or d RETURN to set top dir): " >&2
95 read s
96 if [ "x$s" = "xq" ]; then
97 exit
98 elif [ "x$s" = "x" ]; then
99 top=/
100 else
101 echon "==> Enter top dir: " >&2
102 read top
103 fi
104 echon "--> Searching..." >&2
105 ( find "$top" -type d -name 'GMT[345].*' -print > "$home/.gmtversions" ) 2> /dev/null
106 n=$(cat "$home/.gmtversions" | wc -l | awk '{print $1}')
107 cat << EOF >&2
108
109 There are $n GMT versions currently installed on your system.
110 Before we can allow for switching you must prepare your environment.
111
112 1. CSH OR TCSH USERS
113 In your .cshrc or .tcshrc file:
114
115 Change your path statement. Make sure it includes $home/this_gmt/bin
116 If $home contains spaces places the above within double quotes.
117
118 2. BASH USERS
119 (re)Place these lines in your .profile file:
120
121 Change your PATH statement. Make sure it includes $home/this_gmt/bin
122 If $home contains spaces places the above within double quotes.
123
124 3. GitHub USERS:
125 You will have to manually add the path to the GMT directory since
126 it is not considered an official release.
127
128 After making these edits you must logout and back in again.
129 The next time you run gmtswitch you may do the switching.
130 EOF
131 exit
132 fi
133
134 if [ "X$1" = "XD" ]; then # First entry is default
135 line=$(head -1 "$home/.gmtversions")
136 exists "$line"
137 rm -f "$home/this_gmt"
138 ln -sf "$line" "$home/this_gmt"
139 elif [ $# -eq 1 ]; then
140 line=$(grep $1 "$home/.gmtversions")
141 nl=$(grep $1 "$home/.gmtversions" | wc -l)
142 if [ $nl -eq 0 ]; then
143 echo "Version $1 is not listed among recognized GMT versions" >&2
144 echo "Run gmtswitch -help for more information" >&2
145 exit
146 fi
147 if [ $nl -ne 1 ]; then
148 echo "Version $1 is not unique among recognized GMT versions" >&2
149 echo "Run gmtswitch without argument and choose from a menu" >&2
150 exit
151 fi
152 exists "$line"
153 rm -f "$home/this_gmt"
154 ln -sf "$line" "$home/this_gmt"
155 else
156 n=$(cat "$home/.gmtversions" | wc -l | awk '{print $1}')
157 i=0
158 while [ $i -lt $n ]; do
159 i=$(expr $i + 1)
160 line=$(sed -n 's/#.*//;'${i}p "$home/.gmtversions")
161 comment=$(sed -n ${i}'s/.*# *\(.*\)/ (\1)/p' "$home/.gmtversions")
162 version=$(basename $line)
163 echo "$i. Select ${line}" >&2
164 done
165 echo "" >&2
166 v=-1
167 while [ $v -lt 0 ] || [ $v -gt $n ]; do
168 echon "Please choose a GMT version (1-$n) [1]: " >&2
169 read v
170 if [ "X$v" = "X" ]; then
171 v=1
172 fi
173 if ! is_numeric $v ; then
174 echo "ERROR: You must give an integer from 1-$n" >&2
175 v=-1
176 fi
177 done
178 line=$(sed -n 's/#.*//;'${v}p "$home/.gmtversions")
179 exists "$line"
180 rm -f "$home/this_gmt"
181 ln -sf "$line" "$home/this_gmt"
182 fi