#!/usr/bin/perl

# chkconfig: 35 40 30
# config: NONE
# description: Linux Base script that mount necessary directories during startup
#              and un-mount during reboot. This script is running before the
#              linuxbase_srv.

## @file mount_srv
# @brief Linux Base script that mount directories required for SFTP-jail and
#        UCM accounts during startup and un-mount during reboot.
# @author Evgeny Lazarev
# @version 1.00 - Initial version

use lib "/opt/nortel/base/lib/";

use strict;
use warnings;

use common_variables;
use common_functions;
use baselog;
use base_system;
use base_ftp;

my $SRV_NAME  = 'mount_srv';
my $lock_file = "/var/lock/subsys/$SRV_NAME";
my $start_msg = "Starting $SRV_NAME\n";
my $stop_msg  = "Stopping $SRV_NAME\n";
my $retval    = $OK;

## @skipline
sub mount_srv();
## @skipline
sub default_message();
## @skipline
sub do_start();
## @skipline
sub do_stop();
## @skipline
sub enable_sftp_mounts();
## @skipline
sub disable_sftp_mounts();
## @skipline
sub prepare_sftp_jail_template();

exit(mount_srv());

###############################################################################
## @fn mount_srv()
# @brief Main function of this script. Parses input parameters and calls
#        appropriate function.
# @return
# @li $OK    - all operations successfully completed
# @li $ERROR - some operations failed
# @author Evgeny Lazarev
sub mount_srv()
{
   if (warning_if_not_root_uid())
   {
      return $ERROR;
   }

   if (!defined @ARGV)
   {
      default_message();
      return $OK;
   }

   baselog($LOG_DEBUG, join(" ", @ARGV));
   baselog($LOG_INFO, "$SRV_NAME started");

   if ($ARGV[0] eq $SRV_START)
   {
      $retval = do_start();
   }
   elsif ($ARGV[0] eq $SRV_STOP)
   {
      $retval = do_stop();
   }
   else
   {
      default_message();
      $retval = $ERROR;
   }

   baselog($LOG_INFO, "$SRV_NAME finished");
   return $retval;
} ## end sub mount_srv()

###############################################################################
## @fn do_start()
# @brief Performs actions for 'start' action
# @return $OK - success
# @return $ERROR - any failure occured
# @author Evgeny Lazarev
sub do_start()
{
   my $ret = $OK;

   print $start_msg;

   # Prepare SFTP-jail template
   $ret |= prepare_sftp_jail_template();

   # enable mount points required for SFTP connections if this is not a first
   # boot up. If it is a first boot up then there is no any information about
   # required directories.
   if (!-f $BASEFIRSTBOOTUP)
   {
      $ret |= enable_sftp_mounts();
   }

   if ($ret == $OK)
   {
      do_system([$TOUCH, $lock_file]);
      status_success($start_msg);
   }
   else
   {
      status_failure($start_msg);
   }

   return $ret;
} ## end sub do_start()

###############################################################################
## @fn do_stop()
# @brief Performs actions for 'stop' action
# @return $OK - success
# @return $ERROR - any failure occured
# @author Evgeny Lazarev
sub do_stop()
{
   my $ret = $OK;

   print $stop_msg;

   # disable mount points required for SFTP connections
   $ret |= disable_sftp_mounts();

   if (!unlink($lock_file))
   {
      baselog($LOG_ERROR, "Cannot remove '$lock_file' lock file.");
   }

   if ($ret == $OK)
   {
      status_success($stop_msg);
   }
   else
   {
      status_failure($stop_msg);
   }

   return $ret;
} ## end sub do_stop()

###############################################################################
## @fn enable_sftp_mounts()
# @brief Enables SFTP mount points
# @return OK - operation successful
# @return ERROR - some error happened
# @author Evgeny Lazarev
sub enable_sftp_mounts()
{
   my $ret = $OK;
   if (!base_ftp::mount_sftp_jail_dirs())
   {
      $ret = $ERROR;
      baselog($LOG_ERROR, "Cannot mount directories required for SFTP-jail.");
   }
   if (!base_ftp::mount_UCM_dirs())
   {
      $ret = $ERROR;
      baselog($LOG_ERROR, "Cannot mount additional UCM users directories.");
   }
   if (!base_ftp::activate_sftp_mounts())
   {
      $ret = $ERROR;
      baselog($LOG_ERROR, "Cannot activate SFTP mount points.");
   }
   return $ret;
} ## end sub enable_sftp_mounts()

###############################################################################
## @fn disable_sftp_mounts()
# @brief Disables SFTP mount points
# @return OK - operation successful
# @return ERROR - some error happened
# @author Evgeny Lazarev
sub disable_sftp_mounts()
{
   my $retval = $OK;
   if (!base_ftp::umount_sftp_jail_dirs())
   {
      $retval = $ERROR;
      baselog($LOG_ERROR, "Cannot umount directories required for SFTP-jail.");
   }
   if (!base_ftp::umount_UCM_dirs())
   {
      $retval = $ERROR;
      baselog($LOG_ERROR, "Cannot umount additional UCM users directories.");
   }
   if (!base_ftp::deactivate_sftp_mounts())
   {
      $retval = $ERROR;
      baselog($LOG_ERROR, "Cannot deactivate SFTP mount points.");
   }
   return $retval;
} ## end sub disable_sftp_mounts()

###############################################################################
## @fn prepare_sftp_jail_template()
# @brief Prepare SFTP-jail template
# @return OK - operation successful
# @return ERROR - some error happened
# @author Evgeny Lazarev
sub prepare_sftp_jail_template()
{
   my $ret = $OK;
   if (!do_system([$CREATE_JAIL_TEMPLATE_UTIL, $JAILTEMPLATEDIR]))
   {
      baselog($LOG_ERROR, "SFTP-jail template creation failed.");
      $ret = $ERROR;
   }
   return $ret;
} ## end sub prepare_sftp_jail_template()

###############################################################################
## @fn default_message()
# @brief prints usage help.
# @author Evgeny Lazarev
sub default_message()
{
   print "Usage: $SRV_NAME {$SRV_START|$SRV_STOP\n";
}

