#!/usr/bin/perl
# platinumConf
#
# Configuration Script for ApplePlatinum

############# Stuff to do on Startup, like find eesh, create the backup, and add to the menu.

## Create the backup

	`stty erase ^h`; #this should get rid of the annoying backspace bug. disable if you have problems.

if (-e "$ENV{HOME}/.ApplePlatinum") {
	#load prefs
	open (CONFIGFILE, "$ENV{HOME}/.ApplePlatinum"); # this is the config file for this program
	@configFile = <CONFIGFILE>;
	close (CONFIGFILE);
	foreach $row (@configFile) {
		chomp ($row);
		($setting, $value) = split (/ = /, $row);
		$configs{$setting} = $value;
	}
	print "theme =$configs{'themeDir'}\n";
	$test = chdir("$configs{'themeDir'}/ApplePlatinum");
	print $test;
} else {
	$firstUse = 1;
}
 
## Find out where eesh is.

if ($firstUse) { #($configs{'eesh'} =~ /undefined/)

    if (!$configs{'eesh'}) {
	if ($ENV{'EROOT'}) {
		$configs{'eesh'} = "$ENV{'EROOT'}/bin/eesh";
	} else {
		@sys = `type eesh`;
		if ($#sys < 0) {
			@sys = `/usr/local/enlightenment/bin/eesh -help`;
			if ($#sys < 0) {
				$configs{'eesh'} = "eesh not found.";
				$eeshFail = 1;
			} else {
				$configs{'eesh'} = "/usr/local/enlightenment/bin/eesh";
			}
		} else {
			$configs{'eesh'} = $sys[0];
		}
	}
    

	print "** platinumConf Configuration **\n\n";
	if ($eeshFial) {
		print "Your eesh was not found. Please enter\n";
		print "the location or command to eesh below\n";
		print "(usually found in elightenment/bin.)\n";
		print "Your eesh is at: > ";
		$discard = <STDIN>;
		$configs{'eesh'} = chomp ($discard);
	} else {
		print "Your eesh was found at:\n";
		print "$configs{'eesh'}\n";
		print "Is this correct? [y/n] > ";
		print $discard = <STDIN>;
		if (($discard =~ /n/) or ($discard =~ /N/)) {
			print "Enter the location of your eesh (inluding eesh itself)\n";
			print "> ";
			$discard = <STDIN>;
			$configs{'eesh'} = chomp ($discard);
		}
	}
    }
	
	## figure out working directory
    if (!$configs{'themeDir'}) {
	if ($0 ne "./platinumConf") {
		@eeshTest = split (/\//, $configs{'eesh'});
		if (($#eeshTest > 2) && ($eeshTest[$#eeshTest] eq "eesh")) {
			$discard = pop(@eeshTest);
			$discard = pop(@eeshTest);
			if (@eeshTest[0] ne "~") {
				$discard[0] = "";
				unshift(@discard,@eeshTest);
			}
			@eeshTest[$#eeshTest+1] = "themes";
			$configs{'themeDir'} = join ("/", @eeshTest);
		} else {
			if ($ENV{'EROOT'}) {
				$configs{'themeDir'} = "$ENV{'EROOT'}/themes";
			} else {
				$configs{'themeDir'} = "failed";
			}
		}
	} else {
		use Cwd;
		$apDir = getcwd;
		$apDir =~ s/ApplePlatinum//;
		$configs{'themeDir'} = $apDir;
	}
		
	
	print "\nYour themes seem to be in $configs{'themeDir'}\n";
	print "Is this correct? [y/n] > ";
	$discard = <STDIN>;
	if (($discard =~ /n/) or ($discard =~ /N/)) {
		print "\nEnter your theme directory\n";
		print "> ";
		$discard = <STDIN>;
		$configs{'themeDir'} = chomp($discard);
	}
			
	# Get OK to Procede Configs
    }
    
    	### find out what browser they use.
    	print "\nThe default web browser for viewing websites\n";
    	print "is netscape. Type n if this is not your default\n";
    	print "> ";
    	$discard = <STDIN>;
    	chomp ($discard);
    	if ($discard eq "n") {
    		print "\n*** Enter the command to view webpages with your browser\n";
    		print "    > ";
    		$discard = <STDIN>;
    		chomp ($discard);
    	} else {
    		$discard = "netscape";
	}
    	$configs{'browser'} = $discard;
    	
    	### find out what term program they use
     	print "\nThe default term execution command is 'Eterm -e'\n";
    	print "This is used to launch platinumConf from the finder\n";
    	print "menu. Type n if this is not your default\n";
    	print "> ";
    	$discard = <STDIN>;
    	chomp ($discard);
    	if ($discard eq "n") {
    		print "\n*** Enter the command to have your term program execute a script\n";
    		print "    > ";
    		$discard = <STDIN>;
    		chomp ($discard);
    	} else {
    		$discard = "Eterm -e";
    	}
    	$configs{'term'} = $discard;   	
    	
	#### We're done.
	print "\n\nplatinumConf is now configured. If you have\n";
	print "any problems you can reconfigure platinumConf\n";
	print "By editting .ApplePlatinum in your home directory.\n\n";
	print "Press Enter to continue\n";
	$discard = <STDIN>;

	#### Now add the platinumConf & heagy.com buttons to the 

} ## end of first time use section.

### Should set current working dir before we do anything

print "theme = $configs{'themeDir'}\n";
$test = chdir("$configs{'themeDir'}/ApplePlatinum");
print $test;

if (-e "cfgbak") {
	print "original backups exist.\n";
} else {
	`mkdir cfgbak`;
	`cp *.cfg cfgbak`;
	print "default backup created.\n";
}

### now print to the conf file.

open (CONFIGFILE, ">$ENV{HOME}/.ApplePlatinum");
foreach $key (sort (keys (%configs))) {
	if ($key ne "") {
		print CONFIGFILE "$key = $configs{$key}\n";
	}
}
close (CONFIGFILE);

### Now add heagy.com and platinumConf to the menu.

	## First we get the current list of items
			
	open (CURRENT, "slideouts.cfg");
	@currentItems = <CURRENT>;
	close (CURRENT);
	undef (@items); # should fix a bug on retrering this section.
	foreach $row (@currentItems) {
		if ($row =~ /END_SLIDEOUT/) {
			last;
		} elsif ($row =~ /ADD_BUTTON_TO_SLIDEOUT/) {
			$row =~ /.*"(.*)"/;
			$items[$#items+1] = $1;
			# print $items[$#items];
		}
	}

if (($firstUse) || ($items[$#items-1] ne "TASK_PLATINUMCONF")) {

	open (ACTIONS, "actionclasses.cfg");
	my @actions = <ACTIONS>;
	close (ACTIONS);
	foreach $action (@actions) {
		$action =~ s/__ACTION __A_EXEC netscape/__ACTION __A_EXEC $configs{'browser'}/;
		$action =~ s/__ACTION __A_EXEC Eterm -e platinumConf/__ACTION __A_EXEC $configs{'term'} $configs{'themeDir'}\/ApplePlatinum\/platinumConf/;
	}
	open (ACTIONS, ">actionclasses.cfg");
	print ACTIONS @actions;
	close (ACTIONS);

	&insertItemAt("TASK_BOTTOM","delete");
	&insertItemAt("TASK_SPACE","bottom");
	&insertItemAt("TASK_HEAGYCOM","bottom");
	&insertItemAt("TASK_PLATINUMCONF","bottom");
	&insertItemAt("TASK_BOTTOM","bottom");
	$shouldRestart = 1;
}

############# MENU TITLE

print "\n* * * * * * * * * * * * * * * * * * *\n";
print " ApplePlatinum Configuration Script\n";
print "* * * * * * * * * * * * * * * * * * *\n\n";
print "This script allows you to easily change\n";
print "settings for the ApplePlatinum etheme.\n";
print "Send feedback to platinumConf\@heagy.com\n\n";

############# DEAL WITH CHOICES

while ($choice !~ /q/) { # Continue to let user select from main menu until they decide to quit.
	
	################### MAIN MENU SELECTIONS

	print "\n\n**** Main Menu\n\n";
	print "     1) Dragbar settings\n";
	print "     2) Edit Finder Menu\n";
	print "     3) Smart Maximizing\n";
	print "     4) Selection Color\n";
	print "     r) Restore defaults\n";
	print "     q) Quit\n\n";
	print "     Your Choice > ";
	$choice = <STDIN>; #not highly sophisticated
	chomp ($choice);

	################### Choices are below. May be put into procedures.
	
	if ($choice == 1) { 
	
		$dragChoice = "";
		
		while ($dragChoice !~/b/) {   ### until the user decides to go back to the main menu, don't
			
			################### Dragbar settings
	
			print "\n\n   ***** Dragbar Settings\n\n";
			print "     There is only one setting:\n";
			print "     i) have an invisible dragbar,\n";
			print "     v) have a visible dragbar, and\n";
			print "     b) BACK to the main menu\n\n";
		
			######################## End of info.
			
			print "     Your Choice > ";			
			$dragChoice = <STDIN>;	
			chomp ($dragChoice);
			
			########## Deal with request for invisible drag bar.
					
			if (($dragChoice  eq "i") || ($dragChoice eq "v")) {
				print "\n     Your Dragbar will now be changed.\n";
				open (INVISFILE, "init.cfg");
				@invisFile = <INVISFILE>;
				close (INVISFILE);
				foreach $row (@invisFile) {
					if ($dragChoice eq "i") {
					
						if (($dragNext == 1) && ($row =~ /__NORMAL/)) {
							$row = "	__NORMAL \"images/trans.png\"\n";
							$dragNext = 0;
						} elsif ($dragNext == 2) {
							if ($row =~ /__NORMAL "images\/bar\/up.png/) {
								$row = "	__NORMAL \"images/trans.png\"\n";
							} elsif ($row =~ /__CLICKED/) {
								$row = "";
								$dragNext = 0;
							}
 						} elsif ($dragNext == 3) {
 							if ($row =~ /__NORMAL/) {
 								$row = "	__NORMAL \"images/trans.png\"\n";
 							} elsif ($row =~ /__CLICKED/) {
 								$row = "";
 								$dragNext = 0;
 							}
 						}
 					} elsif ($dragChoice eq "v") {
 						if ($dragNext == 1) {
 							if ($row =~ /__NORMAL/) {
 								$row = "	__NORMAL \"images/bar/bar.png\"\n";
 								$dragNext = 0;
 							}
 						} elsif ($dragNext == 2) {
 							if ($row =~ /__NORMAL/) {
 								$row = "	__NORMAL \"images/bar/up.png\"\n	__CLICKED \"images/bar/up_pressed.png\"\n";
 							} elsif ($row =~ /__END/) {
 								$dragNext = 0;
 							} else {
 								$row = "";
 							}
 						} elsif ($dragNext == 3) {
  							if ($row =~ /__NORMAL/) {
 								$row = "	__NORMAL \"images/bar/down.png\"\n	__CLICKED \"images/bar/down_pressed.png\"\n";
 							} elsif ($row =~/__END/) {
 								$dragNext = 0;
 							} else {
 								$row = "";
 							}
 						}
 					}					
	
				
				
					if ($row =~ /__NAME DESKTOP_DRAGBUTTON_HORIZ/) {
						$dragNext = 1;
					} elsif ($row =~ /__NAME DESKTOP_RAISEBUTTON_HORIZ/) {
						$dragNext = 2;
					} elsif ($row =~ /__NAME DESKTOP_LOWERBUTTON_HORIZ/) {
						$dragNext = 3;
					}	
					push (@finalInvis, $row);
				}
				open (INVISFILE, ">init.cfg");
				print INVISFILE @finalInvis;
				# for debug - print @finalInvis;
				close (INVISFILE);
				$shouldRestart = true;
				$dragChoice = "b";
				
			} else {
				print "\n\nThat was not an option. Please re-enter.\n";
			}
		}
	} elsif ($choice == 2) {
	
		while ($menuChoice !~ /b/) { # same back situation
		
			########## The most difficult section
			
			## First we get the current list of items
			
			open (CURRENT, "slideouts.cfg");
			@currentItems = <CURRENT>;
			close (CURRENT);
			undef (@items); # should fix a bug on retrering this section.
			foreach $row (@currentItems) {
				if ($row =~ /END_SLIDEOUT/) {
					last;
				} elsif ($row =~ /ADD_BUTTON_TO_SLIDEOUT/) {
					$row =~ /.*"(.*)"/;
					$items[$#items+1] = $1;
					# print $items[$#items];
				}
			}						
		
			print "\n\n   ***** Edit Finder Menu\n\n";
			print "     Use this section to fully customize\n";
			print "     The Finder menu.\n\n";
			print "     a) add a menu item\n";
			print "     m) move a menu item\n";
			print "     d) delete a menu item\n\n";
			#print "     b) set button actions\n";
			#print "     t) change tooltips\n";
			#print "     c) set selection color\n\n";
			print "     l) list current menu items\n\n";
			print "     b) BACK to the main menu\n\n";
			
			########################## End of menu
			
			print "     Your Choice > ";
			$menuChoice = <STDIN>;
			chomp ($menuChoice);
			
			if ($menuChoice eq "a") {
			
				$menuAddChoice = "";
				my @possibles; # should fix bug when adding more than once per session
				# add menu items
				# first get all possible bitmaps
				
				opendir (TASKDIR, "images/task");
				# print cwd; # for debug
				while ($fileTest = readdir (TASKDIR)) { #read each existing PNG
					if ($fileTest =~ /^\./) {
						next;
					}
					$fileTest =~ s/(.*)/\U$1\E/;
					# print "$fileTest\n";
					$uniqueBitmap = 0;
					if ($fileTest =~ /TASK_/) { #only consider TASK_* items
						$fileTest =~ s/(.*)\.PNG/$1/;
						$uniqueBitmap = 1;
					}
					foreach $entry (@items) {
						# print"'$fileTest' - '$entry'\n"; # debug
						if ("$fileTest" eq "$entry") {
							$uniqueBitmap = 0;
							# print "OKOKOKOKOKOKKKOKOKOKOKOK\n\n\n\n\n"; # ja
						}
					}
					if ($uniqueBitmap) {
						$possibles[$#possibles+1] = $fileTest;
						# print "NONONONONONN\n\n\n"; # also debug. this was broken
					}
				}
				closedir (TASKDIR);
				
				## print menu
				
				if ($#possibles >= 0) {
					print "   ** Please Select a Bitmap to add\n\n";
					$counter = 0;
					foreach $tempLine (@possibles) {
						$poss = $tempLine;
						$poss =~ s/TASK_//; #delete Task
						$poss =~ s/^(.)(.*)/$1\L$2\E/;
						$counter++;
						print "     $counter) $poss\n";
					}
					$low = 1; 
					$high = $#possibles+1;
					print "\nYour Choice [$low - $high]> ";
					
					#get selection
					$menuAddChoice = <STDIN>;
					chomp ($menuAddChoice);
					if (($menuAddChoice >= $low) && ($menuAddChoice <= $high)) {
						&addItem($possibles[$menuAddChoice-1]);
					} else {
						print "   *** That was out of range\n\n";
					}
				} else {
					print "   *** There are no more bitmaps to add\n";
					print "       Visit heagy.com to download more.\n\n";
				}
						
				
			} elsif ($menuChoice eq "m") {
				
				# move a menu item
				
				&listItems;
				print "\n   Choose an item to move\n";
				$moveSelectMax = $#items-4;
				print "\n   [1 - $moveSelectMax] > ";
				$moveChoice = "";
				$moveChoice = <STDIN>;
				chomp ($moveChoice);
				
				if ($moveChoice < 1 || $moveChoice > $#items -4) {
					print "\n   *** That was out of range\n";
				} else {
					$thisItemName = &getNiceName($items[$moveChoice]);
					$moveHigh = $#items - 3;
					print "   Enter a new Position for item '$thisItemName'\n";
					print "   [1 - $moveHigh] > ";
					$newMovePosition = <STDIN>;
					chomp ($newMovePosition);
					if ($newMovePosition < 1 || $newMovePosition > $moveHigh) {
						print "\n   *** That was out of Range.\n";
					} else {
						&insertItemAt ($items[$moveChoice], $newMovePosition);
						$shouldRestart = 1;
					}
				}
				
			} elsif ($menuChoice eq "d") {
			
				# delete a menu item
				&listItems;
				print "\n   Choose an item to delete\n";
				$delSelectMax = $#items-4;
				print "\n   [1 - $delSelectMax] > ";
				$delChoice = <STDIN>;
				chomp ($delChoice);
				if ($delChoice < 1 || $moveChoice > $delSelectMax) {
					print "   *** That is out of range.";
				} else {
					$delThisNice = &getNiceName($items[$delChoice]);
					$delThis = $items[$delChoice];
					print "\n   Are you sure you want to delete '$delThisNice'?\n";
					print "   [y/n] > ";
					$getOK = <STDIN>;
					chomp ($getOK);
					if ($getOK eq "y") {
						#### first, take it off the menu
						insertItemAt($delThis, "delete");
						#### Working top down, the button is next.
						open (BUTTONS, "buttons.cfg");
						@delButtons = <BUTTONS>;
						close (BUTTONS);
						@delButtons = &delete($delThis,@delButtons);
						open (BUTTONS, ">buttons.cfg");
						print BUTTONS @delButtons;
						close (BUTTONS);
						#### Then, the actionclass
						open (ACLASS, "actionclasses.cfg");
						@delAclass = <ACLASS>;
						close (ACLASS);
						@delAclass = &delete($delThis,@delAclass);
						open (ACLASS, ">actionclasses.cfg");
						print ACLASS @delAclass;
						close (ACLASS);
						#### Then, the Imageclass
						open (ICLASS, "imageclasses.cfg");
						@delIclass = <ICLASS>;
						close (ICLASS);
						@delIclass = &delete($delThis,@delIclass);
						open (ICLASS, ">imageclasses.cfg");
						print ICLASS @delIclass;
						close (ICLASS);
						$shouldRestart = 1;
					}
				}
			
			#} elsif ($menuChoice eq "b") {
			#### Disabled, but important. Will be in next release.
				#### First, print the options
				#&listItems;
				#print "\n     
				
			#} elsif ($menuChoice eq "t") {
			
				# edit tooltips - disabled for this release
				
			} elsif ($menuChoice eq "l") {
			
				#list current items
				&listItems;
				print "     hit enter to continue > ";
				$discard = <STDIN>;
				
			} else {
					print "\n\nThat was not an option. Please re-enter.\n";
			}
			
		} ### End of Finder Menu While
			
			
	} elsif ($choice == 3) {
	
		$maxChoice = "";
	
		while ($maxChoice !~ /b/) { # same back situation
		
			######################## Maximizing Menu
			
			print "\n\n   ***** Smart Maximization Menu\n\n";
			print "     To stop windows from covering the\n";
			print "     macbar extra space is added to the\n";
			print "     tops of windows. On by default.\n";
			print "     y) yes - use Smart Maximizing,\n";
			print "     n) no - cover the macbar\n";
			print "     b) BACK to the main menu\n\n";
			
			######################## End of Menu
			
			print "     Your Choice > ";			
			$maxChoice = <STDIN>;	
			chomp ($maxChoice);
			
			if ($maxChoice eq "y") {
				
				if (-e "nobarborders") {
					print "** It appears you are already using\n";
					print "** Smart Maximizing. If you want to\n";
					print "** Override this, reset the defaults\n";
					print "** For ApplePlatinum. Read the README.\n\n";
					$maxChoice = "b";
				} else {
					open (BORDER, "barborders");
					@newMax = <BORDER>;
					close (ORDER);
					
					open (OLDBORDER, "borders.cfg");
					@oldMax = <OLDBORDER>;
					close (OLDBORDER);
					
					`rm -rf barborders`;
					
					open (NEWBORDER, ">borders.cfg");
					print NEWBORDER @newMax;
					close (NEWBORDER);
					
					open (BAKBORDER, ">nobarborders");
					print BAKBORDER @oldMax;
					close (BAKBORDER);
					$maxChoice = "b";
					$shouldRestart = true;
				}
			} elsif ($maxChoice eq "n") {
				if (-e "barborders") {
					print "** It appears you have already turned\n";
					print "** Smart Maximizing OFF. If you want to\n";
					print "** Override this, reset the defaults\n";
					print "** For ApplePlatinum. Read the README.\n\n";
					$maxChoice = "b";				
				} else {
					open (NOBORDER, "nobarborders");
					@noMax = <NOBORDER>;
					close (NOBORDER);
					
					open (BORDER, "borders.cfg");
					@oldMax = <BORDER>;
					close (BORDER);
					
					`rm -rf nobarborders`;
					
					open (NEWBORDER, ">borders.cfg");
					print NEWBORDER @noMax;
					close (NEWBORDER);
					
					open (BAKBORDER, ">barborders");
					print BAKBORDER @oldMax;
					close (BAKBORDER);
					$maxChoice = "b";
					$shouldRestart = true;
				}
			} else {
					print "\n\nThat was not an option. Please re-enter.\n";
			}
		}
		
	} elsif ($choice == 4) {
		my $colorChoice;
	    while ($colorChoice !~/b/) { # same back situation
	
		# selection color
		
		print "   *** Change Selection Color\n\n";
		
		#### Discover the current color
		
		open (IMAGECLASSES, "imageclasses.cfg");
		my @images = <IMAGECLASSES>;
		close (IMAGECLASSES);
		
		my $currentModifier;		
		foreach $image (@images) {
			### locate current modifier
			if ($image =~ /__MODIFIER/ && $image !~ /NORMAL/) {
				$image =~ /.*__MODIFIER (.*)\n/;
				$currentModifier = $1;
			}
		}
		
		open (MODIFIERS, "colormodifiers.cfg");
		my @modifiers = <MODIFIERS>;
		close (MODIFIERS);
		$colorCounter = 1;
		my @colors;
		foreach $line (@modifiers) {
		
			if ($line =~ /__NAME/ && $line !~ /NORMAL/) {
				$line =~ /__NAME\s*(.*)\s*/;
				$colors[$colorCounter] = $1;
				print "     $colorCounter) $1\n";
				$colorCounter++;
			}
		}
		print "\n     Current color is $currentModifier. Select a new color\n";
		print "     or type b to return [1 - $#colors] > ";
		$colorChoice = <STDIN>;
		chomp ($colorChoice);
		if ($colorChoice < 1 || $colorChoice > $#colors) {
			print "\n   *** That was not a valid choice\n";
		} else {
			print "\n     Your selected $colors[$colorChoice]\n";
		}
		
		open (IMAGECLASSES, "imageclasses.cfg");
		my @freshImages = <IMAGECLASSES>;
		close (IMAGECLASSES);
		
		foreach $row (@freshImages) {
			$row =~ s/$currentModifier/$colors[$colorChoice]/;
		}
		
		open (IMGINF, ">imageclasses.cfg");
		print IMGINF @freshImages;
		close (IMGINF);
		
		#### Now we do the tres impressive thing - coordinate kde.
		
		if (-e "$ENV{HOME}/.kderc") {
			print "\n   *** platinumConf can also use this color as\n";
			print "       your selection color for KDE. Would you like\n";
			print "       the change made to KDE? [y/n] > ";
			my $kdeOK = <STDIN>;
			chomp ($kdeOK);
			if ($kdeOK eq "y") {
				my ($r,$g,$b);
				my $colorCounter;
				while ($colorCounter < $#modifiers) {
					#print "SMEG";
					if ($modifiers[$colorCounter] =~ /$colors[$colorChoice]/) {
						#print "GAGGLE";
						$modifiers[$colorCounter+1] =~ /200.(.{1,3})\s/;
						#print $colors[$colorCounter+1];
						$r = $1;
						$modifiers[$colorCounter+2] =~ /200.(.{1,3})\s/;
						$g = $1;
						$modifiers[$colorCounter+3] =~ /200.(.{1,3}) (.*)/;
						$b = $1;
						$colorCounter = $#modifiers++;
					}
					$colorCounter++;
				}
				open (KDERC, "$ENV{HOME}/.kderc") || die ();;
				@kderc = <KDERC>;
				close (KDERC);
				foreach $kdeLine (@kderc) {
					if ($kdeLine =~ /selectBackground/) {
						$kdeLine = "selectBackground=$r,$g,$b\n";
						#print "\n$kdeLine";
					}
				}
				open (KDERC, ">$ENV{HOME}/.kderc") || print $!;
				print KDERC @kderc;
				# print @kderc;
				close (KDERC);
				print "\nKDE cahnge made\m";
			}
			
		}
		
		$shouldRestart = 1;
		$colorChoice = "b";
	    }		
		
		
	} elsif ($choice eq "r") {
		
		while ($defChoice !~/b/) { # same back situation

			################### Reset Defaults
	
			print "\n\n   ***** Reset Defaults\n\n";
			print "     This section is meant to fix\n";
			print "     any problems that this script\n";
			print "     might cause. It will overwrite\n";
			print "     all your customizations with\n";
			print "     the original .cfg files. If\n";
			print "     this doesn't fix your problem,\n";
			print "     totally reinstall the Theme.\n\n";
			print "     You will loose all customizations!\n";
			print "     Type YES in capitals if you are\n";
			print "     Serious > ";
			
			######################## End of info.
			
			$defChoice = <STDIN>;
			chomp ($defChoice);
			if ($defChoice eq "YES") {
				print "\n\n**OK - Defaults being re-written...\n\n";
				`cp ./cfgbak/* . -f`;
				print "\n\n**Hopefully, your error has been fixed.\n\n";
				$shouldRestart = true;
				$defChoice = "b";
			} else {
				print "\n\n**Disaster avoided. Whew.\n\n";
				$defChoice = "b";
			}
		}
		$defChoice = "";
	} elsif ($choice eq "q") {
		print "\n\nHope you enjoy ApplePlatinum.\n";
		if ($shouldRestart) {
			print "PlatinumConf will now restart Enlightenment...\n";
			`$configs{'eesh'} -e restart`;
		}
		### now print to the conf file.
		open (CONFIGFILE, ">$ENV{HOME}/.ApplePlatinum");
		foreach $key (sort (keys (%configs))) {
			if ($key ne "") {
				print CONFIGFILE "$key = $configs{$key}\n";
			}
		}
		close (CONFIGFILE);
		exit();
	} else {
		print "\n\nThat is not an option. Please re-enter.\n\n";
	}
}

sub addItem ($) {

	my $newItem = $_[0]; #this gets us the name we were sent
	my $correct = "n";   #this ensures user apprroval of what we got. they can't quit, though...
    	while ($correct eq "n") {
    		$correct = "y";
    		my $counter = 1;
    		print "   *** Now you must enter the program or simple instrcution\n";
    		print "       The item will execute. (Blank = no action).\n\n";
    		while ($counter <= 3) {
			print "       Button $counter> ";
			$newExec[$counter-1] = <STDIN>;
			chomp ($newExec[$counter-1]);
			$counter++;
		}
		print "   *** And the tooltip that will show up?\n";
		print "       > ";
		$newTip = <STDIN>;
		chomp ($newTip);
		## get approval for these setings
		print "\n\n   *** These are the settings you have entered:\n\n";
		$counter = 1;
		foreach $numSetting (@newExec) {
			if ($numSetting ne "") {
				print "       Button $counter = $numSetting\n";
			}
			$counter++;
		}
		print "       Tooltip = $newTip\n";
		print "\n   *** If this is not correct, enter n. Otherwise, y.\n";
		print "       > ";
		$correct = <STDIN>;
		chomp ($correct);
	}
	
	## open the files and print etc
	#### First we print the Actionclass.
	#### This way if anything goes wrong, the theme will still work

	open (ACTIONS, "actionclasses.cfg"); #ok, somethings going wrong with this file, not here but...
	my @actions = <ACTIONS>;
	close (ACTIONS);

	## Check for an existing action class
	
	foreach $action (@actions) {
		if ($action =~ /__NAME $newItem/) {
			@actions = &delete($newItem, @actions); # makes it clean
			last;
			print "\n\nIt exists!\n"; # debugging
		}
	}
	
	#create the new action class
	#print it
	my @newAction;
	$newAction[0] = "__ACLASS __BGN\n"; # everyone needs this
	$newAction[1] = "     __NAME $newItem\n"; #name it, bitch
	$newAction[2] = "     __TOOLTIP_TEXT \"$newTip\"\n"; # add the tooltip
	$newAction[3] = "     __TYPE __TYPE_NORMAL\n"; #madatory
	$newAction[4] = "     __ALLOW_ANY_MODIFIER_KEYS __ON\n"; # may be changed in next release
	$newAction[5] = "     __EVENT __MOUSE_RELEASE\n"; # also may changed
	
	#### Add the buttons
	$counter = 1;
	foreach $buttonEntry (@newExec) {
		
		if ($buttonEntry ne "") { #leave out the blank ones
			$newAction[$#newAction+1] = "     __BUTTON $counter\n";
			$newAction[$#newAction+1] = "     __ACTION __A_EXEC $buttonEntry\n";
			$newAction[$#newAction+1] = "     __NEXT_ACTION\n";
		}
		$counter++;
	}
	$newAction[$#newAction+1] = "__END\n";
	
	#
	
	open (ACTIONSTEST, ">actionclasses.cfg");
	print ACTIONSTEST @actions;
	print ACTIONSTEST "\n@newAction";
	close ACTIONSTEST;
	
	#### Now we add the imageclass. Simple.

	open (IMAGECLASSES, "imageclasses.cfg");
	my @images = <IMAGECLASSES>;
	close (IMAGECLASSES);
	
	foreach $image (@images) {
	
		### locate current modifier
		
		if ($image =~ /__MODIFIER/ && $image !~ /NORMAL/) {
			$image =~ /.*__MODIFIER (.*)\n/;
			$currentModifier = $1;
		}
		
		### test & delete a existing duplicate
		
		if ($image =~ /__NAME $newItem/) { # yo yo yo. wassup cleanliness?
			@images = &delete ($newItem, @images);
			last;
			print "wassup bitch yuis here yall\n";
		}
	}
	
		### create the new imageclass
	
	$newLowercase = $newItem;
	$newLowercase =~ s/(.*)/\L$1\E/g;
	
	my @newImage;
	$newImage[0] = "__ICLASS __BGN\n";
	$newImage[1] = "     __NAME $newItem\n";
	$newImage[2] = "     __NORMAL \"images/task/$newLowercase.png\"\n";
	$newImage[3] = "     __MODIFIER NORMAL\n";
	$newImage[4] = "     __HILITED \"images/task/$newLowercase.png\"\n";
	$newImage[5] = "     __MODIFIER $currentModifier\n";
	$newImage[6] = "__END\n";
	
	open (IMAGETEST, ">imageclasses.cfg");
	print IMAGETEST @images;
	print IMAGETEST "\n@newImage";
	close (IMAGETEST);

	#### Now we add the button def. Easy
	
	open (BUTTONS, "buttons.cfg");
	my @buttons = <BUTTONS>;
	close (BUTTONS);
	
	foreach $button (@buttons) {
		if ($button =~ /__NAME $newItem/) { # keep it clean
			@buttons = &delete ($newItem, @buttons);
			last;
			print "\n\nI think it exists!\n";
		}
	}
	
		### create the new button. they are all the same.
	my @newButton;
	$newButton[0] = "__BUTTON __BGN\n";
	$newButton[1] = "     __NAME $newItem\n";
	$newButton[2] = "     __ICLASS $newItem\n";
	$newButton[3] = "     __ACLASS $newItem\n";
	$newButton[4] = "     __KEEP_ON_TOP __ON\n";
	$newButton[5] = "     __USE_SIZE_OF_IMAGE __ON\n";
	$newButton[6] = "     __MAKE_STICKY __ON\n";
	$newButton[7] = "     __SHOW_BY_DEFAULT __OFF\n";
	$newButton[8] = "__END\n";
	
		### write this shit out.
	
	open (BUTTONTEST, ">buttons.cfg");
	print BUTTONTEST @buttons;
	print BUTTONTEST "\n@newButton";
	close (BUTTONTEST);
	
	#### Add to the slideout
	
	&insertItemAt($newItem, "last");
	$shouldRestart = 1;

}

sub insertItemAt ($$) {

	my ($itemName, $itemPosition) = @_;
	#print "$itemName - $itemPosition\n";
	
	my $testCounter = 0;
	
	## deletes a duplicate.
	foreach $testItem (@items) {
		# print "$testItem - $itemName\n";
		if ($testItem eq $itemName) {
			splice (@items, $testCounter, 1);
			last;
		}
		$testCounter++;
	}

	#### find the position
	if ($itemPosition =~ /del/) {
		# return;
	} elsif ($itemPosition eq "last") {
		
		splice (@items, $#items-3, 0, $itemName);
	
	} elsif ($itemPosition eq "bottom") {
	
		push (@items, $itemName);
		
	} else {
	
		if ($itemPosition < 1) {
			$itemPosition = 1;
			print "*********ERROR************\n";
		} elsif ($itemPosition > $#items-3) {
			$itemPosition = $#items-3;
		}
	
		splice (@items, $itemPosition, 0, $itemName);
		
	}
	
	open (SLIDEOUTTEST, ">slideouts.cfg");
	print SLIDEOUTTEST "#include <definitions>\n\n__E_CFG_VERSION 0\n\n";
	print SLIDEOUTTEST "BEGIN_SLIDEOUT(\"tasklist\", __DOWN)\n";
	foreach $item (@items) {
		print SLIDEOUTTEST "     ADD_BUTTON_TO_SLIDEOUT(\"$item\")\n";
	}
	print SLIDEOUTTEST "END_SLIDEOUT";
	close (SLIDEOUTTEST);
}
	
sub listItems () {
	
	my $printCounter = 1;
	while ($printCounter < $#items-3) {
		my $niceName2 = &getNiceName($items[$printCounter]);
		print "     $printCounter) $niceName2\n";
		$printCounter++;
	}
	
}

sub getNiceName ($) {

	my $niceName = @_[0];
	$niceName =~ s/TASK_(.)(.*)/$1\L$2\E/;
	return $niceName;
}

sub delete ($@) { ##### Deletes an entry of the name in the array and returns the new array.

	my ($deleteName, @wholeArray) = @_;
	my $deleteThis = 0;
	
	my $offset = 0;
	while ($offset <= $#wholeArray) {
		if ($wholeArray[$offset] =~ /__BGN/) { #test each instance
			$length = 1;
			my $testLine = "";
			while ($testLine !~ /__END/) { #determine the length to remove this entry
				#print"gungan - $offset : $length\n";
				$testLine = $wholeArray[$offset+$length];
				if ($testLine =~ /__NAME $deleteName/) {
					$deleteThis = 1;
					#print "I AM DELETING THE SMEGGING THING";
				}
				$length++;
			}
		}
		
		if ($deleteThis) {
			last;
		} else {
			$offset++;
		}
	}
	if ($deleteThis) {
		splice (@wholeArray, $offset, $length);
		#print "\n ich - $offset : $length\n";
	}
	return @wholeArray;
}



