#!/bin/sh

M_START_DBS_TCP='Linter TCP/IP listener provides the communication between the database server and
client applications via TCP/IP protocol (Internet/Intranet).'
Q_START_DBS_TCP='Do you want to start Linter TCP/IP listener automatically during Linter start?'

M_START_JDBC='Linter JDBC listener allows client applications written in JAVA to communicate
with the database server via the JDBC interface.'
Q_START_JDBC='Do you want to start Linter JDBC listener automatically when Linter starts?'

Q_S_DATABASE_DIR="Enter database directory name"
M_S_BAD_DATABASE_DIR="Database directory name is not valid."


M_START_DBC_TCP="During Linter initialization the Linter network agent can be started.
Linter network agent provides the communication between user application and
remote Linter database server via TCP/IP network."
Q_START_DBC_TCP="Start Linter network agent?"

M_START_SNMPD="You can start the SNMP agent for remote control the Linter components"
Q_START_SNMPD="Do you want to start SNMP agent automatically when Linter starts?"

#Linter not shut down correctly or already running
M_RUNNING_ALREADY='Linter is already running.'
#Error creating locking directory
M_ERROR_LOCK='Lock Directory creation error'
#Error deleting locking directory
M_ERROR_LOCK_REMOVE='Lock Directory remove error'
#Linter not running
M_LINTER_NOT_RUN='Linter is not running'
#Install RDBMS Linter SQL initialization script into $INIT_D_DIR/linter.sh
M_SS_SCRIPT_INTO='The Linter start/stop procedure script has been copied into
$LINTER_DIR/bin/start_linter.sh and $LINTER_DIR/bin/stop_linter.sh .
You can modify the start/stop procedure script using a text editor.'

M_AUTO_BACKUP='A daily database backup (lhb) process is recommended.
The database backup files can be redirected to a selected directory.'
Q_AUTO_BACKUP='Set daily database backup automatically?'

M_DIR_BACKUP='The database backup directory contains backup files
so you must have a write access to it. The file system should have enough 
space to complete the database backup. If the database backup directory name 
field is empty, database backup will not proceed.'
Q_DIR_BACKUP='Enter the database backup directory name'
#Incorrect backup directory!
M_CREATE_DIR_BACKUP='The database backup directory name is not valid.'
Q_CREATE_DIR_BACKUP='Create directory $ARCDEVICE?'
#error creating directory
M_ERR_CRE_DIR_BACK='Directory $ARCDEVICE creation error!'
#Database backup will be performed once in 24 hours
M_TIME_BACKUP='Database backup time is completed daily based on GMT.'
Q_TIME_BACKUP='Enter the database backup time (in GMT) using the format HH:MI '
#'Incorrect time format!'
M_INCORRECT_TIME='The time entered is not valid.'
#Enter the number of database backup copies
M_NUM_BACKUP='Maximum number of database backup files to be stored.
New database backup files will be created weekly.'
Q_NUM_BACKUP='Enter the maximum number of weekly database backup files stored'
#Incorrect count! 0< count <=9 '
M_BAD_NUM='Number of files should be between 1 and 9.'
#echo "Script installed successfully. Reboot the system. Press enter to continue"
M_PATH_BACKUP='The Linter backup script has been copied into $LINTER_DIR/bin/backup.bsl'
LININST_M_150='RDBMS Linter SQL has been installed successfully!!
To start RDBMS Linter SQL, please reboot the system after completion of the
installation.
Press <Enter> to continue installation.'
M_M_INCORRECT_RUN="This procedure must be started from the Linter root directory."

M_PROC_SCRIPT="Create start-stop procedure"

M_ACTIVATE_REPLIC="This database is configured for asynchronous replication to remote database."
Q_ACTIVATE_REPLIC="Do you want to activate asynchronous replication feature?"
M_CREATE_STORAGE="It is possible to use asynchronous replication feature to replicate data 
to/from remote database. Before activating this feature you have to create 
auxiliary database."
Q_CREATE_STORAGE="Do you want to create auxiliary database now?"

[ -f ./scripts/dialog.sh ] || {
    echo "$M_M_INCORRECT_RUN"
    exit 1
    }

. ./scripts/dialog.sh

[ "$INSTALL_LANG" = "" ] && {
  [ -f scripts/lang ] && {
    i=0
    LANGUAGES=`cat scripts/lang`
    for L in "english" $LANGUAGES; do
      i=`expr $i + 1`
      echo "$i - $L"
    done
    echo "Select language please ( from 1 to $i ):"
    read lang
    i=0
    LLL="english"
    for L in "english" $LANGUAGES; do
      i=`expr $i + 1`
      [ "$i" = "$lang" ] && {
        LLL=$L
      }
    done
    [ "$LLL" = "english" ] || {
      INSTALL_LANG=$LLL
      export INSTALL_LANG
    }
  }
}

