#!/usr/local/bin/perl -w

use strict;

#
# Purpose: makesite creates virtual sites.
# It uses set of templates to create DNS, and HTTPD
# configurations files.
#
# Note: this is a very *simple* script.
#
# Copyright (c) Mohammed J. Kabir (kabir@nitec.com)
#
####################################################

use Time::localtime;

my $site              = $ARGV[0] or &syntax;
my $MAKESITE_DIR      = '/var/makesite';
my $USER              = 'httpd';
my $GROUP             = 'httpd';
my $PERMISSION        = '2770';
my $BASE_DIR          = '/tmp';
my $HTDOCS            = 'htdocs';
my $CGIBIN            = 'cgi-bin';
my $NAMED_PATH        = '/var/named';
my $NAMED_FILE_EXT    = '.zone';
my $NAMED_TEMPLATE    = "$MAKESITE_DIR/named.template";
my $NAMED_CONF        = '/etc/named.conf';
my $HTTPD_CONF        = '/usr/local/apache/conf/httpd.conf';
my $VHOST_TEMPLATE    = "$MAKESITE_DIR/httpd.template";
my $LOG_FILE          = "$BASE_DIR/makesite.log";

my $dir = $site;
my @domain_types = qw(com net org edu);
my ($domain_ext, $thesite_dir, $public_dir,
    $htdocs_dir, $cgibin_dir, $named_file,
    $dir_len, $temp_len);

my $tm = localtime(time);
my $date = sprintf("%s-%02s-%02s-%02d-%02d", $tm->year+1900,
                                             $tm->mon+1,
                                             $tm->mday,
                                             $tm->hour,
                                             $tm->min);

my $serial = sprintf("%s%02d%02d000",        $tm->year+1900,
                                             $tm->mon+1,
                                             $tm->mday);


$site =~ y/[A-Z]/[a-z]/;

# Get the lenght length with the EXT
$dir_len = length($dir);

foreach $domain_ext (@domain_types){ $dir =~ s/\.$domain_ext//g; }

# Get the new length without the EXT
$temp_len = length($dir);

# If the user has not entered an extension then show syntax.
&syntax if($temp_len == $dir_len);

$named_file = $NAMED_PATH . '/' . $dir . $NAMED_FILE_EXT ;
$thesite_dir = $BASE_DIR . '/' . $dir;
$htdocs_dir = $BASE_DIR . '/' . $dir . '/' . $HTDOCS;
$cgibin_dir = $BASE_DIR . '/' . $dir . '/' . $CGIBIN;

die "$thesite_dir already exist! Aborted!\n" if(-e $thesite_dir);

system("mkdir $thesite_dir");
system("mkdir $htdocs_dir");
system("mkdir $cgibin_dir");

&createNamedFile($named_file,$site,"$dir$NAMED_FILE_EXT");
&createIndexFile($htdocs_dir,$site);
&createVirtualHostConf( domain=>$site,
                        website=>"www.$site",
                        cgibin=>$cgibin_dir,
                        htdocs=>$htdocs_dir);

system("chown -R $USER.$GROUP $thesite_dir");
system("chmod -R $PERMISSION $thesite_dir");

open(FP,">>$LOG_FILE") || die "Can't write to log file.\n";
print FP "$date created www.$site [$htdocs_dir] site.\n";
close(FP);

exit 0;

sub createNamedFile{
   my $file = shift;
   my $domain = shift;
   my $database = shift;
   my $line;

   open(OUT,">$file") || die "Can't write $file\n";
   open(FP,$NAMED_TEMPLATE) || die "Can't open $NAMED_TEMPLATE\n";
   while($line=<FP>){
     $line =~ s/<DOMAIN>/$domain/g;
     $line =~ s/<SERIAL>/$serial/g;
     print OUT $line;
     }
   close(FP);
   close(OUT);

   open(FP,">>$NAMED_CONF") || die "Can't open $NAMED_CONF\n";
   print FP <<ZONE;

// $domain was created on $date
zone "$domain" {
        type master;
        file "$database";
};

ZONE
   close(FP);
   }

sub createIndexFile{
  my $htdocs = shift;
  my $domain = shift;

  open(FP,">$htdocs/index.html") || die "Can't write index.html \n";
  print FP <<INDEX_PAGE;
  <HTML>
  <HEAD> <TITLE> $domain </TITLE> </HEAD>
  <BODY BGCOLOR="white">
  <CENTER>
   This is $domain Web site
   <p> It was created on $date </p>
  </CENTER>
  </BODY>
  </HTML>
INDEX_PAGE

  close(FP);
  }

sub createVirtualHostConf{

  my %params = @_;
  my $line;
  open(OUT,">>$HTTPD_CONF") || die "Can't open $HTTPD_CONF $!\n";

  open(FP,$VHOST_TEMPLATE) || die "Can't open $VHOST_TEMPLATE $!\n";
  while($line=<FP>){
    $line =~ s/<DOMAIN>/$params{domain}/g;
    $line =~ s/<CGI-BIN-DIR>/$params{cgibin}/g;
    $line =~ s/<HTDOCS-DIR>/$params{htdocs}/g;
    $line =~ s/<WWW-SITE>/$params{website}/g;
    print OUT $line;
  
    }
  close(FP);
  close(OUT);
  }

sub syntax{
    print <<SYNTAX;

    makesite <virtual Internet domain>
    Example: makesite mobidac.com

SYNTAX
   exit 0;
   }

