worst (best?) bash script I've ever written