[ "$INSTALL_LANG" = "" ] || {
  [ -f make_scripts."$INSTALL_LANG" ] && {
    . make_scripts."$INSTALL_LANG"
  }
  [ -f scripts/make_scripts."$INSTALL_LANG" ] && {
    . scripts/make_scripts."$INSTALL_LANG"
  }
}


[ A"$LINTER_DIR" = A ] && {
  LINTER_DIR=`pwd`
  export LINTER_DIR
  init_log "make_scripts.log"
  }

LINTER_MBX=20561
export LINTER_MBX

push_path "$M_PROC_SCRIPT"

[ A"$DATABASE_DIR" = A ] && {
  init_log "make_scripts.log"
  while true; do
    input_value "S_DATABASE_DIR" "$LINTER_DIR/db"
    DATABASE_DIR=$rets
    export DATABASE_DIR
    [ -d "$DATABASE_DIR" ] || {
      say_comment "$M_S_BAD_DATABASE_DIR"
      continue
      }
    break
  done
  }

[ -f "$LINTER_DIR/bin/private_passwd" ] || {
  echo 'USER="SYSTEM"
PASSWORD="MANAGER"
'>"$LINTER_DIR"/bin/private_passwd
  }
chmod og-wr "$LINTER_DIR"/bin/private_passwd

#running dbs_tcp
RUN_DBS_TCP=0
def_yes_no "START_DBS_TCP" Y && RUN_DBS_TCP=1

RUN_DBC_TCP=0
def_yes_no "START_DBC_TCP" N && {
  RUN_DBC_TCP=1
  [ -f "$LINTER_DIR/bin/nodetab" ] || {
    echo "MY TCPIP localhost 1060 1 20 20" > "$LINTER_DIR/bin/nodetab"
    }
  }


RUN_JDBC_SERVER=0
def_yes_no "START_JDBC" Y && RUN_JDBC_SERVER=1

RUN_SNMPD=0
def_yes_no "START_SNMPD" N && RUN_SNMPD=1

IS_QNX=`uname -a| $AWKRUN 'BEGIN{i=0} /qnx/{i=1} /QNX/{i=1} END{print i}'`

LINTER_MBX=20561
NET_MBX=20562
[ $IS_QNX -eq 1 ] && {
 LINTER_MBX="linter"
 NET_MBX="linter_net"
 }

if [ "$PLATFORM" = "android" ]; then
  LINTER_DIR_SAVE="/cache/linter"
else
  LINTER_DIR_SAVE=$LINTER_DIR
fi

echo '
LINTER_DIR="'$LINTER_DIR_SAVE'"
LINTER_BIN="$LINTER_DIR"/bin
LINTER_MBX='$LINTER_MBX'
POOL=1000
SPOOL=500
SY00="'$DATABASE_DIR'"

RUN_DBS_TCP='"$RUN_DBS_TCP"'
NET_PORT=1060

RUN_DBC_TCP='"$RUN_DBC_TCP"'
NODETAB_FILE="$LINTER_BIN"/nodetab
NET_MBX='$NET_MBX'
DBC_PID_FILE="$LINTER_BIN"/dbc_tcp.pid

RUN_JDBC_SERVER='"$RUN_JDBC_SERVER"'
JDBC_SERVER_PORT=1070

RUN_SNMPD='"$RUN_SNMPD"'
SNMPD_PID_FILE="$LINTER_BIN"/linsnmpd.pid
SNMP_CONF_FILE="$LINTER_DIR"/snmp/snmp.conf

LHB_PID_FILE="$LINTER_BIN"/lhb.pid
LHB_LOG_FILE="$SY00"/lhb.log
' >"$LINTER_DIR"/bin/config_lin.sh

echo '#!/bin/sh

LINTER_DIR="'$LINTER_DIR_SAVE'"

. "$LINTER_DIR"/bin/config_lin.sh

export LINTER_MBX NET_MBX

. "$LINTER_BIN"/private_passwd

is_running()
    {
    LINTER_NET_MBX=$NET_MBX
    NET_MBX=$LINTER_MBX
    export NET_MBX

    "$LINTER_BIN"/chklinter -r -u "\"$USER\""/"\"$PASSWORD\""
    rets=$?

    NET_MBX=$LINTER_NET_MBX
    export NET_MBX
    return $rets;
    };

cd "$SY00"

is_running && {
  echo "'"$M_RUNNING_ALREADY"'"
  exit 1
  }


TCP_OPT=""
[ -x "$LINTER_BIN"/dbs_tcp -a $RUN_DBS_TCP -eq 1 ] && {
  TCP_OPT="/TCP=$NET_PORT"
  }
      
