#!/usr/bin/perl -w
# BEGIN LICENSE BLOCK
# 
#  Copyright (c) 2002-2003 Jesse Vincent <jesse@bestpractical.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License 
#  as published by the Free Software Foundation.
# 
#  A copy of that license should have arrived with this
#  software, but in any event can be snarfed from www.gnu.org.
# 
#  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.
# 
# END LICENSE BLOCK

#
# $Header: /raid/cvsroot/fm/bin/rtfm,v 1.3 2001/09/29 05:17:31 jesse Exp $
# RT is (c) 1996-2001 Jesse Vincent <jesse@fsck.com>

use strict;
use Carp;
use Getopt::Long qw(:config pass_through);


$VERSION="!!RT_VERSION!!";
$CONFIGFILE = '!!CONFIG_FILE_PATH!!';

use lib "!!LIB_PATH!!";


#This drags in  RT's config.pm
use RT::FM;
RT::FM::Init($CONFIGFILE);

use RT::Framework::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
	  		       GetCurrentUser GetMessageContent);



#Clean out all the nasties from the environment
CleanEnv();

#Load etc/config.pm and drop privs
LoadConfig();

#Connect to the database and get RT::SystemUser and RT::Nobody loaded
DBConnect();

#Drop setgid permissions
RT::FM::DropSetGIDPermissions();

#Get the current user all loaded

my $CurrentUser = new RT::FM::CurrentUser();
use RT::FM::CurrentUser;
#my $CurrentUser = GetCurrentUser();
$CurrentUser->Load('jesse');
$CurrentUser->UserObj->SetPassword('iron');

PickMode();


# {{{ Help

sub Help {



}

# }}}

# {{{ PickMode

sub PickMode {
    my ($help, 
	$listarticles);


    GetOptions ('help|h|usage' => \$help,
		'list-articles' => \$listarticles,
	       );
    
    if ($listarticles) { ListArticles() }
    elsif ($help)       { Help()}
    else {
	print "No command found\n";
    }
    exit(0);
}	

# }}}

# {{{ sub AdminArticles

sub AdminArticles {
    my %args;
    GetOptions(\%args, 'create-article','modify-articles=s',
	       'delete-article=s', 'display-article=s',
	       'article-Name=s', 'article-Summary=s',
	       'article-edit-content!');
    

    require RT::Article;	
    my $Article = new RT::Article($CurrentUser);
    if ($args{'delete-Article'}) {
	$Article->Load($args{'delete-Article'});
	unless ($Article->id) {
	    print "Couldn't load Article";
	    return(undef);
	}
	my ($val, $msg) = $Article->Delete();
	print "$msg\n";
    }
    elsif ($args{'create-Article'}) {
	#TODO edit the Article content
	my $content;

	my $linesref = GetMessageContent(CurrentUser => $CurrentUser,
				      Edit => 1);
	
	$content = join("\n", @{$linesref});
	

	my ($val, $msg) = $Article->Create(Name => $args{'Article-Name'},
					    Description => $args{'Article-Description'},
					    Content => $content,
					  );
	print "$msg\n";
    }	
    elsif ($args{'modify-Article'}) {
	
	$Article->Load($args{'modify-Article'});
	unless ($Article->Id()) {
	    print "Article not found\n";
	    return();
	}	
	my @attributes = qw( Name Description );
	foreach my $attrib (@attributes) {
	    if ( (exists ($args{"Article-$attrib"})) and
		 ($Article->$attrib() ne $args{"Article-$attrib"})) {
		
		my $method = "Set$attrib";
		my $val = $Article->$method($args{"Article-$attrib"});
				
	    }
	}
	if ($args{'Article-edit-content'}) {
	    
	    my $linesref = GetMessageContent(CurrentUser => $CurrentUser,
					     Content => $Article->Content,
					     Edit => 1);
	    
	    my $content = join("\n", @{$linesref});	    
	    my ($val) = $Article->SetContent($content);
	    print $val."\n";
	}	

    }	
    if ($args{'display-Article'}) {
	$Article->Load($args{'display-Article'});
	print $Article->Name . "\n". $Article->Description ."\n". $Article->Content."\n";
    }	
}	

# }}}

# {{{ sub ListArticles 
sub ListArticles {
    require RT::FM::ArticleCollection;
    my $articles = new RT::FM::ArticleCollection($CurrentUser);

    $articles->LimitToParent("0");
 
    
    ListChildArticles($articles, 0 );
}

sub ListChildArticles {
    my $articles = shift;
    my $depth = shift;
    
    while (my $article = $articles->Next()){ 
	warn "looking at $article";
	printf("%-16s %-64s\n",
	       " "x $depth . $article->Name(), $article->Summary());
	
	ListChildArticles($article->Children, ($depth+1));
    }    
    

}

# }}}
