summaryrefslogtreecommitdiff
path: root/src/xi.sh
blob: 923612ccf00e4e287392a1876d9c5a3587c85f19 (plain)
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
#!/bin/sh

usage () {
cat << EOF
${LIGHT_WHITE}XiPkg Version $VERSION

${BLUE}Available Options:
    ${BLUE}-r ${LIGHT_BLUE}[path]
        ${LIGHT_CYAN}specify the installation root ${LIGHT_WHITE}[default: /]
    ${BLUE}-c [path]
        ${LIGHT_CYAN}specify the config file to use ${LIGHT_WHITE}[default: /etc/xipkg.conf]
    ${BLUE}-q
        ${LIGHT_CYAN}supress unecessary outputs
    ${BLUE}-v
        ${LIGHT_CYAN}be more verbose
    ${BLUE}-n
        ${LIGHT_CYAN}do not resolve package dependenceies
    ${BLUE}-l
        ${LIGHT_CYAN}do not sync databases (ignored when explicitly running sync)
    ${BLUE}-u
        ${LIGHT_CYAN}do not validate against keychain
    ${BLUE}-y
        ${LIGHT_CYAN}skip prompts
    ${BLUE}-h 
        ${LIGHT_CYAN}show help and exists

${BLUE}Available Commands:
    ${LIGHT_GREEN}sync
        ${LIGHT_CYAN}sync the local package database with the remote repositories

    ${LIGHT_GREEN}install ${LIGHT_BLUE}[packages..]
        ${LIGHT_CYAN}install package(s) into the system
    ${LIGHT_GREEN}update
        ${LIGHT_CYAN}update all packages on the system
    ${LIGHT_GREEN}remove ${LIGHT_BLUE}[packages...]
        ${LIGHT_CYAN}remove packages from the system
    ${LIGHT_GREEN}reinstall ${LIGHT_BLUE}[packages..]
        ${LIGHT_CYAN}remove and reinstall package(s) into the system
    ${LIGHT_GREEN}fetch ${LIGHT_BLUE}[package]
        ${LIGHT_CYAN}download a .xipkg file
    ${LIGHT_GREEN}keyimport ${LIGHT_BLUE}[name] [url]
        ${LIGHT_CYAN}import a key from a url
    ${LIGHT_GREEN}clean
        ${LIGHT_CYAN}clean cached files and data
    ${LIGHT_GREEN}build
        ${LIGHT_CYAN}build a package from source

    ${LIGHT_GREEN}search ${LIGHT_BLUE}[query]
        ${LIGHT_CYAN}search the database for a package
    ${LIGHT_GREEN}files ${LIGHT_BLUE}[package]
        ${LIGHT_CYAN}list files belonging to a package
    ${LIGHT_GREEN}verify ${LIGHT_BLUE}[package]
        ${LIGHT_CYAN}verify that a package's files are intact
    ${LIGHT_GREEN}list
        ${LIGHT_CYAN}list available packages
    ${LIGHT_GREEN}installed
        ${LIGHT_CYAN}lists installed packages
    ${LIGHT_GREEN}list-installed
        ${LIGHT_CYAN}list packages showing the installed ones
    ${LIGHT_GREEN}file ${LIGHT_BLUE}[path]
        ${LIGHT_CYAN}shows which package a file belongs to
    ${LIGHT_GREEN}info ${LIGHT_BLUE}[package]
        ${LIGHT_CYAN}show info about an installed package

    ${LIGHT_GREEN}bootstrap ${LIGHT_BLUE}[additional packages...]
        ${LIGHT_CYAN}installs base packages and system files to an empty system

    ${LIGHT_GREEN}help
        ${LIGHT_CYAN}shows this message${RESET}

${RED}Usage: xi [options] command...
EOF
}


[ -z "${LIBDIR}" ] && LIBDIR=/usr/lib/xipkg
[ -f "/usr/lib/xilib.sh" ] && . /usr/lib/xilib.sh

[ -f ${LIBDIR}/VERSION ] && VERSION=$(cat ${LIBDIR}/VERSION) || VERSION=
export VERSION

export SYSROOT=/
export CONF_FILE="/etc/xipkg.conf"
export VERBOSE=false
export QUIET=false
export RESOLVE_DEPS=true
export DO_SYNC=true
export UNSAFE=false
export NOCONFIRM=false

while getopts ":r:c:qnluyvh" opt; do
    case "${opt}" in
        r)
            SYSROOT=$(realpath ${OPTARG})
            ;;
        c)
            CONF_FILE="${OPTARG}"
            ;;
        n)
            RESOLVE_DEPS=false
            ;;
        l)
            DO_SYNC=false
            ;;
        u)
            UNSAFE=true
            ;;
        y)
            NOCONFIRM=true
            ;;
        v)
            VERBOSE=true
            ;;
        q)
            QUIET=true
            ;;
        h)
            usage
            exit 0
            ;;
    esac
done

# TODO only load these modules when needed
. ${LIBDIR}/profile.sh
. ${LIBDIR}/util.sh
. ${LIBDIR}/validate.sh

. ${LIBDIR}/query.sh
. ${LIBDIR}/sync.sh
. ${LIBDIR}/install.sh
. ${LIBDIR}/build.sh
. ${LIBDIR}/get.sh
. ${LIBDIR}/remove.sh

do_install () {
    [ "$#" = "0" ] && set -- $(installed)

    toinstall=${CACHE_DIR}/toinstall

    echo "" > $toinstall
    tofetch=""
    for f in $@; do
        [ -f "$f" ] && echo $f >> $toinstall || tofetch="$tofetch$f "
    done

    get $tofetch
    install $(cat $toinstall)
}

shift $((OPTIND-1))

if [ "$#" = "0" ]; then
    . ${LIBDIR}/stats.sh
    show_xipkg_stats
else 
    case "$1" in
        "sync")
            checkroot
            sync
            ;;
        "install" | "update")
            shift
            checkroot
            do_install $@
            ;;
        "build")
            shift
            checkroot

            [ "$#" = "0" ] && set -- $(installed)

            build $@
            ;;
        "search")
            shift
            search $@
            ;;
        "fetch")
            shift
            checkroot
            $DO_SYNC && sync
            fetch $@
            ;;
        "remove")
            shift
            checkroot
            remove $@
            ;;
        "reinstall")
            shift
            checkroot
            remove $@
            do_install $@
            ;;
        "files")
            shift
            files $@
            ;;
        "keyimport")
            shift
            checkroot
            set -o noglob
            keyimport $@
            ;;
        "clean")
            shift
            checkroot
            . ${LIBDIR}/remove.sh
            clean $@
            ;;
        "list")
            list
            ;;
        "list-installed")
            list_installed
            ;;
        "installed")
            installed
            ;;
        "file")
            shift
            file_info $@
            ;;
        "info")
            shift
            info $@
            ;;
        "verify")
            shift
            [ -z "$*" ] && set -- $(ls ${INSTALLED_DIR})
            while [ ! -z "$*" ]; do
                validate_files $1 || {
                   ${QUIET} && printf "%s\n" $1 || printf "${LIGHT_RED}Failed to verify $1\n"
                    
                }
                shift
            done
            ;;
        "bootstrap")
            shift
            checkroot
            . ${LIBDIR}/bootstrap.sh
            bootstrap $@
            ;;
        "help")
            usage
            ;;
        *)

            sudo $0 ${DEFAULT_OPTION:-install} $@
            ;;
    esac
fi

${QUIET} || printf "${RESET}"