JDBC_OPT=""
[ -x "$LINTER_BIN"/linapid -a $RUN_JDBC_SERVER -eq 1 ] && {
    JDBC_OPT="/JDBCS=\"$LINTER_DIR\"/jadm /JDBCP=$JDBC_SERVER_PORT"
    }
      
"$LINTER_BIN"/linter /BASE="$SY00" /POOL=$POOL /SPOOL=$SPOOL $TCP_OPT ${JDBC_OPT}
    
for i in 3 5 10 15; do
  sleep $i
  if is_running; then
    break
  fi
done

[ -f "$LHB_PID_FILE" ] && {
  kill `cat "$LHB_PID_FILE"`
  rm -f "$LHB_PID_FILE"
  }

[ -f "$LINTER_BIN"/backup.bsl -a -x "$LINTER_BIN"/lhb ] && {
  "$LINTER_BIN"/lhb script -ft "$LINTER_BIN"/backup.bsl -fl "$LHB_LOG_FILE" -mypid "$LHB_PID_FILE" -bg > /dev/null
  }

[ -f "$DBC_PID_FILE" ] && {
  kill `cat "$DBC_PID_FILE"`
  rm -f "$DBC_PID_FILE"
  }

[ $RUN_DBC_TCP -ne 0 -a -x "$LINTER_BIN"/dbc_tcp ] && {
  "$LINTER_BIN"/dbc_tcp /p="$DBC_PID_FILE" /n="$NODETAB_FILE"
  }

[ $RUN_SNMPD -ne 0 -a -x "$LINTER_BIN"/linsnmpd ] && {
  "$LINTER_BIN"/linsnmpd -c "$SNMP_CONF_FILE" -p "$SNMPD_PID_FILE"
  }

[ -x "$LINTER_BIN"/async_start.sh -a "$NO_AR"A = A ] && "$LINTER_BIN"/async_start.sh

exit 0
' > "$LINTER_DIR"/bin/start_linter.sh

echo '#!/bin/sh

LINTER_DIR="'$LINTER_DIR_SAVE'"

. "$LINTER_DIR"/bin/config_lin.sh

export LINTER_MBX NET_MBX

. "$LINTER_BIN"/private_passwd

is_running()
    {
    LINTER_NET_MBX=$NET_MBX
    NET_MBX=$LINTER_MBX
    export NET_MBX

    "$LINTER_BIN"/chklinter -r -u "\"$USER\""/"\"$PASSWORD\""
    rets=$?

    NET_MBX=$LINTER_NET_MBX
    export NET_MBX
    return $rets;
    };

[ -x "$LINTER_BIN"/async_stop.sh -a "$NO_AR"A = A ] && "$LINTER_BIN"/async_stop.sh

cd "$SY00"
is_running || {
  echo "'"$M_LINTER_NOT_RUN"'"
  exit 1
  }


[ -f "$DBC_PID_FILE" ] && {
  kill `cat "$DBC_PID_FILE"`
  rm -f "$DBC_PID_FILE"
  }

"$LINTER_BIN"/shut -u "\"$USER\""/"\"$PASSWORD\"" -r &

for i in 1 3 5 10 20; do
  if is_running;then
    sleep $i
  fi            
done

if [ -f "$LHB_PID_FILE" ]; then
  kill `cat "$LHB_PID_FILE"`
  rm -f "$LHB_PID_FILE"
fi

if [ -f "$SNMPD_PID_FILE" ]; then
  kill `cat "$SNMPD_PID_FILE"`
  rm -f "$SNMPD_PID_FILE"
fi

' > "$LINTER_DIR"/bin/stop_linter.sh

#path to Script
chmod a+x "$LINTER_DIR/bin/start_linter.sh"
chmod a+x "$LINTER_DIR/bin/stop_linter.sh"

say_comment "$M_SS_SCRIPT_INTO"



[ -f "$LINTER_DIR"/bin/backup.bsl ] && rm -f "$LINTER_DIR"/bin/backup.bsl

