#!/usr/bin/perl
#####################################################
#  install    Part of GnuDIP 2.1.1                  #
#                                                   #
#    Install script for GnuDIP (MySQL Only!)        #
#                                                   #
#    See COPYING for licensing information          #
#                                                   #
#       Mike Machado <mike@innercite.com>           #
#                                                   #
#####################################################


use strict;
use DBI;

my ($sth, $dbtype, $onboot, $initdir, $isinuser, $isindb);
my $VER = "2.1.1";

my $ROOTUSER = 'root';
my $ROOTGROUP = 'root';

die "Must run install as root\n" if $> ne '0';

print "\n-Installing GnuDIP version $VER-\n\n";

while ($dbtype ne "1" && $dbtype ne "2") {
  print "Please select database type \n\t\t 1 - MySQL < 3.22.11\n\t\t[2 - MySQL >= 3.22.11] ";
  $dbtype = <STDIN>;
  chop($dbtype);
  if ($dbtype eq "") {
   $dbtype = "2";
  }
}


print "\nEnter root mysql username [root] ";
my $rootmysqluser = <STDIN>;
chop($rootmysqluser);
if ($rootmysqluser eq "") {
   $rootmysqluser = "root";
}

print "Enter root mysql password: ";
system("stty -echo");
my $rootmysqlpass = <STDIN>;
chop($rootmysqlpass);
system("stty echo");

my $dbh = DBI->connect("DBI:mysql:mysql",$rootmysqluser, $rootmysqlpass) || die "Could not connect to database server. Invalid login?\n";


print "\n\nEnter username GnuDIP should connect to database as [gnudip] ";
my $gnudipuser = <STDIN>;
chop($gnudipuser);
if ($gnudipuser eq "") {
   $gnudipuser = "gnudip";
}

my $gnudippass;
while ($gnudippass eq '') {
  print "Enter password you wish to use with the user $gnudipuser: ";
  $gnudippass = <STDIN>;
  chop($gnudippass);
}

print "\nEnter the user your webserver runs as [nobody] ";
my $webuser = <STDIN>;
chop($webuser);
if ($webuser eq "") {
   $webuser = "nobody";
}

print "Enter the user group the webserver runs as [nobody] ";
my $webgroup = <STDIN>;
chop($webgroup);
if ($webgroup eq "") {
   $webgroup = "nobody";
}

print "\nEnter the user to run the GnuDIP daemon as [nobody] ";
my $daemonuser = <STDIN>;
chop($daemonuser);
if ($daemonuser eq "") {
   $daemonuser = "nobody";
}

print "Enter the group to run the GnuDIP daemon as [nobody] ";
my $daemongroup = <STDIN>;
chop($daemongroup);
if ($daemongroup eq "") {
   $daemongroup = "nobody";
}

print "\nEnter the install prefix [/usr/local] ";
my $prefix = <STDIN>;
chop($prefix);
if ($prefix eq "") {
   $prefix = "/usr/local";
}
my $sedprefix = $prefix;
$sedprefix =~ s/\//\\\\\//g;

print "Enter the web server cgi-bin directory [/usr/local/apache/cgi-bin] ";
my $cgidir = <STDIN>;
chop($cgidir);
if ($cgidir eq "") {
   $cgidir = "/usr/local/apache/cgi-bin";
}

my $perldir;
if (-d "/usr/lib/perl5") {
 $perldir = "/usr/lib/perl5";
} elsif (-d "/usr/local/lib/perl5") {
 $perldir = "/usr/lib/perl5";
} else {
 print "Unable to determine perl library directory. Please enter perl5 library directory: ";
 chop ($perldir = <STDIN>);
}
my $correctperldir;
foreach my $inc (@INC) {
  if ($perldir eq $inc) {
    $correctperldir = "YES";
  }
}
if ($correctperldir ne "YES") {
 print "Perl library directory found is not in your INC path, resetting to a valid INC path\n";
 $perldir = $INC[0];
} 

while ($onboot ne "Y" && $onboot ne "y" && $onboot ne "N" && $onboot ne "n") {
  print "Would you like to automatically start GnuDIP on boot [Y,n] ";
  $onboot = <STDIN>;
  chop($onboot);
  if ($onboot eq "") {
   $onboot = "Y";
  } elsif ($onboot eq "y") {
   $onboot = "Y";
  } elsif ($onboot eq "n") {
   $onboot = "N";
  }

  if ($onboot eq "Y") {
    print "Enter the rc directory to place S99gnudip [/etc/rc.d/rc3.d] ";
    $initdir = <STDIN>;
    chop($initdir);
    if ($initdir eq "") {
       $initdir = "/etc/rc.d/rc3.d";
    }
  }
}


