Created
October 4, 2017 22:59
-
-
Save boltronics/c2d61055ce9d7546144970f8dba9b10f to your computer and use it in GitHub Desktop.
apt-changer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Copyright (c) 2017 Adam Bolte | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# Script to update /etc/apt/sources.list.d symlink | |
# | |
# Points the symlink to a configuration relevant to the local network. | |
# Also does the same for any chroot environments defined within | |
# configuration files under /etc/schroot/chroot.d/. | |
# | |
# Will almost certainly break if chroot root directories contain a space. | |
declare -r default_config="ftp.au.debian.org" | |
declare -r ssid="$(iw dev wlp1s0 link | grep 'SSID:' | \ | |
sed 's/.*SSID:\ \(.*\)/\1/')" | |
get_fs_roots() { | |
if [ -d /etc/schroot/chroot.d ] | |
then | |
for config in /etc/schroot/chroot.d/* | |
do | |
grep -E '^directory=' "${config}" | cut -d '=' -f 2 | |
done | |
fi | |
echo / | |
} | |
apt_changer() { | |
for root in $(get_fs_roots) | |
do | |
if [ -h ${root}/etc/apt/sources.list.d -a \ | |
-d "${root}/etc/apt/sources.list.d.${1}" ] | |
then | |
rm -f "${root}/etc/apt/sources.list.d" && | |
ln -s "sources.list.d.${1}" "${root}/etc/apt/sources.list.d" | |
elif [ ! -h "${root}/etc/apt/sources.list.d" ] | |
then | |
{ | |
echo "${root}/etc/apt/sources.list.d is not a symbolic link." | |
echo "Not deleting" | |
} 1>&2 | |
exit 1 | |
elif [ ! -d "${root}/etc/apt/sources.list.d.${1}" ] | |
then | |
{ | |
echo "${root}/etc/apt/sources.list.d.${1} is not a valid " | |
echo "directory." | |
} 1>&2 | |
exit 1 | |
fi | |
done | |
} | |
if [ -n "${ssid}" ] | |
then | |
apt_changer "${ssid}" | |
else | |
apt_changer ${default_config} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment