aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/os.sh
blob: 640c693001804ee9f4871fa92299c755fcf0d1ef (plain) (blame)
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
#!/usr/bin/env bash

_os=''

detect_os() {
    command -v uname > /dev/null           \
        && [ "$( uname -o )" == 'Cygwin' ] \
        && _os='Cygwin'                    \
        && return 0

    [ -r /etc/os-release ]                              \
        && _os="$( . /etc/os-release && echo "$NAME" )" \
        && return 0

    return 1
}

detect_os

os_detected() {
    test -n "$_os"
}

is_cygwin() {
    test "$_os" == 'Cygwin'
}

is_ubuntu() {
    test "$_os" == 'Ubuntu'
}

is_arch() {
    test "$_os" == 'Arch Linux'
}

list_packages_cygwin() (
    set -o errexit -o nounset -o pipefail

    cygcheck --check-setup --dump-only \
        | tail -n +3                   \
        | cut -d ' ' -f 1
)

list_initial_packages_ubuntu() (
    set -o errexit -o nounset -o pipefail

    local -r initial_status='/var/log/installer/initial-status.gz'

    gzip --decompress --stdout -- "$initial_status" \
        | sed --quiet -- 's/^Package: //p' \
        | sort \
        | uniq
)

list_manually_installed_packages_ubuntu() (
    set -o errexit -o nounset -o pipefail

    comm -23 <( apt-mark showmanual | sort | uniq ) \
             <( list_initial_packages_ubuntu )
)

list_packages_ubuntu() (
    set -o errexit -o nounset -o pipefail

    dpkg --get-selections                     \
        | grep --invert-match -- 'deinstall$' \
        | cut -f 1                            \
        | cut -d ':' -f 1
)

list_initial_packages_arch() (
    set -o errexit -o nounset -o pipefail

    local -ra groups=(base base-devel)

    pacman -Q --groups -q -- ${groups[@]+"${groups[@]}"} | sort
)

list_manually_installed_packages_arch() (
    set -o errexit -o nounset -o pipefail

    comm -23 <( pacman -Q --explicit -q | sort ) \
             <( list_initial_packages_arch )
)

list_packages_arch() {
    pacman -Qq
}

list_packages() (
    set -o errexit -o nounset -o pipefail

    if is_cygwin; then
        list_packages_cygwin
    elif is_ubuntu; then
        list_packages_ubuntu
    elif is_arch; then
        list_packages_arch
    else
        return 1
    fi
)