my $mysqlpassstring;
if ($rootmysqlpass eq '') {
  $mysqlpassstring = "";
} else {
  $mysqlpassstring = "-p$rootmysqlpass";
}


######### DO INSTALL STUFF ################
print "\n\n***********DOING INSTALL*************\n";


  system("mysqladmin -u$rootmysqluser $mysqlpassstring create gnudip2");
  system("mysql -u$rootmysqluser $mysqlpassstring gnudip2 < gnudip2.db");

  $sth = $dbh->prepare("select user from user where user = \"$gnudipuser\"");
  $sth->execute;
  $isinuser = $sth->fetchrow_array;
  $sth = $dbh->prepare("select user from db where user = \"$gnudipuser\" and db = \"gnudip2\"");
  $sth->execute;
  $isindb = $sth->fetchrow_array;

  if ($dbtype eq "1") {
    if ($isinuser eq '') {
      $sth = $dbh->do("insert into user \(host, user, password\) values \(\"localhost\", \"$gnudipuser\", password\(\"$gnudippass\"\)\)") || die "Could not add user to MySQL database\n";
    } else {
      $sth = $dbh->do("update user set password = password\(\"$gnudippass\"\) where user = \"$gnudipuser\"") || die "Could not add user to MySQL database\n";
    }
    if ($isindb eq '') {
      $sth = $dbh->do("insert into db values \(\"localhost\", \"gnudip2\", \"$gnudipuser\", \"Y\", \"Y\", \"Y\", \"Y\", \"N\", \"N\"\)") || die "Could not add database rule entry\n";
    }

  } elsif ($dbtype = "2") {
    if ($isinuser eq '') {
      $sth = $dbh->do("insert into user \(host, user, password\) values \(\"localhost\", \"$gnudipuser\", password\(\"$gnudippass\"\)\)") || die "Could not add user to MySQL database\n";
    } else {
      $sth = $dbh->do("update user set password = password\(\"$gnudippass\"\) where user = \"$gnudipuser\"") || die "Could not add user to MySQL database\n";
    }
    if ($isindb eq '') {
      $sth = $dbh->do("insert into db values \(\"localhost\", \"gnudip2\", \"$gnudipuser\", \"Y\", \"Y\", \"Y\", \"Y\", \"N\", \"N\", \"N\", \"N\", \"N\", \"N\"\)") || die "Could not add database rule entry\n";
    }

  }

  system("mysqladmin -u$rootmysqluser $mysqlpassstring reload");

  system("sed -e s/#MYSQLUSER#/$gnudipuser/g -e s/#MYSQLPASS#/$gnudippass/g -e s/#DAEMONUSER#/$daemonuser/g -e s/#DAEMONGROUP#/$daemongroup/g < gnudip.conf > tmp1");
 
   system("sed -e s/#PREFIX#/$sedprefix/g < S99gnudip > tmp3");

print "Substituted gnudip username and password into scripts\n";


system("install -m 644 gnudip-lib.pl $perldir/gnudip-lib.pl");
print "Succesfully installed gnudip-lib.pl into $perldir\n";

system("install -m 660 -o $ROOTUSER -g $webgroup tmp1 /etc/gnudip.conf");
print "Succesfully installed gnudip.conf into /etc\n";
unlink("tmp1");

system("mkdir -p $prefix/sbin");
system("install -m 750 -o $ROOTUSER -g $ROOTGROUP gdips.pl $prefix/sbin/gdips.pl");
print "Succesfully installed gdips.pl into $prefix/sbin\n";

system("mkdir -p $prefix/sbin");
system("install -m 755 -o $ROOTUSER -g $ROOTGROUP gdipc.pl $prefix/bin");
print "Succesfully installed gdipc.pl into $prefix/bin\n";


system("install -m 750 -o $webuser -g $webgroup gnudip2.cgi $cgidir/gnudip2.cgi");
print "Succesfully installed gnudip2.cgi into $cgidir\n";

if ($onboot eq "Y") {
  system("install -m 750 -o $ROOTUSER -g $ROOTGROUP tmp3 $initdir/S99gnudip");
  print "Succesfully installed S99gnudip into $initdir\n";
  unlink("tmp3");
} else {
  system("mv tmp3 S99gnudip");
  system("chmod 755 S99gnudip");
}   

print "\nYou may now goto your web server and run the gnudip2.cgi and login as 'admin' with password 'GnuDIP' to configure your settings\n";


$dbh->disconnect;
