#!/usr/bin/perl

#  * Copyright © 2001 Paul Mangan <claws@thewildbeast.co.uk>
#  *
#  * This file 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.
#  *
#  * This program 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 this program; if not, write to the Free Software
#  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#  *

#	Script Name: The Nasnas
#	Script Version: 0.4

######################################################
######################################################
###						   ###
###  FOR USAGE INSTRUCTIONS RUN THIS SCRIPT WITH   ###
###  THE FOLLOWING OPTION:			   ###
###						   ###
###  ./sylpheed-switcher --help		     	   ###
###						   ###
######################################################
######################################################

use File::Path;
use Getopt::Long;

my $sylpheed    = 0;
my $claws       = 0;
my $sylphpath   = '';
my $clawspath   = '';
my $debug	= 0;
my $YouWantHelp = 0;

GetOptions("main"         => \$sylpheed,
           "claws"        => \$claws,
	   "main-path=s"  => \$sylphpath,
	   "claws-path=s" => \$clawspath,
           "debug"	  => \$debug,
	   "help"         => \$YouWantHelp);

chdir; ## use $HOME for the working directory

unless (-e ".sylpheed") {  ## check for the .sylpheed directory
	print "Your '.sylpheed' directory is missing. This probably means that ";
        print "you have never run sylpheed before.\n";
        print "Before running sylpheed-switcher run sylpheed normally at least once.\n";
        exit;
}

open (SYLSWIT, "<.sylpheed-switcher"); ## read the config file
	@conf_file = <SYLSWIT>;
close SYLSWIT;

if ($sylphpath && $clawspath) {  ## this is the initial set-up part
## write the config file
	$PATH_INFO = "$clawspath\n$sylphpath";
	open (CONFFILE, ">.sylpheed-switcher");
	print CONFFILE $PATH_INFO;
	close CONFFILE;
## it appears that you'd run this script before, but the config file was missing
	$DIR_CHECK = opendir(CHECKDIR, ".sylpheed-claws");
	closedir(CHECKDIR);
	if ($DIR_CHECK == 1) {
		print "\nNow you can run either the main branch or the claws\n";
		print "branch by using one of the following options:\n\n";
		print "./sylpheed-switcher --main\n./sylpheed-switcher --claws\n\n";
		exit;
	}
## copy the directory tree
	system "cp -R .sylpheed .sylpheed-main";
	system "cp -R .sylpheed .sylpheed-claws";
## when setting-up is done print a little message
	print "\nNow you can run either the main branch or the claws\n";
	print "branch by using one of the following options:\n\n";
	print "./sylpheed-switcher --main\n./sylpheed-switcher --claws\n\n";
	exit;
}
## the --help option stuff
if ($YouWantHelp) {
	print "\n The SYLPHEED<->CLAWS SWITCHER\n\n";
	print "If you are running this script for the first time you will need \n";
	print "to set up the configuration. Here is an example, adjust the paths\n";
	print "to suit your set-up, but remember you need the full path with the\n";
	print "executable included:\n";
	print "./sylpheed-switcher --main-path=/usr/local/bin/sylpheed --claws-path=/usr/bin/sylpheed\n\n";
	print "When you have done the initial set-up, you run either the main branch\n";
	print "or the claws branch by using one of the following switches:\n\n";
	print "./sylpheed-switcher --main\n./sylpheed-switcher --claws\n\n";
        print "Add --debug to run sylpheed in debug mode\n\n";
	exit;
}
## if the config file doesn't exist...
if (!@conf_file) {
	print "Use the following set-up options first, or --help for more info:\n";
	print "--claws-path=PATH   # path to (and including) the claws sylpheed executable\n";
	print "--main-path=PATH    # path to (and including) the main sylpheed executable\n";
	exit;
}
## check that the chosen execuatble exists
## get rid of the old symbolic link, make a new one, and run the required executable
if ($claws) {
        chomp $conf_file[0];
	unless (-e $conf_file[0]) {
		print "\n$conf_file[0] does not exist\n\n";
		exit;
	}
	rmtree(".sylpheed");
	symlink(".sylpheed-claws",".sylpheed");
        if ($debug eq 1) {
		exec "$conf_file[0] --debug";
        } else {
        	exec "$conf_file[0]";
        }
	exit;
} elsif ($sylpheed) {
        chomp $conf_file[1];
	unless (-e $conf_file[1]) {
		print "\n$conf_file[1] does not exist\n\n";
		exit;
	}
	rmtree(".sylpheed");
	symlink(".sylpheed-main",".sylpheed");
        if ($debug eq 1) {
        	exec "$conf_file[1] --debug";
        } else {
		exec "$conf_file[1]";
        }
	exit;
} elsif (!$claws && !$sylpheed && @conf_file) {  		## you've got the config file but haven't said
	print "\nUse one of the following switches:\n";		## which version you want to run
	print " --main		# to run sylpheed main\n";
	print " --claws	# to run sylpheed claws\n";
	print " --help		# to view the help message\n";
        print "Optionally, you can also use:\n";
        print " --debug 	# to run in debug mode [optional]\n\n";
	exit;
}
###############################
