+# netselect-apt finds a fast mirror.
+# but we need to replace the mirrors ourselves,
+# because it doesn't do that. best it can do is
+# output a basic sources file
+# here we get the server it found, get the main server we use
+# then substitute all instances of one for the other in the sources file
+# and backup original to /etc/apt/sources.list-original.
+# this is idempotent
+debian_pick_mirror () {
+ local x=$(mktemp -d)/f # safe way to get file name without creating one
+ sudo netselect-apt -o $x || return 1
+ x=$(_debian_pick_mirror_grep stable $x)
+ sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
+ sudo sed -i "s/$(_debian_pick_mirror_grep wheezy)/$x/" /etc/apt/sources.list
+}
+
+_debian_pick_mirror_grep () {
+ local x="$(grep -oP "^deb [^ ]+ $1 " ${2-/etc/apt/sources.list})"
+ x="${x#deb }"
+ x="${x% $1 }"
+ # replace / with \/ so we can use it with sed
+ echo "${x//\//\\/}"
+}