Files @ 9ca8e851e7e3
Branch filter:

Location: yaah/yaah

metalgamer
Added git to help output
  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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/bin/bash -

#:        Title: yaah - Yet another AUR helper
#:        Usage: yaah <packages>...
#:               yaag [options] [<packages]...
#:      Options: -h, --help                              Display help
#:               -c, --check                             Check installed, foreign packages if there are updates available
#:               -p, --parseable                         Check installed, foreign pacakges if there are updates available and print them in a parsable format
#:               -d DIRECTORY, --downloaddir DIRECTORY   Download packages to the specified directory
#:               -s KEYWORD, --search KEYWORD            Search for packages containing specified keyword
#:               -s MAINTAINER, --maintainer MAINTAINER  Search for packages from specified maintainer
#:               -v, --verbose                           Be more verbose, can be supplied at  maximum 2 times
#:               -q, --quiet                             Be less verbose. Takes precedence over -v and --verbose
#:               --version                               Prints version and exits
#: Requirements: jshon <http://kmkeen.com/jshon/>, pacman, tar, curl
#:      Version: 1.0.1
#:      Created: 2013-02-26
#:     Revision: 2015-08-20
#:       Author: Dennis Fink <dennis.fink@c3l.lu>

## Security
# Unalias everything
command unalias -a
# Secure PATH
PATH='/bin:/usr/bin'
export PATH
# Clearing the Command hash
hash -r
# Setting secure IFS
IFS=$' \t\n'
# Setting a secure umask
UMASK=002
umask $UMASK
# Expand aliases in command substitution
shopt -s expand_aliases

