#!/usr/bin/perl -w
#
#  lcm-firstboot: call lcm with pre-existing local lcm-profile
#                 after user selected options on firstboot
#                 screen 
#                  
#                 should be run ONLY after firstboot and 
#                 ONLY for initial setup
#
# Jaroslaw Polok <Jaroslaw.Polok@cern.ch>
# 
#
# needs error checking .... really ....

$| = 1;

use Getopt::Std;

$lcm='/usr/sbin/lcm --configure';
$timeoutlcm='/usr/sbin/lcm_timeout';
$afs=$update=$comps=$quattorbs=0;$quattorargs=$1;
$cfgfile='/etc/sysconfig/lcm-firstboot';
$opt_f = undef;
$predefcomps="chkconfig sendmail srvtab krb5clt ntpd ssh ocsagent";


getopts('f');

if ($< != 0) { print "$0 can be run only by  root\n";
		exit 1; }

if ($opt_f) {
} else {
 print "lcm-firstboot: initial system setup from values\n";
 print "               obtained via firstboot.\n";
 print "\n";
 print "Written by Jaroslaw Polok <Jaroslaw.Polok\@cern.ch>\n";
 print "-----------------------------------------------------\n";
 print "This program should be called only from init.d script\n";
 print "Cowardly refusing to run ;-)...\n";
 exit 1;
}

if ( -f $cfgfile ) {

open(FH,"$cfgfile");
while (<FH>) {
 s/LCM_FIRSTBOOT_START=yes/LCM_FIRSTBOOT_START=no/;
 if (/LCM_FIRSTBOOT_STARTAFS=yes/) {$afs=1;};
 if (/LCM_FIRSTBOOT_UPDATE=(\d)/) {$update=1;};
 if (/LCM_FIRSTBOOT_YUMUPDATE=(\d)/) {$update=1;};
 if (/LCM_FIRSTBOOT_COMPONENTS=1/) {$comps=1;};
 if (/LCM_QUATTOR_BOOTSTRAP=(\.*)/) {$quattorbs=1;$quattorargs=$1;};
 if (/LCM_FIRSTBOOT_GDMINITUSER=0/) {$gdmconfhack=1;};
 push(@ct,$_);
}
close(FH);

open(FH,">$cfgfile");
foreach $l (@ct) {
print FH $l;
}
close(FH);

print "\n";

if($afs) { 
  print "* Runnning AFS client configuration...\n";
  print "\n";
  system("$lcm afsclt");
  system("/usr//bin/systemctl enable openafs-client");
  system("/sbin/modrpobe openafs");
  system("/usr/bin/systemctl start openafs-client");
#  if ( -d "/afs/cern.ch" ) {
#     system("$lcm srvtab");    
#     }
}

if($update) {
   print "* Runnning UPDATE system configuration...\n";
   print "\n";
   `echo YUMUPDATE=1 >> /etc/sysconfig/yum-autoupdate`;
   `echo YUMONBOOT=0 >> /etc/sysconfig/yum-autoupdate`;
    system("/usr/bin/systemctl enable yum-autoupdate");
}

if($comps) {
   print "* Runnning LCM components configuration...\n";
   print "\n";
   system("$timeoutlcm $lcm $predefcomps");
   system("/usr/bin/systemctl restart nscd");
   system("/usr/bin/systemctl restart ntpd");
}

if($gdmconfhack) {
   print "* Running GDM config setup...\n";
   print "\n";
   if(open(FH2,">","/etc/gdm/custom.conf")) {
     print FH2 "# GDM configuration storage\n";
     print FH2 "# Modified by lcm-firstboot\n";
     print FH2 "[daemon]\n# added\nInitialSetupEnable=False\n\n";
     print FH2 "[security]\n# added\nAllowRoot=true\n\n";
     print FH2 "[xdmcp]\n\n[greeter]\n\n[chooser]\n\n[debug]\n\n";
     close(FH2);
   }	
}


if($quattorbs) {
 system("/usr/sbin/quattorbs $quattorargs > /tmp/quattorbs.log 2>&1");
}

} # if ( -f $cfgfile )

exit 0;
