File: mod.sh

package info (click to toggle)
plowshare 2.1.6-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 712 kB
  • sloc: sh: 5,030; makefile: 92; sed: 13
file content (356 lines) | stat: -rwxr-xr-x 10,353 bytes parent folder | download | duplicates (2)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env bash
#
# Easy module management (installation/update) utility
# Copyright (c) 2015-2016 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

declare -r VERSION='GIT-snapshot'

# Default repository source
declare -r LEGACY_MODULES='https://github.com/mcrapet/plowshare-modules-legacy.git'

declare -r ACTION_OPTIONS='
DO_INSTALL,i,install,,Install one or several given repositories to modules directory
DO_STATUS,s,status,,Print current modules configuration
DO_UPDATE,u,update,,Update modules directory (requires git)'

# This function is duplicated from download.sh
absolute_path() {
    local SAVED_PWD=$PWD
    local TARGET=$1

    while [ -L "$TARGET" ]; do
        DIR=$(dirname "$TARGET")
        TARGET=$(readlink "$TARGET")
        cd -P "$DIR"
    done

    if [ -f "$TARGET" ]; then
        DIR=$(dirname "$TARGET")
    else
        DIR=$TARGET
    fi

    cd -P "$DIR"
    TARGET=$PWD
    cd "$SAVED_PWD"
    echo "$TARGET"
}

# Print usage (on stdout)
usage() {
    cat <<EOH
Usage: plowmod ACTION [OPTIONS] [URL]...
Easy plowshare modules management.

Possible actions:
EOH
    print_options "$ACTION_OPTIONS"
    cat <<EOH

For install, if no source repository is specified, it uses:
$LEGACY_MODULES

Available options:
EOH
    print_options "$MAIN_OPTIONS"
}

