util.sh 17.9 KB
Newer Older
artoo's avatar
artoo committed
1 2 3 4 5 6 7 8 9 10
#!/bin/bash
# This program 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; version 2 of the License.
#
# This program 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.

11
# $1: section
12
parse_section() {
udeved's avatar
udeved committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    local is_section=0
    while read line; do
        [[ $line =~ ^\ {0,}# ]] && continue
        [[ -z "$line" ]] && continue
        if [ $is_section == 0 ]; then
            if [[ $line =~ ^\[.*?\] ]]; then
                line=${line:1:$((${#line}-2))}
                section=${line// /}
                if [[ $section == $1 ]]; then
                    is_section=1
                    continue
                fi
                continue
            fi
        elif [[ $line =~ ^\[.*?\] && $is_section == 1 ]]; then
            break
        else
            pc_key=${line%%=*}
            pc_key=${pc_key// /}
            pc_value=${line##*=}
            pc_value=${pc_value## }
            eval "$pc_key='$pc_value'"
        fi
    done < "$2"
37 38 39
}

get_repos() {
udeved's avatar
udeved committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
    local section repos=() filter='^\ {0,}#'
    while read line; do
        [[ $line =~ "${filter}" ]] && continue
        [[ -z "$line" ]] && continue
        if [[ $line =~ ^\[.*?\] ]]; then
            line=${line:1:$((${#line}-2))}
            section=${line// /}
            case ${section} in
                "options") continue ;;
                *) repos+=("${section}") ;;
            esac
        fi
    done < "$1"
    echo ${repos[@]}
54
}
55

56
check_user_repos_conf(){
udeved's avatar
udeved committed
57 58 59 60 61 62
    local repositories=$(get_repos "$1") uri='file://'
    for repo in ${repositories[@]}; do
        msg2 "parsing repo [%s] ..." "${repo}"
        parse_section "${repo}" "$1"
        [[ ${pc_value} == $uri* ]] && die "Using local repositories is not supported!"
    done
63
}
udeved's avatar
udeved committed
64

65
get_pac_mirrors_conf(){
udeved's avatar
udeved committed
66 67 68 69
    local conf="$tmp_dir/pacman-mirrors-$1.conf"
    cp "${DATADIR}/pacman-mirrors.conf" "$conf"
    sed -i "$conf" \
        -e "s|Branch = stable|Branch = $1|"
70

udeved's avatar
udeved committed
71
    echo "$conf"
72 73
}

udeved's avatar
udeved committed
74
read_build_list(){
udeved's avatar
udeved committed
75 76 77
    local _space="s| ||g" \
        _clean=':a;N;$!ba;s/\n/ /g' \
        _com_rm="s|#.*||g"
78

udeved's avatar
udeved committed
79 80 81
    build_list=$(sed "$_com_rm" "$1.list" \
        | sed "$_space" \
        | sed "$_clean")
82 83
}

udeved's avatar
udeved committed
84 85
# $1: list_dir
show_build_lists(){
udeved's avatar
udeved committed
86 87 88 89 90 91
    local list temp
    for item in $(ls $1/*.list); do
        temp=${item##*/}
        list=${list:-}${list:+|}${temp%.list}
    done
    echo $list
92 93
}

94
# $1: make_conf_dir
udeved's avatar
udeved committed
95
show_build_profiles(){
udeved's avatar
udeved committed
96
    local cpuarch temp
udeved's avatar
udeved committed
97 98
    for item in $(ls $1/*.conf); do
        temp=${item##*/}
udeved's avatar
udeved committed
99
        cpuarch=${cpuarch:-}${cpuarch:+|}${temp%.conf}
udeved's avatar
udeved committed
100
    done
udeved's avatar
udeved committed
101
    echo $cpuarch
102 103
}

udeved's avatar
udeved committed
104 105 106
# $1: list_dir
# $2: build list
eval_build_list(){
udeved's avatar
udeved committed
107 108 109 110
    eval "case $2 in
        $(show_build_lists $1)) is_build_list=true; read_build_list $1/$2 ;;
        *) is_build_list=false ;;
    esac"
111 112
}

udeved's avatar
udeved committed
113
in_array() {
udeved's avatar
udeved committed
114 115 116 117 118 119
    local needle=$1; shift
    local item
    for item in "$@"; do
        [[ $item = $needle ]] && return 0 # Found
    done
    return 1 # Not Found
udeved's avatar
udeved committed
120 121
}

udeved's avatar
udeved committed
122
get_timer(){
udeved's avatar
udeved committed
123
    echo $(date +%s)
udeved's avatar
udeved committed
124 125
}

126

udeved's avatar
udeved committed
127 128
# $1: start timer
elapsed_time(){
udeved's avatar
udeved committed
129
    echo $(echo $1 $(get_timer) | awk '{ printf "%0.2f",($2-$1)/60 }')
udeved's avatar
udeved committed
130 131
}

132
show_elapsed_time(){
udeved's avatar
udeved committed
133
    info "Time %s: %s minutes" "$1" "$(elapsed_time $2)"
134 135
}

136
lock() {
udeved's avatar
udeved committed
137 138 139 140 141 142
    eval "exec $1>"'"$2"'
    if ! flock -n $1; then
        stat_busy "$3"
        flock $1
        stat_done
    fi
143 144 145
}

slock() {
udeved's avatar
udeved committed
146 147 148 149 150 151
    eval "exec $1>"'"$2"'
    if ! flock -sn $1; then
        stat_busy "$3"
        flock -s $1
        stat_done
    fi
152 153
}

artoo's avatar
artoo committed
154
check_root() {
udeved's avatar
udeved committed
155 156 157 158 159 160
    (( EUID == 0 )) && return
    if type -P sudo >/dev/null; then
        exec sudo -- "$@"
    else
        exec su root -c "$(printf ' %q' "$@")"
    fi
artoo's avatar
artoo committed
161 162
}

163
copy_mirrorlist(){
udeved's avatar
udeved committed
164
    cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/"
165 166
}

167
copy_keyring(){
udeved's avatar
udeved committed
168 169 170
    if [[ -d /etc/pacman.d/gnupg ]] && [[ ! -d $1/etc/pacman.d/gnupg ]]; then
        cp -a /etc/pacman.d/gnupg "$1/etc/pacman.d/"
    fi
udeved's avatar
udeved committed
171 172
}

artoo's avatar
artoo committed
173
load_vars() {
udeved's avatar
udeved committed
174
    local var
175

udeved's avatar
udeved committed
176
    [[ -f $1 ]] || return 1
artoo's avatar
artoo committed
177

udeved's avatar
udeved committed
178 179 180
    for var in {SRC,SRCPKG,PKG,LOG}DEST MAKEFLAGS PACKAGER CARCH GPGKEY; do
        [[ -z ${!var} ]] && eval $(grep -a "^${var}=" "$1")
    done
181

udeved's avatar
udeved committed
182
    return 0
artoo's avatar
artoo committed
183 184
}

185
prepare_dir(){
Bernhard Landauer's avatar
Bernhard Landauer committed
186
    if [[ ! -d $1 ]]; then
187 188
        mkdir -p $1
    fi
189 190
}

191 192
# $1: chroot
get_branch(){
udeved's avatar
udeved committed
193
    echo $(cat "$1/etc/pacman-mirrors.conf" | grep '^Branch = ' | sed 's/Branch = \s*//g')
194 195 196 197 198
}

# $1: chroot
# $2: branch
set_branch(){
udeved's avatar
udeved committed
199 200
    info "Setting mirrorlist branch: %s" "$2"
    sed -e "s|/stable|/$2|g" -i "$1/etc/pacman.d/mirrorlist"
201 202
}

203
init_common(){
udeved's avatar
udeved committed
204
    [[ -z ${target_branch} ]] && target_branch='stable'
205

udeved's avatar
udeved committed
206
    [[ -z ${target_arch} ]] && target_arch=$(uname -m)
207

udeved's avatar
udeved committed
208
    [[ -z ${cache_dir} ]] && cache_dir='/var/cache/manjaro-tools'
209

udeved's avatar
udeved committed
210
    [[ -z ${chroots_dir} ]] && chroots_dir='/var/lib/manjaro-tools'
211

udeved's avatar
udeved committed
212
    [[ -z ${log_dir} ]] && log_dir='/var/log/manjaro-tools'
213

214
    [[ -z ${build_mirror} ]] && build_mirror='https://mirror.netzspielplatz.de/manjaro/packages'
215

udeved's avatar
udeved committed
216
    [[ -z ${tmp_dir} ]] && tmp_dir='/tmp/manjaro-tools'
217
}
218

219
init_buildtree(){
udeved's avatar
udeved committed
220
    tree_dir=${cache_dir}/pkgtree
udeved's avatar
udeved committed
221

udeved's avatar
udeved committed
222
    tree_dir_abs=${tree_dir}/packages-archlinux
udeved's avatar
udeved committed
223

Bernhard Landauer's avatar
Bernhard Landauer committed
224
    [[ -z ${repo_tree[@]} ]] && repo_tree=('core' 'extra' 'community' 'multilib')
225

udeved's avatar
udeved committed
226
    [[ -z ${host_tree} ]] && host_tree='https://github.com/manjaro'
227

udeved's avatar
udeved committed
228
    [[ -z ${host_tree_abs} ]] && host_tree_abs='https://projects.archlinux.org/git/svntogit'
229
}
230

231
init_buildpkg(){
udeved's avatar
udeved committed
232
    chroots_pkg="${chroots_dir}/buildpkg"
233

udeved's avatar
udeved committed
234
    list_dir_pkg="${SYSCONFDIR}/pkg.list.d"
235

udeved's avatar
udeved committed
236
    make_conf_dir="${SYSCONFDIR}/make.conf.d"
237

udeved's avatar
udeved committed
238
    [[ -d ${USERCONFDIR}/pkg.list.d ]] && list_dir_pkg=${USERCONFDIR}/pkg.list.d
239

udeved's avatar
udeved committed
240
    [[ -z ${build_list_pkg} ]] && build_list_pkg='default'
udeved's avatar
udeved committed
241

udeved's avatar
udeved committed
242
    cache_dir_pkg=${cache_dir}/pkg
243
}
244

245
get_iso_label(){
udeved's avatar
udeved committed
246 247 248 249 250 251
    local label="$1"
    label="${label//_}"	# relace all _
    label="${label//-}"	# relace all -
    label="${label^^}"	# all uppercase
    label="${label::8}"	# limit to 8 characters
    echo ${label}
252 253 254
}

get_codename(){
udeved's avatar
udeved committed
255 256
    source /etc/lsb-release
    echo "${DISTRIB_CODENAME}"
257 258
}

259 260 261 262 263 264 265 266 267 268
get_release(){
    source /etc/lsb-release
    echo "${DISTRIB_RELEASE}"
}

get_distname(){
    source /etc/lsb-release
    echo "${DISTRIB_ID%Linux}"
}

269 270 271 272 273
get_distid(){
    source /etc/lsb-release
    echo "${DISTRIB_ID}"
}

274
get_disturl(){
275
    source /usr/lib/os-release
276 277 278 279
    echo "${HOME_URL}"
}

get_osname(){
280
    source /usr/lib/os-release
281 282 283 284
    echo "${NAME}"
}

get_osid(){
285
    source /usr/lib/os-release
286 287 288
    echo "${ID}"
}

289
init_buildiso(){
udeved's avatar
udeved committed
290
    chroots_iso="${chroots_dir}/buildiso"
291

udeved's avatar
udeved committed
292
    list_dir_iso="${SYSCONFDIR}/iso.list.d"
293

udeved's avatar
udeved committed
294
    [[ -d ${USERCONFDIR}/iso.list.d ]] && list_dir_iso=${USERCONFDIR}/iso.list.d
295

udeved's avatar
udeved committed
296
    [[ -z ${build_list_iso} ]] && build_list_iso='default'
297

udeved's avatar
udeved committed
298
    cache_dir_iso="${cache_dir}/iso"
udeved's avatar
udeved committed
299

300 301
    profile_repo='iso-profiles'

udeved's avatar
udeved committed
302
    ##### iso settings #####
303

304
    [[ -z ${dist_release} ]] && dist_release=$(get_release)
artoo's avatar
artoo committed
305

306
    [[ -z ${dist_codename} ]] && dist_codename=$(get_codename)
artoo's avatar
artoo committed
307

308
    dist_name=$(get_distname)
309

310
    iso_name=$(get_osid)
311

udeved's avatar
udeved committed
312 313
    [[ -z ${dist_branding} ]] && dist_branding="MJRO"

314 315
    [[ -z ${iso_compression} ]] && iso_compression='xz'

udeved's avatar
udeved committed
316
    iso_label=$(get_iso_label "${dist_branding}${dist_release//.}")
317

318
    [[ -z ${kernel} ]] && kernel="linux414"
udeved's avatar
udeved committed
319

udeved's avatar
udeved committed
320 321
    [[ -z ${gpgkey} ]] && gpgkey=''

udeved's avatar
udeved committed
322
    mhwd_repo="/opt/pkg"
323 324
}

325 326
init_deployiso(){

327
    host="osdn.net"
328

udeved's avatar
udeved committed
329
    [[ -z ${account} ]] && account="[SetUser]"
330

331 332
    [[ -z ${alt_storage} ]] && alt_storage=false

333 334 335
    [[ -z ${tracker_url} ]] && tracker_url='udp://mirror.strits.dk:6969'

    [[ -z ${piece_size} ]] && piece_size=21
336

337
    torrent_meta="$(get_distid)"
338 339
}

340 341
load_config(){

udeved's avatar
udeved committed
342
    [[ -f $1 ]] || return 1
343

udeved's avatar
udeved committed
344
    manjaro_tools_conf="$1"
345

udeved's avatar
udeved committed
346
    [[ -r ${manjaro_tools_conf} ]] && source ${manjaro_tools_conf}
347

udeved's avatar
udeved committed
348
    init_common
349

udeved's avatar
udeved committed
350
    init_buildtree
351

udeved's avatar
udeved committed
352
    init_buildpkg
353

udeved's avatar
udeved committed
354
    init_buildiso
355

udeved's avatar
udeved committed
356
    init_deployiso
357

udeved's avatar
udeved committed
358
    return 0
359 360 361 362
}

load_profile_config(){

udeved's avatar
udeved committed
363
    [[ -f $1 ]] || return 1
364

udeved's avatar
udeved committed
365
    profile_conf="$1"
366

udeved's avatar
udeved committed
367
    [[ -r ${profile_conf} ]] && source ${profile_conf}
368

udeved's avatar
udeved committed
369
    [[ -z ${displaymanager} ]] && displaymanager="none"
370

udeved's avatar
udeved committed
371 372
    [[ -z ${autologin} ]] && autologin="true"
    [[ ${displaymanager} == 'none' ]] && autologin="false"
373

udeved's avatar
udeved committed
374
    [[ -z ${multilib} ]] && multilib="true"
375

udeved's avatar
udeved committed
376
    [[ -z ${nonfree_mhwd} ]] && nonfree_mhwd="true"
377

udeved's avatar
udeved committed
378
    [[ -z ${efi_boot_loader} ]] && efi_boot_loader="grub"
379

udeved's avatar
udeved committed
380
    [[ -z ${hostname} ]] && hostname="manjaro"
381

udeved's avatar
udeved committed
382
    [[ -z ${username} ]] && username="manjaro"
383

udeved's avatar
udeved committed
384
    [[ -z ${password} ]] && password="manjaro"
385

udeved's avatar
udeved committed
386
    [[ -z ${login_shell} ]] && login_shell='/bin/bash'
387

Bernhard Landauer's avatar
Bernhard Landauer committed
388
    if [[ -z ${addgroups} ]]; then
389
        addgroups="lp,network,power,wheel"
udeved's avatar
udeved committed
390
    fi
udeved's avatar
udeved committed
391

Bernhard Landauer's avatar
Bernhard Landauer committed
392 393
    if [[ -z ${enable_systemd[@]} ]]; then
        enable_systemd=('avahi-daemon' 'bluetooth' 'cronie' 'ModemManager' 'NetworkManager' 'org.cups.cupsd' 'tlp' 'tlp-sleep' 'ufw')
udeved's avatar
udeved committed
394
    fi
395

udeved's avatar
udeved committed
396
    [[ -z ${disable_systemd[@]} ]] && disable_systemd=('pacman-init')
397

Bernhard Landauer's avatar
Bernhard Landauer committed
398
    if [[ -z ${enable_systemd_live[@]} ]]; then
udeved's avatar
udeved committed
399 400
        enable_systemd_live=('manjaro-live' 'mhwd-live' 'pacman-init' 'mirrors-live')
    fi
401

udeved's avatar
udeved committed
402
    if [[ ${displaymanager} != "none" ]]; then
403
        enable_systemd+=("${displaymanager}")
udeved's avatar
udeved committed
404
    fi
405

udeved's avatar
udeved committed
406
    [[ -z ${netinstall} ]] && netinstall='false'
407

408 409
    [[ -z ${mhwd_used} ]] && mhwd_used='true'

410 411
    [[ -z ${oem_used} ]] && oem_used='false'

412
    [[ -z ${chrootcfg} ]] && chrootcfg='false'
413

414
    netgroups="https://raw.githubusercontent.com/manjaro/calamares-netgroups/master"
415

udeved's avatar
udeved committed
416
    [[ -z ${geoip} ]] && geoip='true'
417

udeved's avatar
udeved committed
418
    [[ -z ${smb_workgroup} ]] && smb_workgroup=''
419

420
    [[ -z ${extra} ]] && extra='false'
421 422
    [[ ${full_iso} ]] && extra='true'

423
    basic='true'
424
    [[ ${extra} == 'true' ]] && basic='false'
udeved's avatar
udeved committed
425

udeved's avatar
udeved committed
426
    return 0
427
}
428

udeved's avatar
udeved committed
429 430 431 432 433 434 435
get_edition(){
    local result=$(find ${run_dir} -maxdepth 2 -name "$1") path
    [[ -z $result ]] && die "%s is not a valid profile or build list!" "$1"
    path=${result%/*}
    echo ${path##*/}
}

436 437 438 439 440 441 442 443 444 445 446 447
get_project(){
    case "${edition}" in
        'manjaro')
            project="manjaro"
        ;;
        'community')
            project="manjaro-community"
        ;;
    esac
    echo "${project}"
}

448 449 450 451 452 453 454 455 456 457 458 459 460
reset_profile(){
    unset displaymanager
    unset autologin
    unset multilib
    unset nonfree_mhwd
    unset efi_boot_loader
    unset hostname
    unset username
    unset password
    unset addgroups
    unset enable_systemd
    unset disable_systemd
    unset enable_systemd_live
461
    unset packages_desktop
462 463 464
    unset packages_mhwd
    unset login_shell
    unset netinstall
465
    unset chrootcfg
466
    unset geoip
467
    unset extra
468
    unset full_iso
469 470 471
}

check_profile(){
udeved's avatar
udeved committed
472 473
    local keyfiles=("$1/Packages-Root"
            "$1/Packages-Live")
474

udeved's avatar
udeved committed
475 476
    local keydirs=("$1/root-overlay"
            "$1/live-overlay")
477 478 479

    local has_keyfiles=false has_keydirs=false
    for f in ${keyfiles[@]}; do
Bernhard Landauer's avatar
Bernhard Landauer committed
480
        if [[ -f $f ]]; then
481 482 483 484 485 486 487
            has_keyfiles=true
        else
            has_keyfiles=false
            break
        fi
    done
    for d in ${keydirs[@]}; do
Bernhard Landauer's avatar
Bernhard Landauer committed
488
        if [[ -d $d ]]; then
489 490 491 492 493 494
            has_keydirs=true
        else
            has_keydirs=false
            break
        fi
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
495
    if ! ${has_keyfiles} && ! ${has_keydirs}; then
udeved's avatar
udeved committed
496
        die "Profile [%s] sanity check failed!" "$1"
497 498
    fi

udeved's avatar
udeved committed
499
    [[ -f "$1/Packages-Desktop" ]] && packages_desktop=$1/Packages-Desktop
500

udeved's avatar
udeved committed
501
    [[ -f "$1/Packages-Mhwd" ]] && packages_mhwd=$1/Packages-Mhwd
502

503 504
    if ! ${netinstall}; then
        chrootcfg="false"
505 506 507 508 509 510 511
    fi
}

# $1: file name
load_pkgs(){
    info "Loading Packages: [%s] ..." "${1##*/}"

512 513
    local _multi _nonfree_default _nonfree_multi _arch _arch_rm _nonfree_i686 _nonfree_x86_64 _basic _basic_rm _extra _extra_rm

Bernhard Landauer's avatar
Bernhard Landauer committed
514
    if ${basic}; then
515 516 517 518
        _basic="s|>basic||g"
    else
        _basic_rm="s|>basic.*||g"
    fi
519

Bernhard Landauer's avatar
Bernhard Landauer committed
520
    if ${extra}; then
521 522 523 524 525
        _extra="s|>extra||g"
    else
        _extra_rm="s|>extra.*||g"
    fi

526 527 528 529 530 531 532
    case "${target_arch}" in
        "i686")
            _arch="s|>i686||g"
            _arch_rm="s|>x86_64.*||g"
            _multi="s|>multilib.*||g"
            _nonfree_multi="s|>nonfree_multilib.*||g"
            _nonfree_x86_64="s|>nonfree_x86_64.*||g"
Bernhard Landauer's avatar
Bernhard Landauer committed
533
            if ${nonfree_mhwd}; then
534
                _nonfree_default="s|>nonfree_default||g"
535 536
                _nonfree_i686="s|>nonfree_i686||g"

537 538
            else
                _nonfree_default="s|>nonfree_default.*||g"
539
                _nonfree_i686="s|>nonfree_i686.*||g"
540
            fi
541 542 543 544 545
        ;;
        *)
            _arch="s|>x86_64||g"
            _arch_rm="s|>i686.*||g"
            _nonfree_i686="s|>nonfree_i686.*||g"
Bernhard Landauer's avatar
Bernhard Landauer committed
546
            if ${multilib}; then
547
                _multi="s|>multilib||g"
Bernhard Landauer's avatar
Bernhard Landauer committed
548
                if ${nonfree_mhwd}; then
549 550 551 552 553 554 555 556
                    _nonfree_default="s|>nonfree_default||g"
                    _nonfree_x86_64="s|>nonfree_x86_64||g"
                    _nonfree_multi="s|>nonfree_multilib||g"
                else
                    _nonfree_default="s|>nonfree_default.*||g"
                    _nonfree_multi="s|>nonfree_multilib.*||g"
                    _nonfree_x86_64="s|>nonfree_x86_64.*||g"
                fi
557
            else
558
                _multi="s|>multilib.*||g"
Bernhard Landauer's avatar
Bernhard Landauer committed
559
                if ${nonfree_mhwd}; then
560 561 562 563 564 565 566 567
                    _nonfree_default="s|>nonfree_default||g"
                    _nonfree_x86_64="s|>nonfree_x86_64||g"
                    _nonfree_multi="s|>nonfree_multilib.*||g"
                else
                    _nonfree_default="s|>nonfree_default.*||g"
                    _nonfree_x86_64="s|>nonfree_x86_64.*||g"
                    _nonfree_multi="s|>nonfree_multilib.*||g"
                fi
568
            fi
569 570
        ;;
    esac
571

572 573
    local _edition _edition_rm
    case "${edition}" in
574
        'sonar')
575 576 577
            _edition="s|>sonar||g"
            _edition_rm="s|>manjaro.*||g"
        ;;
578
        *)
579 580 581 582
            _edition="s|>manjaro||g"
            _edition_rm="s|>sonar.*||g"
        ;;
    esac
583

584 585 586 587 588 589 590 591 592
    local _blacklist="s|>blacklist.*||g" \
        _kernel="s|KERNEL|$kernel|g" \
        _used_kernel=${kernel:5:2} \
        _space="s| ||g" \
        _clean=':a;N;$!ba;s/\n/ /g' \
        _com_rm="s|#.*||g" \
        _purge="s|>cleanup.*||g" \
        _purge_rm="s|>cleanup||g"

593
    packages=$(sed "$_com_rm" "$1" \
594 595 596 597 598 599 600 601 602 603 604
            | sed "$_space" \
            | sed "$_blacklist" \
            | sed "$_purge" \
            | sed "$_arch" \
            | sed "$_arch_rm" \
            | sed "$_nonfree_default" \
            | sed "$_multi" \
            | sed "$_nonfree_i686" \
            | sed "$_nonfree_x86_64" \
            | sed "$_nonfree_multi" \
            | sed "$_kernel" \
605 606
            | sed "$_edition" \
            | sed "$_edition_rm" \
607 608
            | sed "$_basic" \
            | sed "$_basic_rm" \
609 610
            | sed "$_extra" \
            | sed "$_extra_rm" \
611 612 613 614
            | sed "$_clean")

    if [[ $1 == "${packages_mhwd}" ]]; then

615
        [[ ${_used_kernel} < "42" ]] && local _amd="s|xf86-video-amdgpu||g"
616 617 618 619 620 621 622 623 624 625

        packages_cleanup=$(sed "$_com_rm" "$1" \
            | grep cleanup \
            | sed "$_purge_rm" \
            | sed "$_kernel" \
            | sed "$_clean" \
            | sed "$_amd")
    fi
}

udeved's avatar
udeved committed
626
user_own(){
udeved's avatar
udeved committed
627 628
    local flag=$2
    chown ${flag} "${OWNER}:$(id --group ${OWNER})" "$1"
udeved's avatar
udeved committed
629 630
}

udeved's avatar
udeved committed
631
clean_dir(){
udeved's avatar
udeved committed
632 633 634 635
    if [[ -d $1 ]]; then
        msg "Cleaning [%s] ..." "$1"
        rm -r $1/*
    fi
udeved's avatar
udeved committed
636 637
}

638
write_repo_conf(){
639
    local repos=$(find $USER_HOME -type f -name "repo_info")
udeved's avatar
udeved committed
640 641 642
    local path name
    [[ -z ${repos[@]} ]] && run_dir=${DATADIR}/iso-profiles && return 1
    for r in ${repos[@]}; do
643
        path=${r%/repo_info}
udeved's avatar
udeved committed
644 645 646
        name=${path##*/}
        echo "run_dir=$path" > ${USERCONFDIR}/$name.conf
    done
647 648
}

udeved's avatar
udeved committed
649
load_user_info(){
udeved's avatar
udeved committed
650
    OWNER=${SUDO_USER:-$USER}
udeved's avatar
udeved committed
651

udeved's avatar
udeved committed
652 653 654 655 656
    if [[ -n $SUDO_USER ]]; then
        eval "USER_HOME=~$SUDO_USER"
    else
        USER_HOME=$HOME
    fi
657

udeved's avatar
udeved committed
658 659
    USERCONFDIR="$USER_HOME/.config/manjaro-tools"
    prepare_dir "${USERCONFDIR}"
udeved's avatar
udeved committed
660
}
661

662
load_run_dir(){
udeved's avatar
udeved committed
663 664 665
    [[ -f ${USERCONFDIR}/$1.conf ]] || write_repo_conf
    [[ -r ${USERCONFDIR}/$1.conf ]] && source ${USERCONFDIR}/$1.conf
    return 0
666 667
}

udeved's avatar
udeved committed
668
show_version(){
udeved's avatar
udeved committed
669 670
    msg "manjaro-tools"
    msg2 "version: %s" "${version}"
udeved's avatar
udeved committed
671 672 673
}

show_config(){
udeved's avatar
udeved committed
674
    if [[ -f ${USERCONFDIR}/manjaro-tools.conf ]]; then
675
        msg2 "config: %s" "~/.config/manjaro-tools/manjaro-tools.conf"
udeved's avatar
udeved committed
676
    else
677
        msg2 "config: %s" "${manjaro_tools_conf}"
udeved's avatar
udeved committed
678
    fi
udeved's avatar
udeved committed
679
}
680 681

# $1: chroot
682
kill_chroot_process(){
udeved's avatar
udeved committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
    # enable to have more debug info
    #msg "machine-id (etc): $(cat $1/etc/machine-id)"
    #[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)"
    #msg "running processes: "
    #lsof | grep $1

    local prefix="$1" flink pid name
    for root_dir in /proc/*/root; do
        flink=$(readlink $root_dir)
        if [ "x$flink" != "x" ]; then
            if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then
                # this process is in the chroot...
                pid=$(basename $(dirname "$root_dir"))
                name=$(ps -p $pid -o comm=)
                info "Killing chroot process: %s (%s)" "$name" "$pid"
                kill -9 "$pid"
            fi
        fi
    done
702
}
udeved's avatar
udeved committed
703

udeved's avatar
udeved committed
704
create_min_fs(){
udeved's avatar
udeved committed
705 706 707 708
    msg "Creating install root at %s" "$1"
    mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc}
    mkdir -m 1777 -p $1/tmp
    mkdir -m 0555 -p $1/{sys,proc}
udeved's avatar
udeved committed
709
}
udeved's avatar
udeved committed
710

udeved's avatar
udeved committed
711
is_valid_arch_pkg(){
udeved's avatar
udeved committed
712 713 714 715
    eval "case $1 in
        $(show_build_profiles "${make_conf_dir}")) return 0 ;;
        *) return 1 ;;
    esac"
udeved's avatar
udeved committed
716 717 718
}

is_valid_arch_iso(){
udeved's avatar
udeved committed
719 720 721 722
    case $1 in
        'i686'|'x86_64') return 0 ;;
        *) return 1 ;;
    esac
udeved's avatar
udeved committed
723
}
724 725

is_valid_branch(){
udeved's avatar
udeved committed
726 727 728 729
    case $1 in
        'stable'|'testing'|'unstable') return 0 ;;
        *) return 1 ;;
    esac
730 731 732
}

run(){
Bernhard Landauer's avatar
Bernhard Landauer committed
733
    if ${is_build_list}; then
Bernhard Landauer's avatar
Bernhard Landauer committed
734
        for item in ${build_list[@]}; do
udeved's avatar
udeved committed
735 736 737 738 739
            $1 $item
        done
    else
        $1 $2
    fi
740
}
artoo's avatar
artoo committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

is_btrfs() {
    [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
}

subvolume_delete_recursive() {
    local subvol

    is_btrfs "$1" || return 0

    while IFS= read -d $'\0' -r subvol; do
        if ! btrfs subvolume delete "$subvol" &>/dev/null; then
            error "Unable to delete subvolume %s" "$subvol"
            return 1
        fi
    done < <(find "$1" -xdev -depth -inum 256 -print0)

    return 0
}
Bernhard Landauer's avatar
Bernhard Landauer committed
760 761 762 763 764 765

create_chksums() {
    msg2 "creating checksums for [$1]"
    sha1sum $1 > $1.sha1
    sha256sum $1 > $1.sha256
}