1
0
Fork 0
update-mirrors/update-mirrors
Tom Willemse 6093fddfff Fall back to reading config from XDG_DATA_DIRS
If there is no configuration file in “$XDG_CONFIG_HOME/update-mirrors”,
try looking through directories specified in XDG_CONFIG_DIRS. If
XDG_CONFIG_DIRS is not specified, use “/etc/xdg”.

Now config files are searched for in this order:

- “$XDG_CONFIG_HOME/update-mirrors/config.sh”, or
  “$HOME/.config/update-mirrors/config.sh” if XDG_CONFIG_DIR is not
  defined.
- For each DIR in XDG_CONFIG_DIRS “$DIR/update-mirrors/config.sh” is
  tried, or “/etc/xdg/update-mirrors/config.sh” if XDG_CONFIG_DIRS is
  not defined.
2016-06-12 15:05:13 +02:00

81 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
# Changeable options
url="https://www.archlinux.org/mirrorlist"
countries=("BE" "NL")
protocols=("http" "https")
ip_versions=(4)
use_mirror_status="on"
dest="/etc/pacman.d/mirrorlist"
# 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=$(urlify country ${countries[@]})
local protocolparams=$(urlify protocol ${protocols[@]})
local ipparams=$(urlify ip_version ${ip_versions[@]})
local 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 $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
curl -s "$(make-url)" | sed 's/^#\([^#].*\)/\1/' > "$dest"
echo "Saved in ${dest}"
if [[ -f "${dest}.pacnew" ]]; then
rm "${dest}.pacnew"
echo "Removed ${dest}.pacnew"
fi