# Compute modules.d/NAME from URL
get_dir_name() {
    local n=$1
    n=$(basename_file "${n%/}")
    if [[ $n =~ ([Pp]lowshare[-._])([Mm]odules?[-._])?([^/?#]*) ]] ; then
        echo "${BASH_REMATCH[3]}"
    else
        echo "$n"
    fi
}

# Install a new repository or archive file
# $1: local modules directory path
# $2: repository URL (file:// is accepted)
mod_install() {
    local L=$1
    local R=$2
    local RET=0

    log_notice "- installing new directory: $L"

    if [ -d "$L" -a -n "$HAVE_GIT" ]; then
        if git -C "$L" rev-parse --is-inside-work-tree &>/dev/null; then
            log_notice 'WARNING: directory already exists! Do a git pull.'
            git -C "$L" pull --quiet
        else
            # Check for empty directory (find -empty)
            if [[ ! $(ls -1A "$L" 2>/dev/null) ]] && [ -w "$L" ]; then
                # Be stupid for now and git clone. See --depth later.
                git clone --quiet "$R" "$L"
            else
                log_error 'ERROR: directory exists but it does not appear to be a git repository, abort'
                RET=$ERR_FATAL
            fi
        fi
    else
        # Be stupid for now and git clone. See --depth later.
        git clone --quiet "$R" "$L"
    fi
    return $RET
}

# Install a new repository
# $1: local modules directory path
# $2: repository URL or empty string (will detect .git)
mod_update() {
    local L=$1
    local RET=0

    log_notice "- updating directory: $L"

    if [ -d "$L" ]; then
        if [ -n "$HAVE_GIT" ]; then
            if git -C "$L" rev-parse --is-inside-work-tree &>/dev/null; then
                git -C "$L" pull --quiet
            else
                log_error 'ERROR: directory exists but it does not appear to be a git repository, abort!'
                RET=$ERR_FATAL
            fi
        else
            log_error 'ERROR: git is not installed, abort'
            RET=$ERR_SYSTEM
        fi
    else
        log_error 'ERROR: directory does not exists, abort'
        RET=$ERR_FATAL
    fi
    return $RET
}

# Information about modules location
# $1: System modules path
# $2: User modules or modules.d path
mod_status() {
    local -a SRCS
    local -A MLIST DLIST
    local -i N
    local D CONFIG

    if [ -d "$1/modules" -a -f "$1/modules/config" ]; then
        SRCS+=( "$1/modules" )
    fi

    if [ "${2%/modules.d}" = "$2" -a -f "$2/config" ]; then
        SRCS+=( "$2" )
    else
        if [ "${2%.d}" -a -f "${2%.d}/config" ]; then
            SRCS+=( "${2%.d}" )
        fi
        while read -r; do
            D=$(dirname "$REPLY")
            SRCS+=( "$D" )
        done < <(find "$2" -mindepth 2 -maxdepth 2 -name config)
    fi

    # Check for first install
    N=$(grep '^[^#]' "${SRCS[@]/%//config}" | wc -l)
    if (( N == 0 )); then
        log_error \
"-------------------------------------------------------------------------------
Your plowshare installation has currently no module.
($2/ is empty)

In order to use plowdown you must install some modules. Here is a quick start:
$ plowmod --install
-------------------------------------------------------------------------------"
        return $ERR_NOMODULE
    fi

    log_notice 'Found modules locations:'
    for D in "${SRCS[@]}"; do
        N=$(grep -c '^[^#]' "$D/config")
        if [ $N -eq 0 ]; then
            log_notice "- $D (no module detected)"
        elif [ $N -eq 1 ]; then
            log_notice "- $D ($N module found)"
        else
            log_notice "- $D ($N modules found)"
        fi
    done

    N=0
    for D in "${SRCS[@]}"; do
        CONFIG="$D/config"
        if [[ -d "$D" && -f "$CONFIG" ]]; then
            while read -r; do
                if [ -f "$D/$REPLY.sh" ]; then
                    if [[ ${MLIST["$REPLY"]} ]]; then
                        if [[ ! ${DLIST["$D"]} ]]; then
                            DLIST["$D"]=$(dirname ${MLIST["$REPLY"]})
                        fi
                        (( ++N ))
                    else
                        MLIST[$REPLY]="$D/$REPLY.sh"
                    fi
                else
                    log_error "$CONFIG: \`$REPLY\` module not found, ignoring"
                fi
            done < <(sed -ne "/^[^#]/s/^\([^[:space:]|]*\).*/\1/p" "$CONFIG")
        fi
    done
    if (( N == 0 )); then
        log_debug 'Modules directories overrides: no conflict detected!'
    else
        log_debug "Modules directories overrides: $N modules conflicts detected!"
        for D in "${!DLIST[@]}"; do
            log_debug "$D conflicts with ${DLIST["$D"]}"
        done
    fi

    return 0
}

#
# Main
#

# Get library directory
LIBDIR=$(absolute_path "$0")
readonly LIBDIR
TMPDIR=${TMPDIR:-/tmp}

set -e # enable exit checking

source "$LIBDIR/core.sh"

declare -r MAIN_OPTIONS="
HELP,h,help,,Show help info and exit
GETVERSION,,version,,Output plowmod version information and exit
VERBOSE,v,verbose,c|0|1|2|3|4=LEVEL,Verbosity level: 0=none, 1=err, 2=notice (default), 3=dbg, 4=report
QUIET,q,quiet,,Alias for -v0
NO_COLOR,,no-color,,Disables log notice & log error output coloring
MOD_DIR,,modules-directory,D=DIR,For maintainers only. Set modules directory (default is ${PLOWSHARE_CONFDIR/#$HOME/\~}/modules.d)"

# Process command-line (plowmod options).
# Note: Ignore returned UNUSED_ARGS[@], it will be empty.
eval "$(process_core_options 'plowmod' "$MAIN_OPTIONS$ACTION_OPTIONS" "$@")" || exit

test "$HELP" && { usage; exit 0; }
test "$GETVERSION" && { echo "$VERSION"; exit 0; }

if [ -n "$NO_COLOR" ]; then
    unset COLOR
else
    declare -r COLOR=yes
fi

# Verify verbose level
if [ -n "$QUIET" ]; then
    if [ -z "$VERBOSE" ]; then
        declare -r VERBOSE=0
    else
        log_notice "WARNING: --quiet switch conflicts with --verbose=$VERBOSE, ignoring -q"
    fi
elif [ -z "$VERBOSE" ]; then
    declare -r VERBOSE=2
fi

declare -a ARGS=("${UNUSED_OPTS[@]}")

if [ -z "$DO_INSTALL" -a -z "$DO_UPDATE" -a -z "$DO_STATUS" ]; then
    log_error 'plowmod: no action specified!'
    log_error "plowmod: try \`plowmod --help' for more information."
    exit $ERR_BAD_COMMAND_LINE
elif [ -n "$DO_INSTALL" -a -n "$DO_UPDATE" ]; then
    log_error 'plowmod: --install and --update are conflicting actions. You must choose only one.'
    exit $ERR_BAD_COMMAND_LINE
elif [ -z "$DO_INSTALL" -a "${#ARGS[@]}" -gt 0 ]; then
    # No argument for update or status
    log_notice "plowmod: two much arguments given, ignoring \`${ARGS[*]}'"
elif [ -n "$DO_INSTALL" ]; then
    # Check provided repositories
    REPOS=()
    for U in "${ARGS[@]}"; do
        if match_remote_url "$U"; then
            REPOS+=("$U")
        else
            log_notice "plowmod: invalid URL \`$U', ignoring"
        fi
    done
    if [ "${#ARGS[@]}" -le 0 ]; then
        log_notice "plowmod: adding legacy (default) repository: $LEGACY_MODULES"
        REPOS+=("$LEGACY_MODULES")
    elif [ "${#REPOS[@]}" -le 0 ]; then
        log_error "plowmod: no valid URL given. Maybe just try \`plowup -i' for default installation."
    fi
fi

# Check modules directory
if [ -z "$MOD_DIR" ]; then
    DDIR="$PLOWSHARE_CONFDIR/modules.d"
else
    DDIR=${MOD_DIR%/}
fi
log_debug "modules directory: $DDIR"

[ -d "$DDIR" ] || mkdir -p "$DDIR"
if [ ! -w "$DDIR" ]; then
    log_error 'ERROR: Modules directory is not writable, abort.'
    exit $ERR_BAD_COMMAND_LINE
fi

if check_exec 'git'; then
    HAVE_GIT=1
    log_report "[git ] $(git --version)"
fi

core_init 'plowmod'

declare -a RETVALS

if [ -n "$DO_INSTALL" ]; then
    for U in "${REPOS[@]}"; do
        RETVAL=0
        DDIR="$DDIR/$(get_dir_name "$U")"
        mod_install "$DDIR" "$U" || RETVAL=$?
        RETVALS+=($RETVAL)
    done
elif [ -n "$DO_STATUS" ]; then
    RETVAL=0
    mod_status "$LIBDIR" "$DDIR" || RETVAL=$?
    RETVALS+=($RETVAL)
elif [ -n "$DO_UPDATE" ]; then
    while read -r; do
        RETVAL=0
        U=$(dirname "$REPLY")
        mod_update "$U" || RETVAL=$?
        RETVALS+=($RETVAL)
    done < <(find "$DDIR" -mindepth 2 -maxdepth 2 -name config)
    test "$RETVAL" || \
        log_notice "plowmod: no directory found, have you invoked \`plowmod --install' first?"
fi

if [ ${#RETVALS[@]} -eq 0 ]; then
    exit 0
elif [ ${#RETVALS[@]} -eq 1 ]; then
    exit ${RETVALS[0]}
else
    log_debug "retvals:${RETVALS[*]}"
    # Drop success values
    RETVALS=(${RETVALS[@]/#0*} -$ERR_FATAL_MULTIPLE)

    exit $((ERR_FATAL_MULTIPLE + ${RETVALS[0]}))
fi