Edit 20/05/2009: Realised I didnt attach a licence to this little project, so let it be the lastest version of the GPL, which at present is v3. 🙂
Before I start this, it should be known I am relatively new to shell scripts, and while this may not be the best method by any means, it does work.
Everything needed is available here as a tarball, it should be placed in /etc/apt/ and unpacked using ‘tar -xvf source-switcher.tar’, running source-swapper.sh will get you set up. You will have to configure your sources files though!
My problem was this. I have a local repo on a machine in my house, which makes updating and installing a lot quicker that it used to be, let alone being fun to set up. My laptop however, is not always in the house for obvious reasons, and even when it is, it’s not always connected to the network.
For this, I wanted a way of switching between sources.list files with as little user intervention as possible.
I already have a folder called ‘sources.safe’ which contains a different list for each location, and this is a listing so far:
root@ben-laptop:/etc/apt/sources.safe# ls -1
sources.CD-Rom
sources.Home
sources.Official
So I wrote this script which checks to see what repo is available, and copies the right sources.list and overwriting the one that is already there, and then notifies user 1000 (perfect for single user systems).
#!/bin/sh
apply()
{
export DISPLAY=:0.0
sudo -u \#1000 notify-send -u normal -t 10000 -i /etc/apt/apt.png "APT Source: $1" 'Your sources list has been updated for this network!'
cp "/etc/apt/sources.safe/sources.$1" '/etc/apt/sources.list'
}
if wget -t 1 -T 1 --spider http://192.168.0.200/; then
apply 'Home'
elif wget -t 1 -T 1 --spider http://archive.ubuntu.com/ubuntu/; then
apply 'Official'
else
apply 'CD-Rom'
fi
As you can see, the apply lines use the same extension as the sources files in the sources.safe folder. More scripts can be added by more else statements. This also allows prioritisation. Note that if all else fails it uses the CD-Rom sources list, this should allow me to have something to install from available at all times.
Also in my /etc/apt folder I created this script, which sets up the links in /etc/network/if-up.d and /etc/network/if-down.d so that the script runs when you ‘change location’.
#!/bin/sh
echo 'Setting up links...'
link /etc/apt/source-swapper.sh /etc/network/if-up.d/source-swapper
link /etc/apt/source-swapper.sh /etc/network/if-down.d/source-swapper
echo 'making main script executable...'
chmod +x /etc/apt/source-swapper.sh
echo 'Installing dependencies...'
apt-get -y install libnotify-bin
echo 'should be done :-)'
A few things that should probably be noted. First is that the setup script will need to be set executable. If you decide not to use the setup script, make sure you manually apply its changes. The only other thing that have is the ‘package’ icon. That should be places in the apt folder as /etc/apt/apt.png, if you decide to place it elsewhere or which to use another image, you will need to adapt the script accordingly.
Here is the package icon. I got it from here: http://files.opensuse.org/opensuse/en/thumb/3/39/Icon-package.png/120px-Icon-package.png