File: mccode-select.sh

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (307 lines) | stat: -rwxr-xr-x 6,950 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/sh

fullpath_dir() {
    (
        cd $(dirname $0);
        echo $(pwd)
    )
}

# Locate bin-folder and prefix from current script-location
BIN="$(fullpath_dir)"
PREFIX=$(dirname "${BIN}")

usage() {
    echo "usage: $0 [OPTION]... package [VERSION]";
    cat <<EOF
Select a specific version of McStas/McXtrace as default.

  --bootstrap  install called version of mccode-select and mccode-select-bundle.
  --list       list available versions of package.
  --install    install a new version of package for later linking.
  --dryrun     run checks and simulate actions only.
  --help       display this help and exit.

Report bugs to jsbn@fysik.dtu.dk
EOF
}

if [ "x$1" = "x" ] || [ "x$1" = "x--help" ] ; then
    usage;
    exit 0;
fi



# Check to see whether we have update-alternatives (Debian)
ALTERNATIVES=$(command -v update-alternatives)
if [ $? -eq 0 ] && [ -x ${ALTERNATIVES} ]; then
    HAS_ALTERNATIVES=true;
else
    HAS_ALTERNATIVES=false;
fi


if [ "x$1" = "x--no-update-alternatives" ]; then
    HAS_ALTERNATIVES=false;
    shift;
fi



if [ "x$1" = "x--bootstrap" ]; then
    VERSION=$(echo `basename $0` | cut -f'3-' -d'-')

    if [ "x" = "x${VERSION}" ]; then
        echo "Error: Cannot bootstrap without explicit version.";
        exit 1;
    fi

    if ${HAS_ALTERNATIVES}; then
        # Install mccode-select version
        $0 --install mccode-select        ${VERSION};
        $0 --install mccode-select-bundle ${VERSION};
        # Choose mccode-select version
        $0 mccode-select        ${VERSION};
        $0 mccode-select-bundle ${VERSION};
    else
        # Install mccode-select version
        $0 --no-update-alternatives --install mccode-select        ${VERSION};
        $0 --no-update-alternatives --install mccode-select-bundle ${VERSION};
        # Choose mccode-select version
        $0 --no-update-alternatives mccode-select        ${VERSION};
        $0 --no-update-alternatives mccode-select-bundle ${VERSION};
    fi
    exit 0;
fi



# Parse arguments
LIST=false;
INSTALL=false;
DOIT=true;

while true; do
    case "$1" in
        "--install" )
            INSTALL=true;
            ;;
        "--list" )
            LIST=true
            ;;
        "--dryrun" )
            DOIT=false;
            ;;
        * )
            # No match, drop out
            break;
    esac
    shift;
done

# Sanity check
if ${LIST} && [ "x${INSTALL}" = "x--install" ] ; then
    echo "Error: list and install cannot be combined. Pick one.";
    exit 1;
fi

# Check for help or missing package name after parsing arguments
if [ "x$1" = "x" ] || [ "x$1" = "x--help" ] ; then
    usage;
    exit 0;
fi

# Set name and version
NAME="$1"
VERSION="$2"

# Define dirname where existing non-overwritable binaries are moved
BACKUPDIR=$PREFIX/bin/MCCODE_BACKUP_`date +%Y%m%d_%H.%M`

linksTo() {
    (
        link=$1
        file=$2
        if ( ls -l "${link}" | grep "${file}"'$' > /dev/null ); then
            exit 0;
        fi
        ALT=/etc/alternatives/"${link}";

        if [ -e "${ALT}" ] &&
           ( ls -l "${link}" | grep "${ALT}"'$'  > /dev/null ) &&
           ( ls -l "${ALT}"  | grep "${file}"'$' > /dev/null ); then
            exit 0;
        fi

        # Link does not point to file
        exit 1;
    )
}

