aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/os.sh
blob: 6655ab1f8144cdae5b7aa37b5ce93c240feebf4d (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
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
#!/usr/bin/env bash

# Copyright (c) 2016 Egor Tensin <egor@tensin.name>
# This file is part of the "linux-home" project.
# For details, see https://github.com/egor-tensin/linux-home.
# Distributed under the MIT License.

_os=''

_CYGWIN='Cygwin'
_MACOS='macOS'
_UBUNTU='Ubuntu'
_DEBIAN='Debian GNU/Linux'
_MINT='Linux Mint'
_ARCH='Arch Linux'
_ARCH_ARM='Arch Linux ARM'
_FEDORA='Fedora'

os_detect() {
    case "$OSTYPE" in
        cygwin)
            _os="$_CYGWIN"
            return 0
            ;;
        darwin*)
            _os="$_MACOS"
            return 0
            ;;
        linux-gnu*)
            if [ -r /etc/os-release ] && _os="$( . /etc/os-release && echo "$NAME" )"; then
                case "$_os" in
                    "$_UBUNTU"|"$_DEBIAN"|"$_MINT"|"$_ARCH"|"$_ARCH_ARM"|"$_FEDORA")
                        return 0
                        ;;
                    *)
                        _os=''
                        return 1
                        ;;
                esac
            fi
            ;;
        *)
            return 1
            ;;
    esac
}

os_detect

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

os_is_cygwin() { test "$_os" == "$_CYGWIN" ; }
os_is_macos()  { test "$_os" == "$_MACOS"  ; }

os_is_ubuntu() { test "$_os" == "$_UBUNTU" ; }
os_is_debian() { test "$_os" == "$_DEBIAN" ; }
os_is_mint()   { test "$_os" == "$_MINT"   ; }
os_is_arch()   { test "$_os" == "$_ARCH" -o "$_os" == "$_ARCH_ARM" ; }
os_is_fedora() { test "$_os" == "$_FEDORA" ; }

os_is_ubuntu_like() { os_is_ubuntu || os_is_debian || os_is_mint ; }
os_is_linux() { os_is_ubuntu_like || os_is_arch || os_is_fedora ; }

# Cygwin

pkg_list_cygwin() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

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

# Ubuntu-likes

setup_pkg_list_ubuntu() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

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

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

user_pkg_list_ubuntu() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    local manual
    manual="$( apt-mark showmanual | sort | uniq )"

    local setup
    setup="$( setup_pkg_list_ubuntu )"

    comm -23 <( echo "$manual" ) <( echo "$setup" )
)

pkg_list_ubuntu() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

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

# Arch Linux

setup_pkg_list_arch() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    # Assuming you only selected groups 'base and 'base-devel' during
    # installation.
    local -ra groups=(base base-devel)

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

user_pkg_list_arch() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    local manual
    manual="$( pacman -Q --explicit -q | sort )"

    local setup
    setup="$( setup_pkg_list_arch )"

    comm -23 <( echo "$manual" ) <( echo "$setup" )
)

manual_pkg_list_arch() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    expac '%n %p' | grep 'Unknown Packager'
)

pkg_list_arch() {
    pacman -Qq
}

# Fedora

pkg_list_fedora() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    rpm --queryformat="%{NAME}\n" -qa | sort
)

# Generic routines

pkg_list() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    if os_is_cygwin; then
        pkg_list_cygwin
    elif os_is_ubuntu_like; then
        pkg_list_ubuntu
    elif os_is_arch; then
        pkg_list_arch
    elif os_is_fedora; then
        pkg_list_fedora
    else
        echo "${FUNCNAME[0]}: unsupported OS" >&2
        return 1
    fi
)

user_pkg_list() (
    set -o errexit -o nounset -o pipefail
    shopt -s inherit_errexit 2> /dev/null || true

    if os_is_ubuntu_like; then
        user_pkg_list_ubuntu
    elif os_is_arch; then
        user_pkg_list_arch
    else
        echo "${FUNCNAME[0]}: unsupported OS" >&2
        return 1
    fi
)