#! /usr/bin/python

import os
import random
import argparse

def choose_background(path):
    files = []
    for root, dirs, temp_files in os.walk(path, followlinks=True):
        files.extend([os.sep.join((root, f)) for f in temp_files
                      if f[-3:].lower() in ('jpg', 'png', 'jpeg')])
    if len(files) > 0:
        choice = random.randint(0,len(files)-1)
        wall = os.path.join(os.path.abspath(path),files[choice])
        os.system('gconftool-2 --set /desktop/gnome/background/picture_filename --type string "%s"' % wall)
        return wall

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="""Choose an arbitrary file to set on the background""")

    parser.add_argument(dest='path', help='path you want to choose the image from')
    args = parser.parse_args()
    print 'selected %s' % choose_background(args.path)
