#!/usr/bin/perl -w
my $vernr = "0.0.2";
my $monthshort = "Mar";
my $monthlong = "March";
my $year = "2009";


use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;

###
# User defined settings
###
my $svn_path_remote  = 'https://kicad.svn.sourceforge.net/svnroot/kicad/trunk';
my $svn_path_local   = '/media/data/projects/applications/kicad/subversion/kicad-core';
my $build_path = '/home/jerry/builds/kicad';


##  TODO 
##  Add debian package generation
##	dh_make -e jerkejacobs@gmail.com -s -n
##  debuild -us -uc
##  TODO


###
# Commandline options
###
my $option_about              = 0;
my $option_manual             = 0;
my $option_help               = 0;
my $option_build_binaries     = 0;
my $option_svn_update         = 0;
my $option_verbose            = 1;
my $option_version            = 0;
my $option_generate_makefiles = 0;
my $option_no_clear           = 0;
my $option_install_binaries   = 0;
my $no_options                = 0; # No options given

###
# Commands
###
my $command_silent     = '&> /dev/null';          # Nullify stderr and stdout from commands
my $command_cmake      = "cmake -DCMAKE_BUILD_TYPE=Debug -DwxWidgets_USE_DEBUG=ON -DKICAD_AUIMANAGER=ON -DKICAD_AUITOOLBAR=ON $svn_path_local"; # Where cmake looks for CMakeLists.txt
my $command_svn_update = 'svn update';            # Subversion update command

###
# Help and about messages
###
my $about_message = "KiCad Devel, version $vernr, $monthshort $year, jerkejacobs\@gmail.org\n";
my $short_help = "No options given try `kicad_devel.pl --help' for more information.\n";


###########################################
########### Commandline options ###########
###########################################
if (@ARGV == 0) 
{
    print $short_help;
}
else 
{
    GetOptions('help|?'                   => \$option_help,
               'man'                      => \$option_manual,
               'build-binaries|compile'   => \$option_build_binaries,
               'svn-update'               => \$option_svn_update,
               'about'                    => \$option_about,
               'version'                  => \$option_version,
               'generate-makefiles'       => \$option_generate_makefiles,
               'install|install-binaries' => \$option_install_binaries,
               'no-clear'                 => \$option_no_clear,

               # Verbose settings
               'quiet|noverbose'          => sub { $option_verbose = 0 });

       pod2usage(1) if $option_help;
       pod2usage(-verbose => 2) if $option_manual;
}


###########################
########### Main ##########
###########################

# Init main function
main();

sub
main
{
    # No commandline options given
    if ($no_options) {
        print $short_help;
        exit(0);
    }

    # Option svn update given
	if ($option_svn_update) {
        svn_update();
    }

    # Generate makefiles
    if ($option_generate_makefiles) {
        generate_makefiles();
    }

    # Option build binaries given
    if ($option_build_binaries) {
        build_binaries();
    }

    # Install compiled binaries
    if ($option_install_binaries) {
        install_binaries();
    }
    exit(0);
}

###
# Clear the console screen
###
sub
clear_screen {

  # Clear screen if no clear option is false
  if ($option_no_clear == 0)
  {
    print `clear`;
  }
  else
  {
    print "\n\n";
  }
}

###
# Print line of $_[1] char
#  $_[0] = Number of chars before newline
#  $_[1] = Char to print line of
###
sub
print_line {
    for(my $i = 0; $i < $_[0]; ++$i) {
        print $_[1];
    }
    print "\n";
}

###
# Execute cmake on svn_path_local to generate makefiles
#   on build_path
###
sub
generate_makefiles {

  # Print settings to output
  if ($option_verbose == 1) 
  {
      clear_screen();

      print_line(80, '#');

      print " Generating makefiles\n";

      print_line(80, '#');

      print " SVN Path      : $svn_path_local\n";
      print " Build Path    : $build_path\n";
      print " CMake Command : $command_cmake\n";

      print_line(80, '#');
  }

  ###
  # Execute cmake command with correct verbose level output
  ###

  # Execute command and dump output to console
  if ($option_verbose == 0)
  {
      chdir $build_path;
      `$command_cmake $command_silent`;
  }

  # Execute command and display output to console
  if ($option_verbose == 1)
  {
      chdir $build_path
          or die "Can't cd to $build_path";
      print `$command_cmake`;
  }

  # Print output
  if ($option_verbose == 1)
  {
      print_line(80, '#');
  }
}

###
# Update local subversion repository on $svn_path_local
###
sub
svn_update 
{  
    if ($option_verbose)
    {
        clear_screen();
        print_line(80, '#');
        print "Updating local subversion repository\n";
        print_line(80, '#');
        print "Repository path : $svn_path_local\n";
        print "SVN Command     : $command_svn_update\n";
        print_line(80, '#');
        chdir $svn_path_local
            or die "Can't cd to $svn_path_local";
        print `$command_svn_update`;
        print_line(80, '#');
    }
    else
    {
        chdir $svn_path_local;
        `$command_svn_update $command_silent`;
    } 
}

###
# Build the binaries on $build_path
###
sub
build_binaries
{
	chdir $build_path
		or die "Can't cd to $build_path";
	system("make -j 4");
}

###
# Install the compiled binaries from $build_path
###
sub
install_binaries
{
  chdir $build_path
    or die "Can't cd to $build_path";
  system("make install");
}

######## Begin of POD manual page ########
__END__

=head1 NAME

kicad_devel - KiCad development helper program

=head1 SYNOPSIS

kicad_devel [options]

Options:

  --help               -?         brief help message
  --man                -M         full program manual
  --verbose            -V         set verbosity level
  --about                         about information
  --version            -v         display version information
  --svn-update         -svn-up    update kicad subversion path
  --build-binaries     -compile   compile sourcecode in build path
  --install-binaries   -install   install compiled binaries
  --no-clear                      dont clear the console screen after every command is executed

=head1 OPTIONS

=head2 HELP

=head1 DESCRIPTION

B<This program> will read the given input file(s) and do something
useful with the contents thereof.

=cut
