#!/usr/bin/perl

# Support functionality for the TWiki Collaboration Platform, http://TWiki.org/
# 
# TWiki Shell 
# Oct 2004 - written by Rafael Alvarez based on MartinCleaver work
#
# Assumes that the user has enough privileges to perform the required operations.
#
#-------------------------------------------------------------------------------
#
# For licensing info read license.txt file in the TWiki root.
# This program 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, published at 
# http://www.gnu.org/copyleft/gpl.html
#
#-------------------------------------------------------------------------------

use vars qw {
    $twikiLibPath
    $config
};


#My vars
use vars qw {
    $VERSION
};
    
$VERSION = "1.00";


use diagnostics;


####################################################################################
{
package Config;
    my $mode="";
    
    sub new {
        my $self=bless {};
        
        $self->{verbosity}=1;
        return  $self;
    }
    
    #######################################
    # Printing stuff
    #######################################
    
    sub printVerbose { #verbose == 1 && verbose !=0
        my ($config,$text)=@_;    
        print $text unless ($config->{verbosity}<1);
    }

    sub printVeryVerbose{
        my ($config,$text)=@_;    
        print $text unless ($config->{verbosity}<2);
    }
    
    sub printNotQuiet {
        my ($config,$text)=@_;    
        print $text unless ($config->{verbosity}==0);
    }
    
    #######################################
    # Mode stuff
    #######################################

    sub mode {
        my ($self,$newMode)=@_;
        if ($newMode) {
            if ($newMode eq "--") {
                $mode="";
            } else {
                $mode=$newMode;
            }
        } else {
            return $mode;
        }
    }

    
    #######################################
    # Handle CL params
    #######################################

    sub handleParams {
        my $self=shift;
        my $n = 0;
        my $execute = 0;
    
        while ($n <= $#ARGV) {
            if ($ARGV[$n] =~ /^--?(.+)/o) {
                if ($1 eq "v") {
                    $self->{verbosity}=1;
                } elsif ($1 eq "vv") {
                    $self->{verbosity}=2;
                } elsif ($1 eq "q") {
                    $self->{verbosity}=0;
                } elsif ($1 eq "d") {
                    $self->{debug}=1;
                } elsif ($1 eq "init") {
                    $self->{initialize}=1;
                }

            } else {
                $self->{execute}.=$ARGV[$n]." ";
            }
            $n++;
        }   
    }
    
    #######################################
    # Prepare the enviroment
    #######################################
    
    sub prepareEnvironment {
        my $self=shift;
        print "The config file was not found\n";
        print "I'll explore the system and try to figure out your configuration\n";
        $self->_checkSetLibCfg("./bin");
        print "\n";
    }
    
    sub _checkSetLibCfg {
        my ($self,$twikiBinDir)=@_;
        print("   * Looking for $twikiBinDir/setlib.cfg ..... ");
    
        if (-d $twikiBinDir && -f "$twikiBinDir/setlib.cfg") {
            print("FOUND ($twikiBinDir/setlib.cfg)\n");   
        } else {
            print("NOT FOUND\n");
            $twikiBinDir=_findLibFile();
        }
        $self->{twikiBinDir}=$twikiBinDir;
    }
    
    sub loadSetLibCfg {
        my  $self=shift;
        my $setLibFile=$self->{twikiBinDir}."/setlib.cfg";
       
        do $setLibFile;    
        #$self->{twikiLibDir}=$twikiLibPath;
    }
    
    sub _findLibFile {
        print "     Please tell me the path to setlib.cfg \n      ---> "; 
        my $configPath;
        do {
            chomp ($configPath = <STDIN>) ;
        } until ((-f "$configPath/setlib.cfg") ? 1 :
           (print("     Hmmm - I can't see setlib.cfg at $configPath ... please check and try again\n      --->"), 0) 
        );
        return $configPath;
    }
    
    
    sub saveAndDispose {
        my $self=shift;
        
        if ($self->{execute}) {
            $self->{execute}="";
        }
        
        $self->{debug}=0;
        $self->{initialize}=0;
        $self->save();        
    }
    
    sub save {
        my $self=shift;
        use Data::Dumper;
        
        my $data= Data::Dumper->Dump([$self],[qw(config)]);
        open CONFIG,">".$self->{configFileName};
        print CONFIG $data;
        close CONFIG;
    }
}
####################################################################################


BEGIN {
    my $configFileName = ".twikisdkrc";    
    if (-f "./".$configFileName) {
        do "./".$configFileName
    } else {
        $config=new Config();
        #$config->prepareEnvironment();
        $config->{configFileName}=".twikisdkrc";
    }
 
    if (defined $config) {
	    $config->handleParams();
        #$config->loadSetLibCfg();
    }

    unshift @INC,"./lib";
    $config->save();
}

# The shell starts here

use TWiki::Contrib::TWikiShellContrib::TWikiShell;

my  $shell= new TWiki::Contrib::TWikiShellContrib::TWikiShell($config);

if ($config->{initialize}) {
    $shell->cmd("import Sdk::Init");
    $shell->cmd("sdk init");
    $shell->cmd("import sdk");    
} elsif ($config->{execute} && $config->{execute} ne "shell") {
    $shell->importCommand("Sdk");    
    $shell->cmd("sdk " . $config->{execute});
}else {
    print "\n";
    print "TWiki Interactive Shell v$VERSION
 Oct 2004 - written by Rafael Alvarez based on MartinCleaver work
 Type \"help\" for a list of available commands
 
";
    $shell->importCommand("Sdk");    
    $shell->prompt("sdk");
    $shell->cmdloop();
}

$config->saveAndDispose();



