#!/usr/bin/perl
#####################################################
#
# Multiscript Version .50
#
# This program executes multiscript files 
# in different languages.
#
# Multiscript files contain a scripts separated by tag
#
# This allows multiple scripts to reside in a 
# single file in different versions and languages.
# The individual scripts can be called by language
# or by name as command arguments.
#
# This project is on sourceforge and mirrored at URL
# http://www.mad-dragon.com/multiscript.html 
#
# Author Nathan Ross
#
# Copyright 2005 Nathan Ross
# Licensed under the GPL and Artistic Licence
#####################################################

use Getopt::Long;
use IO::Handle;
use IO::File;
use Fcntl;

my $VERSION = ".50";
my $numargs = $#ARGV + 1;
my $appname = $0;

local $writeflag = 0;
local $tmpfilename;
local $TMPFILE;

if ($numargs == 0) {
   print "$appname [FILENAME] -options ... \n";
   exit(0);
}

my $filename, $help, $getversion;
my @list, @lang, @program;
my $opt_version, $opt_random, $opt_perl, $opt_ruby, $opt_python, $opt_shell;
GetOptions("help|h"    => \$help, 
           "version|v" => \$getversion, 
           "Lperl"      => \$opt_perl, 
           "Lpython"    => \$opt_python, 
           "Lruby"      => \$opt_ruby, 
           "Lphp"      => \$opt_ruby, 
           "shell"     => \$opt_shell, 
           "prog=s"    => \@program); 

   if ($numargs == 1) {
     if ((!$help) && (!$getversion)) {
         parse_multiscriptfile($ARGV[0]);
     }
   }

   if ($help) {
       print "$appname [FILENAME]\n";
       usage();
   }
   if ($getversion) {
       print "$appname Version $VERSION\n";
   }

   # argument list 
   if ($#list >= 0) {
       foreach my $item (@list) {
       print "argument list item is $item\n";
       }
   }

   if (($numargs > 1) ) {
       parse_multiscriptfile($ARGV[0]);
   }

sub parse_multiscriptfile() {
my $filename = $_[0];
my $line;
my $lang;                 # the script language version
my $program_args;         # the script command line args if defined

open (CODEFILE, $filename) or die "Can't open $filename";
    $tmpfilename = get_tmpfilename();

    # print "Creating a new script temp file $tmpfilename\n";
    print "Creating a new script temp file $tmpfilename\n";
    umask 077;
    open ($TMPFILE, ">$tmpfilename") or die $!;

    while ($line = <CODEFILE>) {
       # print $line;
       if ($line =~ /^(<code ruby>\n)/) {
           set_writeflag(1);
       }
       elsif ($line =~ /(^<\/code ruby>\n)/) {
          clear_writeflag(1);
       }
       elsif ($line =~ /(^<code perl>\n)/) {
           set_writeflag(2);
       }
       elsif ($line =~ /(^<\/code perl>\n)/) {
           clear_writeflag(2);
       }
       elsif ($line =~ /(^<code python>\n)/) {
           set_writeflag(3);
       }
       elsif ($line =~ /(^<\/code python>\n)/) {
           clear_writeflag(3);
       }
       elsif ($line =~ /(^<code>\n)/) {
           set_writeflag(4);
       }
       elsif ($line =~ /(^<\/code>\n)/) {
           clear_writeflag(4);
       }
       else
       {
          if ($writeflag != 0) {
	      print $TMPFILE $line; 
	  }
       }
       $i++;
      }
# print "running the script\n";
# system("perl $tmpfilename");
close($TMPFILE);
close(CODEFILE);
unlink($tmpfilename);
}

sub usage() {
    print "  Mulitscript Executor \n";
    print "  This program will execute a multiscript file \n";
    print "  A multiscript file contains multiple script files ";
    print "  dillineated by tags. \n\n";
    print "  Program Arguments: \n";
    print "  -help    -h     help\n";
    print "  -version -v     program version\n";
    print "  -Lpython         run python\n";
    print "  -Lperl           run perl\n";
    print "  -Lruby           run ruby\n";
    print "  -Lphp            run php\n";
    print "  -list           program argument list to execute \n";
}

sub get_tmpfilename() {
    my $tmpname, $random;

    $tmpname = ".ms.";
    srand(time());
    $random = rand();
    $tmpname .= "$$";
    $tmpname .= $random;
    $tmpname .= ".tmp";

    print "tmpname = $tmpname\n";

    return ($tmpname);

}

sub set_writeflag()
{
  my $flag = $_[0];
  if ($writeflag != 0) {
      print "Code Error -- Not allowed nested code within code!!\n";
      exit(1);
  }
  $writeflag = $flag; 


}

sub clear_writeflag()
{
  my $flag = $_[0];
  my $returncode = 0;
  if ($writeflag != $flag) {
     print "Code Error -- Not allowed a different end tag to close a tag!!\n";
     exit(1);
  }
  if ($writeflag == 1) { $returncode = system("ruby $tmpfilename"); }
  if ($writeflag == 2) { $returncode = system("perl $tmpfilename"); } 
  if ($writeflag == 3) { $returncode = system("python $tmpfilename"); } 
  if ($writeflag == 4) { $returncode = system("$tmpfilename"); } 
  seek($TMPFILE, 0, 0);
  truncate($TMPFILE, 0);
  $writeflag = 0;
}



# POD
=head1 NAME
ms.pl
=head1 DESCRIPTION
- a Perl script that allows for multi script programming. The program will allow Perl, Python, Ruby or Shell or any other language to coexist in the same script. The scripts can be given version attributes and are dillineated by tags.  This program will run a multiscript program according to command options. 
=head1 README
The project page is mirrored on sourceforge.net and at http://www.mad-dragon.com/multiscript.html.
=pod SCRIPT CATEGORIES
CPAN
CPAN/Language
Educational:ComputerScience
=cut