list() {
    (
        cd "${PREFIX}/bin";
        for ver in "${NAME}-"[0-9]*; do
            SEL=""
            if [ -L "${NAME}" ] && ( linksTo "${NAME}" "${ver}" ); then
                SEL='*'
            fi
            echo "${ver} ${SEL}" | sed s/"${NAME}\-/${NAME}"': '/;
        done
    )
}


flavor() {
    case "$1" in
        "mcstas" )
            echo "mc";
            ;;
        "mcxtrace" )
            echo "mx";
            ;;
    esac
}

whenReal() {
    if ${DOIT}; then
        $*
    fi
}


doLink() {
    (
        FROM="$1"
        TO="$2"

        if [ -L "${TO}" ]; then
            rm "${TO}";
        fi

        if [ -e "${TO}" ]; then
            echo "Error: cannot replace existing file: ${TO}";
            exit 1;
        fi

        ln -vs "${FROM}" "${TO}" ;
    )
}

check() {
    (
        name="$1"
        file="$2"
        link="$3"

        if ! [ -x "${file}" ]; then
            echo "${name}: cannot locate binary: ${file}"
            exit 1;
        fi
        if [ -e "${link}" ] && ( ! [ -L "${link}" ] ); then
	          echo
            echo "NOTE: ${name} was NOT a link: ${link}, moving to ${BACKUPDIR}/"
            echo

            ${DOIT} &&
            mkdir -p ${BACKUPDIR} &&
            mv ${link} ${BACKUPDIR}/`basename $link`;

            exit 0;
        fi
    )
}

installBinary() {
    (
        name="$1"
        vers="$2"

        link="${PREFIX}/bin/${name}"
        file="${link}-${vers}"

        # Priority:
        # only numbers and dot, then try and compute a priority from this
        prio=$( echo ${vers} | sed \
            -e 's/[^0-9\\.]//g' \
            -e 's/\./ * 100000 + /' | bc);
        # sanitise to only allow numbers (version can be something strange)
        prio=`echo ${prio} | sed 's/[^0-9]//g'`
        if [ "x${prio}" = "x" ]; then
            prio=1;
        fi

        # Python beats Perl
        if ( echo "${vers}" | grep '\-py' >/dev/null ); then
            prio=`echo ${prio} '+' 1 | bc`;
        fi

        # Sanity check
        if ! (check "${name}" "${file}" "${link}"); then
            exit 1;
        fi

        MANCMD=""
        manlink="${PREFIX}/man/man1/${name}.1"
        manfile="${PREFIX}/man/man1/${name}-${vers}.1"
        if [ -f "${manfile}" ]; then
            MANCMD="--slave ${manlink} ${name}.1 ${manfile}"
        fi

        # Install using update alternatives
        if ${HAS_ALTERNATIVES}; then
            echo "INSTALL: ${name}: ${file}"
            whenReal ${ALTERNATIVES} --install \
                "${link}" "${name}" "${file}" ${prio} \
                ${MANCMD} ;
        fi

        # When update-alternatives is not present --install only checks
    )
}

linkBinary() {
    (
        cd "${PREFIX}";

        name="$1";
        vers="$2"

        link="${PREFIX}/bin/${name}";
        file="${link}-${vers}";

        # Sanity check
        if ! (check "${name}" "${file}" "${link}"); then
            exit 1;
        fi


        if ${HAS_ALTERNATIVES}; then
            echo "${name} -> ${file}"
            whenReal installBinary "${name}" "${vers}"
            whenReal ${ALTERNATIVES} --set "${name}" "${file}"
            exit 0;
        fi
        whenReal doLink "${file}" "${link}"

        # Man pages are auto linked when using update-alternatives
        # The below is for manual linking only

        link="${PREFIX}/man/man1/${name}.1";
        file="${PREFIX}/man/man1/${name}-${vers}.1";

        if [ -f "${file}" ]; then
            whenReal doLink "${file}" "${link}";
        fi
    )
}


if ${LIST}; then
    list "${NAME}"
else
    if ${INSTALL}; then
        linkBinary "${NAME}" "${VERSION}"
    fi
fi