#!/usr/bin/perl -w
# Automatic mono-server file generator
#
# With this script the user can update the host files 
# that are installed in /etc/mono-server/conf.d/package and create a 
# 'big' host file (/etc/mono-server/mono-server*-hosts.conf) that will be used
# by mono-server to setup the virtual hosts needed by 
# the user.
#
# Under GPL, please read: 
# http://www.gnu.org/copyleft/gpl.html
#
# Written by: Pablo Fischer

=head1 NAME

mono-xsp2-update.conf - creates .webapp needed by xsp2

=head1 SYNOPSIS

mono-xsp2-update.conf

=head1 DESCRIPTION

 mono-xsp2-update.conf is a perl tool to update/create a .webapp in /etc/xsp2

 This file is needed by xsp2 cause it has all each ASP.NET application with a path and alias, needed 
 by xsp2 to setup these applications.

 The .webapp is created with other host configuration files that are in /etc/xsp2/conf.d

 For more information read the README.Debian of this package (/usr/share/doc/mono-xsp2/README.Debian).

=head1 AUTHOR

 Pablo Fischer <pablo@pablo.com.mx>

=cut

use strict; 
use Digest::MD5;

#Main vars..
my ($monoxsp2_dir, $monoxsp2_confd, $monoxsp2_hostfile, $monoxsp2_webapp,
    $daemon, $daemon_pid, $default_file,
    $applications, $libs);

#Setup main vars
$monoxsp2_dir = "/etc/xsp2";
$monoxsp2_confd = "$monoxsp2_dir/conf.d";
$monoxsp2_webapp = "$monoxsp2_dir/debian.webapp";
$daemon = "/etc/init.d/mono-xsp2";
$daemon_pid = "/var/run/mono-xsp2.pid";
$applications = "";
$default_file = "/etc/default/mono-xsp2";

my $restart = "yes";
my $first_file = "yes";
my ($orig_md5, $new_md5);

#Check write access to $monoxsp2_hostfile
if( ( -e "$monoxsp2_webapp" && ! -w "$monoxsp2_webapp" ) || ! -w "$monoxsp2_dir" ) {
    print "mono-xsp2-update.conf requires write access to $monoxsp2_webapp or 
be executed by root\n" ; 
    exit 1 ;
}

#Read the default file
&read_default_file;
#Orig md5
$orig_md5 = &get_md5;
#Read directory..
&read_dir;
#The tail
&write_tempdefault_end;
#Prepare the application string
$applications =~ s/,$//;
#Final md5
$new_md5 = &get_md5;
#Equal?
if(("$new_md5" ne "$orig_md5") && ($restart eq "yes")) {
    if(( -f $daemon ) && ( -f $daemon_pid )) {
	system("$daemon restart");
    }
}


sub get_md5 {
    if( -e $monoxsp2_webapp ) {
	open(WEBAPP, $monoxsp2_webapp);
	binmode(WEBAPP);
	return Digest::MD5->new->addfile(*WEBAPP)->hexdigest;
	
    }
    else {
	return "";
    }
}

sub read_default_file {
    
    if(-e $default_file) {
	open(DEFAULT_FILE, "$default_file");
	while(my $line = <DEFAULT_FILE>) {
	    if($line =~ /start_boot/i) {
		if($line =~ /true/i) {
		    $restart = "yes";
		}
		else {
		    $restart = "no";
		}
	    }
	}
	close(DEFAULT_FILE);
    }
}


sub read_dir {
    opendir(DIR, $monoxsp2_confd);
    my @host_dirs = sort (grep { -d "$monoxsp2_confd/$_" } readdir(DIR));
    closedir(DIR);

    system("rm -Rf $monoxsp2_webapp");
    system("touch $monoxsp2_webapp");

    #How many dirs?
    if($#host_dirs ne "0") {
	#The head
	&write_tempdefault_start;
	foreach my $dir (@host_dirs) {
	    if(($dir ne "..") && ($dir ne ".")) {
		#Ok, in the dir.. we have more files, so read them
		opendir(DIR, "$monoxsp2_confd/$dir");
		my @host_files = sort (readdir(DIR));
		closedir(DIR);
		#Is it empty?		
		if($#host_files ne "0") {
		    #So, read it..
		    foreach my $hostfile (@host_files) {
			#Just remember.. we don't like directories inside directories!
			if(($hostfile ne "..") && ($hostfile ne ".")) {
			    $hostfile = "$monoxsp2_confd/$dir/$hostfile";
			    write_tempxsp2hostfile($hostfile);
			}
		    }
		}
	    }
	}
    }
}

sub write_tempdefault_start {

    open(TEMPWEBAPP, ">> $monoxsp2_webapp");
    print TEMPWEBAPP "<apps>\n";
    close(TEMPWEBAPP);
    
}


sub write_tempdefault_end {

    open(TEMPWEBAPP, ">> $monoxsp2_webapp");
    print TEMPWEBAPP "</apps>\n";
    close(TEMPWEBAPP);

}  

sub write_tempxsp2hostfile {
    my $hostfile = shift;

    #Write the content to a temp file..
    open(TEMPWEBAPP, ">> $monoxsp2_webapp");
    #And open the hostfile..
    open(HOSTFILE, "$hostfile");
    #Read it..
    my @content_hostfile = <HOSTFILE>;
    #Close it..
    close(HOSTFILE);
    #Write the header to the monoxsp2_hostfile
    
    my ($path, $alias, $name);

    foreach my $line (@content_hostfile) {	
	if($line =~ /path/i) {
	    #Ok, the directory exists?
	    my $dir = (split /\=/, $line)[1];
	    #Remove blank spaces
	    $dir =~ tr/\ //d;
	    #remove that \n
	    $dir =~ s/\n//;
 	    if ( ! -d "$dir" ) {
		$dir = "";
		last;
 	    }
	    else {
		$path = $dir;
	    }
	}

	if($line =~ /alias/i) {
	    $alias = (split /\=/, $line)[1];
	    #Blank Spaces
	    $alias =~ tr/\ //d;
	    #New lines..
	    $alias =~ s/\n//;
	    #The name
	    $name = $alias;
	    $name =~ s|/*||;
	}	
    }	
    
    if($path) {
	$applications = "$applications$alias:$path,";
	$libs = "$libs:";

	print TEMPWEBAPP "  <web-application>\n";
	print TEMPWEBAPP "    <name>$name</name>\n";
	print TEMPWEBAPP "    <vpath>$alias</vpath>\n";       
	print TEMPWEBAPP "    <path>$path</path>\n";
	print TEMPWEBAPP "  </web-application>\n";
    }
    close(TEMPWEBAPP);
}  
