#!/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

# Read arguments passed from cmd line
if len(sys.argv) > 1:
	if sys.argv[1] == "l" or sys.argv[1] == "r":
		status = 1
		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"
		superkaramba = os.system("which superkaramba 2>&1 > /dev/null")		
		havexwi = os.system("which xwininfo 2>&1 > /dev/null") # Autodetect the resolution, thanks to Adam Geitgey!
		kfmclient = os.system("which kfmclient 2>&1 > /dev/null")		
		kdialog = os.system("which kdialog 2>&1 > /dev/null")
# Some file checking
		if (superkaramba == 0): pass
		else:
		    status = 0
		    print "\n Oooops. It seems that you DO NOT have SuperKaramba!!!\nThere is no other way to use PNM but with SK. Better install one :)\n"

		if (kdialog == 0): pass
		else: 
		    status = 0
		    print "\nCan't find kdialog in your path.\nDo you have KDE installed?\n"

		if (kfmclient == 0): pass
		else: 
		    status = 0
		    print "\nCan't find kfmclient in your path.\nDo you have KDE properly installed?\n"

		if (havexwi == 0): pass
		else: 
		    status = 0
		    print "\nCan't find xwininfo in your path.\nIt is necessary for PNM to run.\n"

# Well, do we have everything? If so, let's go forward.
		if status == 1:
		    os.system("rm -f $HOME/.superkaramba/PNM4.rc")
    		    if side == "l":
			    x = 0
		    elif side == "r":
			    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()
		    print "\nAll is OK.\n"
		else:
		    print "Too many errors.\nGiving up.\n"
		    sys.exit(0)
	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)
# Have fun!
