aboutsummaryrefslogtreecommitdiffstats
path: root/update-mirrors
blob: 0cfd0468b13186909de52a79df6f72cb1a6c2fe3 (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
#!/bin/bash

# Functions
function fold() { /usr/bin/fold --spaces --width="$(tput cols)"; }

function _urlify-1() {
    local items=($@)
    for item in "${items[@]:1}"; do
        echo "$1=${item}"
    done
}

function urlify() {
    local uitems=($(_urlify-1 "$@"))
    local IFS="&"
    echo "${uitems[*]}"
}

function make-url() {
    local countryparams
    local protocolparams
    local ipparams
    local statusparams

    countryparams=$(urlify country "${countries[@]}")
    protocolparams=$(urlify protocol "${protocols[@]}")
    ipparams=$(urlify ip_version "${ip_versions[@]}")
    statusparams=$(urlify use_mirror_status "$use_mirror_status")

    echo "${url}/?${countryparams}&${protocolparams}&${ipparams}&${statusparams}"
}

function load-global-config() {
    local IFS=":"

    for dir in $1; do
        local cfgfile="${dir}${2}"
        if [[ -x $cfgfile ]]; then
            source "$cfgfile"
            return
        fi
    done
}

# Load config file
etcbases=${XDG_CONFIG_DIRS:-"/etc/xdg"}
cfgbase=${XDG_CONFIG_HOME:-"${HOME}/.config"}
cfgfile="/update-mirrors/config.sh"

if [[ -x "${cfgbase}${cfgfile}" ]]; then
    source "${cfgbase}${cfgfile}"
else
    load-global-config "$etcbases" $cfgfile
fi

# Check destination
if [[ ! -e $dest ]] && [[ ! -w "$(dirname "$dest")" ]]; then
    echo "Need write permission for $(dirname "$dest") to create \
${dest}. Perhaps you should use sudo." | fold >&2
    exit 1
elif [[ -e $dest ]] && [[ ! -w $dest ]]; then
    echo "Need write permission for ${dest}, perhaps you should use \
sudo." | fold >&2
    exit 1
fi

# Download, ready, output
mirrorlist=$(curl --no-progress-meter --location "$(make-url)")

if [[ $? -eq 0 ]]; then
    echo "$mirrorlist" | sed 's/^#\([^#].*\)/\1/' > "$dest"
    echo "Saved in ${dest}"
else
    echo "Couldn't load mirrorlist, not updating."
fi

if [[ -f "${dest}.pacnew" ]]; then
    rm "${dest}.pacnew"
    echo "Removed ${dest}.pacnew"
fi