#!/usr/bin/env python

import findrox
import os, sys
import os.path

from rox import filer, choices, support
from rox.MultipleChoice import MultipleChoice

if len(sys.argv) > 1:
	box = MultipleChoice("Show this image on the desktop background?",
		('Tiled', 'Centred', 'Scaled', 'Cancel'))
	box.set_title('Wallpaper')
	ans = box.wait()
	import display
	file = sys.argv[1]
	if ans == 0:
		display.tile(file)
	elif ans == 1:
		display.centre(file)
	elif ans == 2:
		display.scale(file)
	sys.exit(0)

def die(message):
	box = MultipleChoice(message, ['OK'])
	box.set_title('Wallpaper')
	box.wait()
	sys.exit(1)

box = MultipleChoice(
"Wallpaper sets your desktop background by randomly choosing \
an image from your wallpaper directory.\n\n\
Choose 'Setup' to show the directories containing the images (creating \
them if they don't exist).\n\n\
Choose 'AutoStart' to choose a random image when you login (if you use \
ROX-Session).\n\n\
Choose 'Random' to pick a random image now.\n\n\
You can also drag an image to the Wallpaper icon to see it immediately.",
['Setup', 'AutoStart', 'Random', 'Cancel'])

box.set_title('Wallpaper')
answer = box.wait()

def endir(dir):
	if not os.path.exists(dir):
		os.mkdir(dir)

if answer == 0:
	dir = choices.save('Wallpaper', 'Tiled')
	if not dir:
		die("Saving of choices is disabled by CHOICESPATH --- \
can't create Wallpaper directories")
	endir(dir)
	endir(choices.save('Wallpaper', 'Scaled'))
	endir(choices.save('Wallpaper', 'Centred'))

	dir = choices.save('Wallpaper', '')
	box = MultipleChoice("Copy, move or symlink images into the three \
directories `%s<type>', depending on how you want them displayed." % dir,
			['OK'])
	box.set_title('Setup')
	box.wait()
	filer.open_dir(dir)
elif answer == 1:
	dir = choices.save('ROX-Session', 'AutoStart')
	if not dir:
		die("Saving of choices is disabled by CHOICESPATH --- \
can't create ROX-Session/AutoStart directory")
	endir(dir)
	src = os.path.join(os.path.abspath(support.app_dir), 'AutoStart.py')
	dst = os.path.join(dir, 'Wallpaper')
	if os.path.islink(dst):
		os.remove(dst)
	if os.path.exists(dst):
		message = "%s already exists. Please delete it first if you \
don't want it." % dst
	else:
		os.symlink(src, dst)
		message = "I've added a Wallpaper entry to `%s'. You \
can delete this file to stop the wallpaper being set on login." % dir
		   
	box = MultipleChoice(message, ['OK'])
	box.set_title('AutoStart')
	box.wait()
	filer.open_dir(dir)
elif answer == 2:
	import AutoStart