#install backup script
#def_yes_no Y "Do you want to start the database backup process?" && {
def_yes_no "AUTO_BACKUP" Y && {

  while true ; do
    #backup directory
    input_value "DIR_BACKUP" "$LINTER_DIR/arc"
    ARCDEVICE="$rets"
    [ -d "$ARCDEVICE" -o "$ARCDEVICE" = "" ] && break
#  echo 'Incorrect backup directory!'
#  def_yes_no Y "create directory $ARCDEVICE?" || continue
    def_yes_no "CREATE_DIR_BACKUP" Y || continue
    mkdir "$ARCDEVICE" || {
      say_comment "$M_ERR_CRE_DIR_BACK"
      continue
      }
    [ -d "$ARCDEVICE" ] && break
  done

  [ "$ARCDEVICE" = "" ] && {
    pop_path
    exit
    }

  while true; do
    #Time of backup
    input_value "TIME_BACKUP"
    TIME_LHB="$rets"
    tmp=`echo "$TIME_LHB" | awk ' /[0-9][0-9]:[0-9][0-9]/ {print $0}'`
    [ ! -z "$tmp" ] && break
#   echo 'Incorrect format time!'
    say_comment "$M_INCORRECT_TIME"
  done

  while true; do
#  echo 'Enter the number of database backup copies:'
  echo "$LININST_M_130"
  echo "$LININST_Q_130"
  input_value "NUM_BACKUP" "4"
  COUNT_ARC=$rets
  tmp=`echo "$COUNT_ARC" | awk ' /[0-9]/ {print $0}'`
  [ A"$tmp" != A ] && break
#  echo 'Incorrect count! 0< count <=9 '
  say_comment "$M_BAD_NUM"
  done

  [ "$COUNT_ARC" -eq 0 ] && {
    pop_path
    exit
    }

  echo 'Variables:
  USERNAME=     "SYSTEM";    /* user name */
  USERPASSWORD= "MANAGER";   /* user password */
  NODE=         "";
  ARCDEVICE=    "'$ARCDEVICE'";        /* for new files */
  COMMAND      = "";
  i = 0;
/*--------------------------------------------------------*/
Rights:
Everyday ( time = '"'$TIME_LHB'"' )
  {
  if ( (CDAY() == 7) OR NOT exist(ARCDEVICE+"/linter.lhb.0") )
    {
    if (exist(ARCDEVICE+"/linter.lhb.0"))
      backup ( COMMAND +" -stopinc" );
    i = '$COUNT_ARC';
    if ( exist(ARCDEVICE+"/linter.lhb."+tostr(i)) )
      delete(ARCDEVICE+"/linter.lhb."+tostr(i));
    while ( i > 0)
      {
      if (exist(ARCDEVICE+"/linter.lhb."+tostr(i-1)))
        rename(ARCDEVICE+"/linter.lhb."+tostr(i-1) , ARCDEVICE+"/linter.lhb."+tostr(i));
      i = i - 1;
      }
    backup ( COMMAND +" -startinc" );
    }
  else
    {
    backup ( COMMAND +" -inc");
    }
  logprint ( CTIMESTAMP() + " --- File " + "linter.lhb.0" +
             " created." );
Exception: /* for everyday */
  print (  "Error=" + TOSTR(CERROR) +
           " , LinError=" + TOSTR(LINERROR) +
           " , SysError=" + TOSTR(SYSERROR) );
  logprint ( CTIMESTAMP() + " --- Error=" + TOSTR(CERROR) +
                            " , LinError=" + TOSTR(LINERROR) +
                            " , SysError=" + TOSTR(SYSERROR) );
  stop;
  } /* Everyday */
/*--------------------------------------------------------*/
Special:
before /* just after the start */
  {
  if(NODE == "")
    {
    COMMAND = "s -u "+USERNAME+"/"+USERPASSWORD+" -f \""+ARCDEVICE+"/linter.lhb.0\" -qc DF";
    }
  else
    {
    COMMAND = "s -u "+USERNAME+"/"+USERPASSWORD+" -n "+NODE+" -f \""+ARCDEVICE +"/linter.lhb.0\" -qc DF";
    }
  }
after /* after stop or Ctrl-C */
        {
  print ( " --- Stop backup system" );
  if ( CERROR != 0 )
    logprint ( CTIMESTAMP() + " --- Error present: " + TOSTR(CERROR) );
  logprint ( CTIMESTAMP() + " --- Stop backup system" );
  }
iferr /* global */
  {
  print (  "Error=" + TOSTR(CERROR) +
           " , LinError=" + TOSTR(LINERROR) +
           " , SysError=" + TOSTR(SYSERROR) );
  logprint ( CTIMESTAMP() + " --- Error=" + TOSTR(CERROR) +
                            " , LinError=" + TOSTR(LINERROR) +
                            " , SysError=" + TOSTR(SYSERROR) );
  stop;
  }

'> "$LINTER_DIR"/bin/backup.bsl
  chmod go-wr "$LINTER_DIR"/bin/backup.bsl
  say_comment "$M_PATH_BACKUP"
  } # Backup

[ -x "$LINTER_DIR"/bin/lreplserver -a -x "$LINTER_DIR"/db/make_storage ] && {
  [ -f "$DATABASE_DIR"/storage/1.01 ] ||   {
    def_yes_no "CREATE_STORAGE" Y && {
      cd "$DATABASE_DIR"
      "$LINTER_DIR"/db/make_storage
      cd "$LINTER_DIR"
      }
    }
  }
[ -x "$LINTER_DIR"/scripts/make_async_scripts -a -f "$DATABASE_DIR/storage/1.01" ] && def_yes_no "ACTIVATE_REPLIC" Y && "$LINTER_DIR"/scripts/make_async_scripts

pop_path