## Script metadata
SCRIPTNAME=${0##*/}
DESCRIPTION="Yet another AUR helper"
DATE_OF_CREATION=2013-02-26
DATE_OF_REVISION=2015-08-20
VERSION=1.0.1
AUTHOR="Dennis Fink <dennis.fink@c3l.lu>"

readonly SCRIPTNAME DESCRIPTION DATE_OF_CREATION DATE_OF_REVISION VERSION AUTHOR

## Constants
AURURL="http://aur.archlinux.org/"

readonly AURURL


## Set Colors (copied from makepkg)
if tput setaf 0 $>/dev/null; then
    ALL_OFF="$(tput sgr0)"
    RED="$(tput setaf 1)"
    GREEN="$(tput setaf 2)"
    YELLOW="$(tput setaf 3)"
    BLUE="$(tput setaf 4)"
    PURPLE="$(tput setaf 5)"
    CYAN="$(tput setaf 6)"
else
    ALL_OFF="\e[1;0m"
    BOLD="\[1;1m"
    RED="\e[1;31m"
    GREEN="\e[1;32m"
    YELLOW="\e[1;33m"
    BLUE="\e[1;34m"
    PURPLE="\e[1:35m"
    CYAN="\e[1:36m"
fi
COLS="$(tput cols)"

readonly ALL_OFF BOLD RED GREEN YELLOW BLUE PURPLE CYAN COLS

# Alias commands to save characters
alias jshon='jshon -Q'
alias curl='curl -s -fL'

#=== FUNCTION =================================================================
#        Name: msg
# Description: Print message with a green pretag if $quiet is not set
# Parameter 1: Message to print
#==============================================================================

# copied from makepkg and modified
msg () {

    if [[ $quiet -eq 0 ]]; then
        local mesg=$1; shift
        printf "${GREEN}==>${ALL_OFF}${BOLD} %s${ALL_OFF}\n" "$mesg" "$@"
    fi
}

#=== FUNCTION =================================================================
#        Name: msg_verbose1
# Description: Prints message if $verbose is 1 or greater
# Parameter 1: Message to print
#==============================================================================

msg_verbose1 () {

    if [[ $verbose -ge 1 ]]; then
        msg "$1"
    fi

}

#=== FUNCTION =================================================================
#        Name: msg_verbose2
# Description: Prints message if $verbose is 2 or greater
# Parameter 1: Message to print
#==============================================================================

msg_verbose2 () {

    if [[ $verbose -ge 2 ]]; then
        msg "$1"
    fi

}

#=== FUNCTION =================================================================
#        Name: msg2
# Description: Print message with a blue pretag
# Parameter 1: Message to print
#==============================================================================

# copied from makepkg and modified
msg2 () {

    local mesg=$1; shift
    printf "    ${BLUE}==>${ALL_OFF}${BOLD} %s${ALL_OFF}\n" "$mesg" "$@"

}

#=== FUNCTION =================================================================
#        Name: error
# Description: Print message with a red pretag an ERROR
# Parameter 1: Message to print
#==============================================================================

# copied from makepkg
error() {

    local mesg=$1; shift
    printf "${RED}==> ERROR:${ALL_OFF}${BOLD} %s${ALL_OFF}\n" "$mesg" "$@" >&2

}

#=== FUNCTION =================================================================
#        Name: search
# Description: Search packages in AUR
# Parameter 1: Type of search
# Parameter *: Keywords to search for
#==============================================================================

search() {

    if [[ $# -lt 2 ]]; then
        error "Not enough parameters supplied"
        return 1
    fi

    local type=$1
    shift
    if [[ "$type" == "msearch" ]]; then
        msg "Searching AUR for packages by maintainer $1"
    else
        msg "Searching AUR for $1"
    fi

    local url="${AURURL}rpc.php?type=$type&arg=$*"

    msg_verbose2 "URL is: ${url}"

    local aur_response=$(curl "${url}")

    local resultcount=$(jshon -e resultcount <<< "$aur_response")
    aur_response=$(jshon -e results <<< "$aur_response")

    if [[ $? -ne 0 ]]; then
        error "Packages infos could not be loaded!"
        return 2
    fi

    msg_verbose2 "Getting number of objects in json array!"

    msg_verbose1 "There are $resultcount packages found."
    msg_verbose2 "Looping over json array!"

    if [[ "$quiet" -eq 0 ]]; then
        printf "\n"
    fi

    for i in $(seq 0 $((resultcount-1))); do

        local package=$(jshon -e "$i" <<< "$aur_response")

        local package_name=$(jshon -e Name -u <<< "$package")
        local package_version=$(jshon -e Version -u <<< "$package")
        local package_description=$(fold -sw "$COLS" <<< "$(jshon -e Description -u <<< "$package")")
        local out_of_date=$(jshon -e OutOfDate <<< "$package")
        local num_votes=$(jshon -e NumVotes <<< "$package")
        local maintainer=$(jshon -e Maintainer -u <<< "$package")
        local last_modified=$(date -d @"$(jshon -e LastModified <<< "$package")")

        if [[ $out_of_date -eq 0 ]]; then
            local color="${GREEN}"
        else
            local color="${RED}"
        fi
        printf "${PURPLE}%s${ALL_OFF} ${color}%s${ALL_OFF} ${BLUE}%s${ALL_OFF} ${YELLOW}%s${ALL_OFF} ${CYAN}%s${ALL_OFF}\n%s\n" "$package_name" "$package_version" "$last_modified" "$num_votes" "$maintainer" "$package_description"

    done
}


#=== FUNCTION =================================================================
#        Name: check_version
# Description: Check version between two packages and print a message if the
#              later one is newer
# Parameter 1: Name of the package
# Parameter 2: Version of the local package
# Parameter 3: Version of the online package
#==============================================================================

check_version () {

    if [[ $# -ne 3 ]]; then
        error "Not enough parameters supplied"
        return 1
    fi

    if [[ ! "$parseable_flag" ]]; then
        msg "Checking version for '$1'"
    fi

    if [[ $(vercmp "$2" "$3") -eq -1 ]]; then
        if [[ "$parseable_flag" ]]; then
            printf "%s ${GREEN}%s${ALL_OFF}\n" "$1" "$3"
        else
            msg2 "New version for '$1': $2 -> $3"
        fi
    fi

}

#=== FUNCTION =================================================================
#        Name: query_online_version
# Description: Querying online packages version
#==============================================================================

query_online_version () {

    msg "Getting version of online packages."

    local arguments=""

    msg_verbose2 "Creating argument list."

    for package in "${!packages[@]}"; do
        arguments+="&arg\[\]=${package}"
    done

    msg_verbose1 "Sending query to AUR."

    local url="${AURURL}rpc.php?type=multiinfo${arguments}"

    msg_verbose2 "URL is: ${url}"

    local aur_response=$(curl "${url}")

    if [[ $? -ne 0 ]]; then
        error "Packages infos could not be loaded!"
        return 2
    fi
    msg_verbose2 "Getting number of objects in json array!"
    local resultcount=$(jshon -e resultcount <<< "$aur_response")
    aur_response=$(jshon -e results <<< "$aur_response")
    msg_verbose2 "Looping over json array!"

    for i in $(seq 0 $((resultcount-1))); do

        msg_verbose2 "Extracting package name."

        local package=$(jshon -e "$i" <<< "$aur_response")
        local package_name=$(jshon -e Name -u <<< "$package")

        msg_verbose2 "Extracting old package version."

        local old_package_version=${packages[$package_name]}

        msg_verbose2 "Extracting new package version."

        local new_package_version=$(jshon -e Version -u <<< "$package")

        check_version "$package_name" "$old_package_version" "$new_package_version"
        unset packages[$package_name]
    done

    msg_verbose2 "Check for missing packages."

    for package in "${!packages[@]}"; do
        msg "$package was not found."
    done

}

#=== FUNCTION =================================================================
#        Name: query_local_version
# Description: Querying local packages version
# Parameter *: Optional. The packages to query, if not specified, querying all
#              packages
#==============================================================================

query_local_version () {

    msg "Getting version of installed packages."

    declare -A packages

    local IFS=$'\n'
    for package in $(pacman -Qm "$@"); do
        local package_name=${package%\ *}
        local package_version=${package#*\ }
        packages[$package_name]=$package_version
    done


    if [[ $? -eq 0 ]]; then
        msg "There are ${#packages[@]} foreign packages to check."
        query_online_version
    else
        error "Packages could not be loaded!"
        return 2
    fi

}

#=== FUNCTION =================================================================
#        Name: extract_and_remove
# Description: Extracting a tar.gz file and remove the archive file
# Parameter 1: File to extract
#==============================================================================

extract_and_remove () {

    if [[ $# -ne 1 ]]; then
        error "Not enough parameters supplied"
        return 1
    fi

    if [[ -f $1 ]]; then

        msg "Extracting '$1'"

        tar xzf "$1" -C "$downloaddir"

        msg_verbose2 "Setting trap to remove extracted directory"
        trap "rm -rf '$downloaddir/${1%.tar.gz}'" EXIT

        msg_verbose1 "Removing '$1'"
        rm "$1"
    else
        error "'$1' is not a file"
        return 2
    fi

}

#=== FUNCTION =================================================================
#        Name: download
# Description: Download a package from the AUR and extracting it
# Parameter 1: The package name to download
#==============================================================================

download () {

    if [[ $# -eq 0 ]]; then
        error "Not enoug parameters supplied!"
        return 1
    fi

    until [[ -z "$1" ]]; do

        if [[ -z $git_flag ]]; then
            local filename="${1}.tar.gz"
            msg "Downloading '$filename' to '$downloaddir'"
            curl -o "$downloaddir/$filename" "${AURURL}cgit/aur.git/snapshot/$filename"
        else
            local gitrepo="${1}.git"
            msg "Cloning '$1' into '$downloaddir'"
            git clone --quiet "${AURURL}/$gitrepo"
        fi

        if [[ $? -eq 0 ]]; then
            if [[ $git_flag -ne 1 ]]; then
                msg_verbose2 "Setting trap to remove downloaded file"
                trap "rm -f '$downloaddir/$filename'" EXIT
                extract_and_remove "$downloaddir/$filename"
            else
                msg_verbose2 "Checking if we downloaded an empty repository"
                if [[ ! -e "$downloaddir/$1/PKGBUILD" ]]; then
                    rm -fr "$downloaddir/$1"
                    error "'$1' could not be found!"
                    return 2
                fi
            fi
        else
            error "'$1' could not be found!"
            return 2
        fi
        shift
    done

}

#=== FUNCTION =================================================================
#        Name: version
# Description: Prints version information
#==============================================================================

version() {

    printf "Scriptname: %s
Version: %s
Description: %s
Author: %s
Date of creation: %s
Date of revision: %s\n" "$SCRIPTNAME" "$VERSION" "$DESCRIPTION" "$AUTHOR" "$DATE_OF_CREATION" "$DATE_OF_REVISION"
}


#=== FUNCTION =================================================================
#        Name: usage
# Description: Prints the usage
#==============================================================================

usage() {

    printf "%s - %s - %s

    Download packages from AUR or check installed, foreign packages, if there are updates available.

    Usage:
      %s <packages>...
      %s [options] [<packages>]...

    Options:
      -h, --help                              Display help
      -c, --check                             Check installed, foreign packages if there are updates available
      -p, --parseable                         Check installed, foreign packages if there are updates available and print them in a parseable format
      -d DIRECTORY, --downloaddir DIRECTORY   Download packages to the specified directory
      -s KEYWORDS, --search KEYWORDS          Search for packages containing specified keywords
      -m MAINTAINER, --maintainer MAINTAINER  Search packages from specified maintainer
      -g, --git                               Download packages using git
      -v, --verbose                           Be more verbose, can be supplied at  maximum 2 times
      -q, --quiet                             Be less verbose. Takes precedence over -v and --verbose
      --version                               Print version information and exits

    For complete documentation, run: man 8 yaah\n" "$SCRIPTNAME" "$VERSION" "$DESCRIPTION" "$SCRIPTNAME" "$SCRIPTNAME"
}


check_flag=
search_flag=
maintainer_flag=
parseable_flag=
git_flag=0
verbose=0
quiet=0
downloaddir="$PWD"

if [[ $# -eq 0 ]]; then
    usage
    exit 1
fi

# Check for dependencies
hash curl 2>/dev/null || { error "curl is not installed!"; exit 3; }
hash tar 2>/dev/null || { error "tar is not installed!"; exit 3; }
hash pacman 2>/dev/null || { error "pacman is not installed!"; exit 3; }
hash jshon 2>/dev/null || { error "jshon is not installed"; exit 3; }


while true; do
    case $1 in
        -h|--help|-\?)
            usage
            exit 0
            ;;
        -c|--check)
            check_flag=1
            shift
            ;;
        -d|--downloaddir)
            downloaddir=$2
            shift 2
            ;;
        -v|--verbose)
            verbose=1
            shift
            ;;
        -vv)
            verbose=2
            shift
            ;;
        -q|--quiet)
            quiet=1
            shift
            ;;
        -s|--search)
            search_flag=1
            shift
            ;;
        -m|--maintainer)
            maintainer_flag=1
            shift
            ;;
        -p|--parseable)
            check_flag=1
            parseable_flag=1
            quiet=1
            shift
            ;;
        -g|--git)
            git_flag=1
            hash git 2>/dev/null || { error "git is not installed"; exit 3; }
            shift
            ;;
        --version)
            version
            exit 0
            ;;
        --)
            shift
            break
            ;;
        -*)
            error "Unknown option: $1"
            usage
            exit 1
            ;;
        *)
            break
            ;;
    esac
done

# check if downloaddir is a directory and remove trailing slash
if [[ -d $downloaddir ]]; then
    if [[ "$downloaddir" == "./" ]]; then
        downloaddir="$PWD"
    else
        downloaddir=${downloaddir%/}
    fi
else
    error "$downloaddir is not a directory"
    exit 1
fi

if [[ "$check_flag" ]]; then
    query_local_version "$@"
elif [[ "$search_flag" ]]; then
    search "search" "$@"
elif [[ "$maintainer_flag" ]]; then
    search "msearch" "$@"
else
    download "$@"
fi