#!/usr/bin/env python
# Copyright 2003,  Maciej "maciunio" Paczesny <maciunio (at) ask - bsi (dot) org>
#                  Lukasz "lookanio" Purgal <lookanio (at) interia (dot) pl>
#
#    This file is part of "Polish News Module".
#
#    "Polish News Module" is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    "Polish News Module" is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with "Polish News Module"; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
import sys

if len(sys.argv) > 1:
	if sys.argv[1] == "l" or sys.argv[1] == "r":
		if sys.argv[1] == "l":
			print "You've decided to install PNM on 'left' side"
			side = "l"
		elif sys.argv[1] == 'r':
			print "You've decided to install PNM on 'right' side"
			side = "r"
		else:
			print "Hmmm... It is not possible to see this line. Theoretically. ;)\n"
		# Autodetect the resolution, thanks to Adam Geitgey!
		havexwi = os.system("which xwininfo 2>&1 > /dev/null")
		os.system("rm -f $HOME/.superkaramba/PNM4.rc")

		if side == "l":
			x = 0
		elif side == "r":
			if (havexwi == 0): pass
			else: print "\nCan't find xwininfo in your path.\n"

			fp = os.popen("xwininfo -root -stats")
			output = fp.read()
			output = output.splitlines()
			i = 0
			print "Your screen resolution is: "
			for x in output:
				param = x.split()
				if (len(param) > 1):
					if param[0].find("Width:") != -1:
						print "X = " + str(param[1])
						resX = int(param[1])
					if param[0].find("Height:") != -1:
						print "Y = " + str(param[1])
						resY = int(param[1])
			x = resX-465

		theme = "# Main \"window\"\n"
		theme += "KARAMBA X=" + str(x) + " Y=40 W=465 H=430 INTERVAL=1000000 LOCKED=TRUE\n"

		theme_file = open(sys.path[0] + '/PNM4.theme', 'w')
		theme_file.write(theme)
		theme_file.close()
	else:
		print "\n\nERROR: You specified WRONG 'side' argument!"
		print "It should be 'l' or 'r' only.\n"
		sys.exit(0)
else:
	print "\n\nERROR: You did not specify the 'side' argument!"
	print "Try to install PNM again and do not forget to decide the side of your desktop the panel should be installed at.\n"
	sys.exit(0)
