| 1 | #!/usr/bin/envscip-shell filesystem . . `/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_numericscip-shell shell shell-project 0.0.0 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 | existsscip-shell shell shell-project 0.0.0 exists().() |
| 27 | { |
| 28 | if ! [scip-shell system . . `[`. -d "${1}/bin" ]; then |
| 29 | # directory does not exist |
| 30 | echoscip-shell system . . echo. "ERROR: ${1}/bin does not exist" >&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | } |
| 34 | help=0 |
| 35 | if [scip-shell system . . `[`. $# -gt 1 ]; then |
| 36 | help=1 |
| 37 | fi |
| 38 | if [scip-shell system . . `[`. $# -eq 1 ] && [scip-shell system . . `[`. "X$1" = "X-help" ]; then |
| 39 | help=1 |
| 40 | fi |
| 41 | if [scip-shell system . . `[`. $help -eq 1 ]; then |
| 42 | catscip-shell system . . 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 [scip-shell system . . `[`. x$(echoscip-shell system . . echo. -n) = x ]; then # echo -n works |
| 56 | echonscip-shell shell shell-project 0.0.0 echon().() |
| 57 | { |
| 58 | echoscip-shell system . . echo. -n "$*" |
| 59 | } |
| 60 | elif [scip-shell system . . `[`. x$(echoscip-shell system . . echo. -e) = x ]; then # echo -e works |
| 61 | echonscip-shell shell shell-project 0.0.0 echon().() |
| 62 | { |
| 63 | echoscip-shell system . . echo. -e "$*"'\c' |
| 64 | } |
| 65 | else # echo with escapes better work |
| 66 | echonscip-shell shell shell-project 0.0.0 echon().() |
| 67 | { |
| 68 | echoscip-shell system . . echo. "$*"'\c' |
| 69 | } |
| 70 | fi |
| 71 | |
| 72 | cd ~ |
| 73 | home=$(pwdscip-shell system . . pwd.) |
| 74 | if [scip-shell system . . `[`. ! -f "$home/.gmtversions" ]; then # No .gmtversions exists yet, first do that part |
| 75 | catscip-shell system . . 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 | echonscip-shell shell shell-project 0.0.0 echon(). "==> Press RETURN to continue (or q RETURN to quit, or d RETURN to set top dir): " >&2 |
| 95 | read s |
| 96 | if [scip-shell system . . `[`. "x$s" = "xq" ]; then |
| 97 | exit |
| 98 | elif [scip-shell system . . `[`. "x$s" = "x" ]; then |
| 99 | top=/scip-shell filesystem . . `/`. |
| 100 | else |
| 101 | echonscip-shell shell shell-project 0.0.0 echon(). "==> Enter top dir: " >&2 |
| 102 | read top |
| 103 | fi |
| 104 | echonscip-shell shell shell-project 0.0.0 echon(). "--> Searching..." >&2 |
| 105 | ( findscip-shell system . . find. "$top" -type d -name 'GMT[345].*' -print > "$home/.gmtversions" ) 2> /dev/nullscip-shell filesystem . . `/dev/null`. |
| 106 | n=$(catscip-shell system . . cat. "$home/.gmtversions" | wcscip-shell system . . wc. -l | awkscip-shell system . . awk. '{print $1}') |
| 107 | catscip-shell system . . 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 [scip-shell system . . `[`. "X$1" = "XD" ]; then # First entry is default |
| 135 | line=$(headscip-shell system . . head. -1 "$home/.gmtversions") |
| 136 | existsscip-shell shell shell-project 0.0.0 exists(). "$line" |
| 137 | rmscip-shell system . . rm. -f "$home/this_gmt" |
| 138 | lnscip-shell system . . ln. -sf "$line" "$home/this_gmt" |
| 139 | elif [scip-shell system . . `[`. $# -eq 1 ]; then |
| 140 | line=$(grepscip-shell system . . grep. $1 "$home/.gmtversions") |
| 141 | nl=$(grepscip-shell system . . grep. $1 "$home/.gmtversions" | wcscip-shell system . . wc. -l) |
| 142 | if [scip-shell system . . `[`. $nl -eq 0 ]; then |
| 143 | echoscip-shell system . . echo. "Version $1 is not listed among recognized GMT versions" >&2 |
| 144 | echoscip-shell system . . echo. "Run gmtswitch -help for more information" >&2 |
| 145 | exit |
| 146 | fi |
| 147 | if [scip-shell system . . `[`. $nl -ne 1 ]; then |
| 148 | echoscip-shell system . . echo. "Version $1 is not unique among recognized GMT versions" >&2 |
| 149 | echoscip-shell system . . echo. "Run gmtswitch without argument and choose from a menu" >&2 |
| 150 | exit |
| 151 | fi |
| 152 | existsscip-shell shell shell-project 0.0.0 exists(). "$line" |
| 153 | rmscip-shell system . . rm. -f "$home/this_gmt" |
| 154 | lnscip-shell system . . ln. -sf "$line" "$home/this_gmt" |
| 155 | else |
| 156 | n=$(catscip-shell system . . cat. "$home/.gmtversions" | wcscip-shell system . . wc. -l | awkscip-shell system . . awk. '{print $1}') |
| 157 | i=0 |
| 158 | while [scip-shell system . . `[`. $i -lt $n ]; do |
| 159 | i=$(exprscip-shell system . . expr. $i + 1) |
| 160 | line=$(sedscip-shell system . . sed. -n 's/#.*//;'${i}p "$home/.gmtversions") |
| 161 | comment=$(sedscip-shell system . . sed. -n ${i}'s/.*# *\(.*\)/ (\1)/p' "$home/.gmtversions") |
| 162 | version=$(basenamescip-shell system . . basename. $line) |
| 163 | echoscip-shell system . . echo. "$i. Select ${line}" >&2 |
| 164 | done |
| 165 | echoscip-shell system . . echo. "" >&2 |
| 166 | v=-1 |
| 167 | while [scip-shell system . . `[`. $v -lt 0 ] || [scip-shell system . . `[`. $v -gt $n ]; do |
| 168 | echonscip-shell shell shell-project 0.0.0 echon(). "Please choose a GMT version (1-$n) [1]: " >&2 |
| 169 | read v |
| 170 | if [scip-shell system . . `[`. "X$v" = "X" ]; then |
| 171 | v=1 |
| 172 | fi |
| 173 | if ! is_numericscip-shell shell shell-project 0.0.0 is_numeric(). $v ; then |
| 174 | echoscip-shell system . . echo. "ERROR: You must give an integer from 1-$n" >&2 |
| 175 | v=-1 |
| 176 | fi |
| 177 | done |
| 178 | line=$(sedscip-shell system . . sed. -n 's/#.*//;'${v}p "$home/.gmtversions") |
| 179 | existsscip-shell shell shell-project 0.0.0 exists(). "$line" |
| 180 | rmscip-shell system . . rm. -f "$home/this_gmt" |
| 181 | lnscip-shell system . . ln. -sf "$line" "$home/this_gmt" |
| 182 | fi |