#!/bin/bash

# filecloud -- Enterprise File Sharing, Sync and Mobile Access for Business v21.3.0.18447

bindir='/usr/bin'
case $1 in
--__bindir) bindir=${2?}; shift; shift;;
esac
PATH=$bindir:$PATH
usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
Look for instances of PATTERN in the input FILEs, using their
uncompressed contents if they are compressed."

usage()
{
    echo "usage: filecloud $arguments"
    echo "   ";
    echo "  -v | --version                 : Show the current version of FileCloud installed";
    echo "  -c | --check-upgrade           : Checks for FileCloud Upgrades";
    echo "  -u | --run-upgrade             : Upgrades FileCloud Version";
    echo "  -r | --reset-password          : Performs FileCloud Admin Password";
    echo "  -s | --install-solr            : Installs FileCloud Solr Service";
    echo "  -p | --install-preview         : Installs Filecloud Preview service";
    echo "  -l | --libreoffice-check       : Checks for Libreoffice version upgrades";
    echo "  -t | --install-tesseract       : Installs Filecloud Tesseract service";
    #echo "  -m40 | --upgrade-mongodb40     : Upgrades MongoDB to 4.0";
   # echo "  -m42 | --upgrade-mongodb42     : Upgrades MongoDB to 4.2";
    echo "  -h | --help                    : This message";
}

write_log()
{
  while read text
  do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
LOG=/var/log/$(basename -- "$0" .sh ).log
if [[ ! -e "$LOG" ]] ; then
         touch ${LOG}
         chmod 744 ${LOG}
fi
     echo ${LOGTIME}": $text" | tee -a ${LOG};

  done
}
checkupgradefile()
{
if [[ -d /var/www/html/src ]];then

cat > /usr/local/src/checkupgrade.php << \EOF
<?php
define('TONIDO_CLOUD_ROOT_DIR', realpath('/var/www/html'));
 require_once(TONIDO_CLOUD_ROOT_DIR . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'runtime.php');

use CodeLathe\Core\Subsystem\System\Install\UpdateCheckManager;
   $local = "0.0.0.0";
            $remote = "0.0.0.0";
                $fcheck = 1;

            $infoArray = array();
            $retcode = UpdateCheckManager::isBuildUptodate($local,$remote,$infoArray, $fcheck);
             if ($retcode === 1) // up to date
                {
                   echo "You are running the latest version of Filecloud v$local";
                }
                else if ($retcode === 2) // update available
                {

                    echo "New Filecloud Upgrade is available to v$remote";
                    $updatestring = '2';
                                        file_put_contents('/usr/local/src/upgrade_available.txt', $updatestring);
                }
                else if ($retcode === 0) // error
                {
                    echo "ERROR";
                }


EOF
else
cat > /usr/local/src/checkupgrade.php << \EOF
<?php
define('TONIDO_CLOUD_ROOT_DIR', realpath('/var/www/html'));
 require_once(TONIDO_CLOUD_ROOT_DIR . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'runtime.php');


   $local = "0.0.0.0";
            $remote = "0.0.0.0";
                $fcheck = 1;

            $infoArray = array();
            $retcode = \core\framework\UpdateCheckManager::isBuildUptodate($local,$remote,$infoArray, $fcheck);
             if ($retcode === 1) // up to date
                {
                   echo "You are running the latest version of Filecloud v$local";
                }
                else if ($retcode === 2) // update available
                {

                    echo "New Filecloud Upgrade is available to v$remote";
                    $updatestring = '2';
                                        file_put_contents('/usr/local/src/upgrade_available.txt', $updatestring);
                }
                else if ($retcode === 0) // error
                {
                    echo "ERROR";
                }


EOF
fi
}
libreoffice_check()
{
if ! [ -x "$(command -v libreoffice)" ]; then
	  echo 'Libreoffice is not installed.' 2>&1 | write_log > /dev/null
	  else
	libreoffice_upgrade
	fi
}
#DEPENDENCY CHECKS
##################################
dependency_check_rpm()
{
if ! [[ -x "$(command -v unzip)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
yum install unzip lsof -y
yum remove java* -y
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v wget)" ]]; then
echo "Wget not installed" 2>&1 | write_log
yum install wget -y
echo "Installing Wget" 2>&1 | write_log
else
echo "Wget Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
yum install java-1.8.0-openjdk -y
echo "Installing Java"
else
echo "Java Installed"
fi
}

dependency_check_deb()
{
if ! [ -x "$(command -v unzip)" ]; then
echo "Unzip not installed"
apt-get install unzip -y
apt-get purge --auto-remove oracle-java* openjdk-8-jre java* -y
echo "Installing Unzip"
else
echo "Unzip Installed"
fi
if ! [ -x "$(command -v wget)" ]; then
echo "Wget not installed"
apt-get install wget -y
echo "Installing Wget"
else
echo "Wget Installed"
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
sudo apt-get install software-properties-common  -y
apt-get install openjdk-8-jre-headless -y
echo "Installing Java"
fi
}
removeupgradefile()
{
rm -rf /usr/local/src/checkupgrade.php
if [ -e /usr/local/src/upgrade_available.txt ]; then
rm -rf /usr/local/src/upgrade_available.txt
fi
}
if [  $# -ne 1 ]; then
usage
exit 1
 fi

checkupgrade(){
checkupgradefile
whiptail --title "FileCloud Control Panel" --textbox /dev/stdin 10 100 <<<"$(/usr/bin/php -f /usr/local/src/checkupgrade.php | sed -e 's/^[ \t]*//' | tr -d '\r')"
removeupgradefile
}
libreoffice_upgrade()
{

LIBRE_VERSION=$(libreoffice --version | awk '{print $2}' )
REQ_VERSION="7.4.1.2"

if [ "$(printf "$REQ_VERSION\n$LIBRE_VERSION" | sort -V | head -n1)" == "$LIBRE_VERSION" ] && [ "$LIBRE_VERSION" != "$REQ_VERSION" ];then

  echo    "LibreVersion is $LIBRE_VERSION"
  if [ -n "$(command -v apt-get)" ]; then

 apt-get purge libreoffice* -y
 unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice
  sudo mkdir -p /usr/local/src/libreoffice 2>&1
sudo apt-get install libxinerama1 libcairo2 libglu1-mesa libcups2 libsm6 libnss3 -y 2>&1
cd /usr/local/src/libreoffice
                        {
    URL1="https://patch.codelathe.com/tonidocloud/live/libreoffice/LibreOffice_7.4.1_Linux_x86-64_deb.tar.gz"

sudo wget -N "$URL1" -O LibreOffice.tar.gz 2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading FileCloud DOCCONVERTOR" 7 60 0
sudo tar xvf LibreOffice.tar.gz 2>&1
cd LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS
echo "Installing LibreOffice ..." 2>&1
set +e
sudo dpkg -R --install /usr/local/src/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS/ 2>&1
set -e
echo "Removing LibreOffice temp files" 2>&1
ln -sfn /usr/local/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1

 fi
 if [ -n "$(command -v yum)" ]; then
  yum remove libreoffice* -y
   set -euo pipefail
unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice

# Grab LibreOffice 6.0.1 -- version in centos repo does not work.
mkdir -p /tmp/libreoffice 2>&1
yum install libXinerama cairo cups-libs -y 2>&1
cd /tmp/libreoffice

                        {
    URL2="https://patch.codelathe.com/tonidocloud/live/libreoffice/LibreOffice_7.4.1_Linux_x86-64_rpm.tar.gz"

wget -N "$URL2" -O LibreOffice.tar.gz 2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading FileCloud DOCCONVERTOR" 7 60 0
tar xvf LibreOffice.tar.gz 2>&1
cd /tmp/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS 2>&1
echo "Installing LibreOffice ..." 2>&1
set +e
rpm -ivh /tmp/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS/*.rpm 2>&1
set -e
ln -sfn /usr/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1
echo "Removing LibreOffice temp files" 2>&1
rm -rvf /tmp/libreoffice
fi
 else
      echo "Filecloud Preview Service is Installed and LibreVersion is $LIBRE_VERSION"  2>&1


 fi

 }

install_tesseract()
 {
  if ! [ -x "$(command -v tesseract)" ]; then
curl -L https://patch.codelathe.com/tonidocloud/live/3rdparty/tesseract/install_tesseract.sh --output /tmp/install_tesseract.sh --silent && bash /tmp/install_tesseract.sh
rm -rf /tmp/install_tesseract.sh
     else
echo "Tesseract is already installed and configured"
fi
 }
compile_tesseract()
{
 if ! [ -x "$(command -v tesseract)" ]; then
curl -L https://patch.codelathe.com/tonidocloud/live/3rdparty/tesseract/tesseract_compiler.sh --output /tmp/tesseract_compiler.sh --silent && bash /tmp/tesseract_compiler.sh
rm -rf /tmp/tesseract_compiler.sh
else
echo "Tesseract is already installed and configured"
fi
}
install_preview()
{
if ! [ -x "$(command -v libreoffice)" ]; then
echo -e ""
echo -e "###############################"
echo -e "### INSTALLING DOCCONVERTOR ###"
echo -e "###############################"


######UBUNTU INSTALL STARTS##########

if [ -n "$(command -v apt-get)" ]; then

sudo mkdir -p /usr/local/src/libreoffice 2>&1
sudo apt-get install libxinerama1 libcairo2 libglu1-mesa libcups2 libsm6 libnss3 -y 2>&1
dependency_check_deb
cd /usr/local/src/libreoffice
                        {
     URL1="https://patch.codelathe.com/tonidocloud/live/libreoffice/LibreOffice_7.4.1_Linux_x86-64_deb.tar.gz"

sudo wget -N "$URL1" -O LibreOffice.tar.gz 2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading FileCloud DOCCONVERTOR" 7 60 0
sudo tar xvf LibreOffice.tar.gz 2>&1
cd /usr/local/src/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS
echo "Installing LibreOffice ..." 2>&1
set +e
sudo dpkg -R --install /usr/local/src/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS/ 2>&1
set -e
echo "Removing LibreOffice temp files" 2>&1
rm -rf /usr/local/src/libreoffice
ln -sfn /usr/local/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1
echo "Cleaning and removing old FCDocconverter" | write_log
systemctl disable fcdocconverter | write_log

rm -rf /etc/systemd/system/fcdocconverter.service
rm -rf  /opt/fcdocconverter/
systemctl daemon-reload 2>&1 | write_log
echo "Downloading FileCloud Document Converter wrapper.." 2>&1 | write_log
mkdir -p /opt/app/fcdocconverter/
curl --location 'https://patch.codelathe.com/tonidocloud/live/3rdparty/fcdocconverter/FCDocConverter.jar' -o /opt/app/fcdocconverter/FCDocConverter.jar 2>&1 | write_log
 echo "Creating systemd service unit for FCDocConverter.." 2>&1 | write_log
 cat > /etc/systemd/system/fcdocconverter.service <<EOL
        [Unit]
        Description=FileCloud Doc Converter service
        After=network.target httpd.service

        [Service]
        Type=simple
        ExecStart=/bin/sh -c '/usr/bin/env  HOME=/var/www/html  java -Djava.library.path="/usr/lib/libreoffice/program/" -jar /opt/app/fcdocconverter/FCDocConverter.jar'
        WorkingDirectory=/opt/app/fcdocconverter
        PrivateTmp=no
        User=www-data
        Restart=always
        SuccessExitStatus=143

        [Install]
        WantedBy=multi-user.target
EOL
echo "Enabling and starting fcdocconverter." 2>&1 | write_log
systemctl daemon-reload 2>&1 | write_log
systemctl enable fcdocconverter 2>&1 | write_log
systemctl restart fcdocconverter 2>&1 | write_log
fi
######UBUNTU INSTALL ENDS##########


######CENTOS/RHEL INSTALL STARTS##########

        if [ -n "$(command -v yum)" ]; then
                 set -euo pipefail

# Grab LibreOffice 6.0.1 -- version in centos repo does not work.
mkdir -p /usr/local/src/libreoffice 2>&1
yum install libXinerama cairo cups-libs -y 2>&1
dependency_check_rpm
cd /usr/local/src/libreoffice

                        {
    URL2="https://patch.codelathe.com/tonidocloud/live/libreoffice/LibreOffice_7.4.1_Linux_x86-64_rpm.tar.gz"

wget -N "$URL2" -O LibreOffice.tar.gz 2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading FileCloud DOCCONVERTOR" 7 60 0
tar xvf LibreOffice.tar.gz 2>&1
cd /usr/local/src/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS 2>&1
echo "Installing LibreOffice ..." 2>&1
set +e
rpm -ivh /usr/local/src/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS/*.rpm 2>&1
set -e
ln -sfn /usr/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1
echo "Removing LibreOffice temp files" 2>&1
rm -rf /usr/local/src/libreoffice 2>&1
echo "Downloading FileCloud Document Converter wrapper.." 2>&1 | write_log
mkdir -p /opt/app/fcdocconverter/
curl --location 'https://patch.codelathe.com/tonidocloud/live/3rdparty/fcdocconverter/FCDocConverter.jar' -o /opt/app/fcdocconverter/FCDocConverter.jar 2>&1 | write_log
 echo "Creating systemd service unit for FCDocConverter.." 2>&1 | write_log
 cat > /etc/systemd/system/fcdocconverter.service <<EOL
        [Unit]
        Description=FileCloud Doc Converter service
        After=network.target httpd.service

        [Service]
        Type=simple
        ExecStart=/bin/sh -c '/usr/bin/env  HOME=/var/www/html  java -Djava.library.path="/usr/lib/libreoffice/program/" -jar /opt/app/fcdocconverter/FCDocConverter.jar'
        WorkingDirectory=/opt/app/fcdocconverter
        PrivateTmp=no
        User=apache
        Restart=always
        SuccessExitStatus=143

        [Install]
        WantedBy=multi-user.target
EOL
echo "Enabling and starting fcdocconverter." 2>&1 | write_log
systemctl daemon-reload 2>&1 | write_log
systemctl enable fcdocconverter 2>&1 | write_log
systemctl restart fcdocconverter 2>&1 | write_log
fi
######CENTOS/RHEL INSTALL ENDS##########

        else
        libreoffice_upgrade
        fi
}

upgrade_solr()
{
service solr restart
SOLR_VERSION=$(curl -s  http://127.0.0.1:8983/solr/admin/info/system?wt=json  | python -mjson.tool | grep "solr-spec-version" | awk '{print $2}' | sed -e 's/^"//' -e 's/"$//')
if [[ $SOLR_VERSION =~ ^([0-9]+\.?)+$ ]]; then
echo " No Authentication set" | write_log > /dev/null
echo    "SOLR VERSION is $SOLR_VERSION"
echo "$SOLR_VERSION" > /tmp/solr_version
else
echo "Solr has authentication or Service is not responding"
if [ "$(readlink -- "/opt/solr")" = /opt/solr-5.3.1 ]; then
        SOLR_VERSION="5.3.1"
        echo "$SOLR_VERSION" > /tmp/solr_version
        echo    "SOLR VERSION is $SOLR_VERSION" | write_log
fi
if [ "$(readlink -- "/opt/solr")" = /opt/solr-7.6.0 ]; then
        SOLR_VERSION="7.6.0"
        echo "$SOLR_VERSION" > /tmp/solr_version
        echo    "SOLR VERSION is $SOLR_VERSION" | write_log
fi
if [ "$(readlink -- "/opt/solr")" = /opt/solr-8.2.0 ]; then
        SOLR_VERSION="8.2.0"
        echo "$SOLR_VERSION" > /tmp/solr_version
        echo    "SOLR VERSION is $SOLR_VERSION" | write_log
fi
if [ "$(readlink -- "/opt/solr")" = /opt/solr-8.5.0 ]; then
        SOLR_VERSION="8.5.0"
        echo "$SOLR_VERSION" > /tmp/solr_version
        echo    "SOLR VERSION is $SOLR_VERSION" | write_log
fi
if [ "$(readlink -- "/opt/solr")" = /opt/solr-8.8.2 ]; then
        SOLR_VERSION="8.8.2"
        echo "$SOLR_VERSION" > /tmp/solr_version
        echo    "SOLR VERSION is $SOLR_VERSION" | write_log
fi
fi


SOLR_REQ_VERSION="8.11.1"
if [ "$(printf "$SOLR_REQ_VERSION\n$SOLR_VERSION" | sort -V | head -n1)" == "$SOLR_VERSION" ] && [ "$SOLR_VERSION" != "$SOLR_REQ_VERSION" ];then


echo -e ""
echo -e "###############################"
echo -e "### UPGRADING  SOLR         ###"
echo -e "###############################"
service solr stop
echo 'Stopped SOLR Service'
lsof -ti :8983 | xargs --no-run-if-empty kill -9
if [ -n "$(command -v yum)" ]; then
 dependency_check_rpm
fi
if [ -n "$(command -v apt-get)" ]; then
 dependency_check_deb
fi

touch /var/log/filecloudcp_solr_upgrader.log
if [ -f /etc/init.d/solr ]; then
sudo mv /etc/init.d/solr /etc/init.d/solr-oldv
fi
if [ -f /etc/default/solr.in.sh ]; then
mv /etc/default/solr.in.sh /etc/default/solr.in.sh-oldv
fi
if [ -d /opt/solr ]; then
echo "SOLR SYMBLNK EXISTS" >> /var/log/filecloudcp_solr_upgrader.log
unlink /opt/solr >> /var/log/filecloudcp_solr_upgrader.log
fi
sudo mkdir -p /usr/local/src/solrinstall 2>&1 >> /var/log/filecloudcp_solr_upgrader.log

SOLR_URL="https://patch.codelathe.com/tonidocloud/live/3rdparty/solr/solr-8.11.1.tgz"


SOLR_DOWNLOAD_DIR=/usr/local/src/solrinstall
 {
    for ((i = 0 ; i <= 100 ; i+=50)); do
        sleep 1
        echo $i
    done
} | whiptail --gauge "Setting up FileCloud Solr Installer" 7 60 0

echo  "Downloading Solr zip"
cd ${SOLR_DOWNLOAD_DIR}
  {
sudo wget -N "$SOLR_URL"  2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading Latest Solr " 7 60 0

echo  "Downloaded Solr zip" >> /var/log/filecloudcp_solr_upgrader.log
tar -xzvf solr-8.11.1.tgz solr-8.11.1/bin/install_solr_service.sh 2>&1
sudo cp solr-8.11.1/bin/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/ 2>&1
SOLR_DIR_PROC=$(cat /tmp/solr_version)

if [[ $SOLR_DIR_PROC == 8.8.2 ]] ;then
if [ -d /opt/solr76data ]; then
echo "SOLRFCDIR /opt/solr76data exists" >> /var/log/filecloudcp_solr_upgrader.log
mv -vf /opt/solr76data /opt/solrfcdata 2>&1
sed -i 's/solr76data/solrfcdata/g'  /etc/default/solr.in.sh-8.8.2
systemctl daemon-reload
chown solr.solr /opt/solrfcdata -Rf
sudo bash  ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh  ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
cp -rvf /etc/default/solr.in.sh-8.8.2  /etc/default/solr.in.sh
service solr stop
mv /opt/solr-8.11.1/server/etc /opt/solr-8.8.2/server/etc_8.11.1_default 2>&1  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
/usr/bin/rsync -av --progress  /opt/solr-8.8.2/server/etc /opt/solr-8.11.1/server  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
else
sudo bash  ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh  ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
mv /opt/solr-8.11.1/server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF
fi
if [[ -d /opt/solr-8.8.2 ]]; then
  echo "Removing Solr 8.8.2 installation files" | write_log
  rm -rf  /opt/solr-8.8.2
fi
if [[ -d /opt/solr-8.5.0 ]]; then
  echo "Removing Solr 8.5.0 installation files" | write_log
  rm -rf  /opt/solr-8.5.0
fi
if [[ -d /opt/solr-8.2.0 ]]; then
  echo "Removing Solr 8.2.0 installation files" | write_log
  rm -rf  /opt/solr-8.2.0
fi
if [[ -d /opt/solr-7.6.0 ]]; then
  echo "Removing Solr 7.6.0 installation files" | write_log
  rm -rf  /opt/solr-7.6.0
fi
fi

if [[ $SOLR_DIR_PROC == 8.5.0 ]] ;then
if [ -d /opt/solr76data ]; then
echo "SOLRFCDIR /opt/solr76data exists" >> /var/log/filecloudcp_solr_upgrader.log
mv -vf /opt/solr76data /opt/solrfcdata 2>&1
sed -i 's/solr76data/solrfcdata/g'  /etc/default/solr.in.sh-8.5.0
systemctl daemon-reload
chown solr.solr /opt/solrfcdata -Rf
sudo bash ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
cp -rvf /etc/default/solr.in.sh-8.5.0  /etc/default/solr.in.sh
service solr stop
mv /opt/solr-8.11.1/server/etc /opt/solr-8.11.1/server/etc_8.11.1_default 2>&1  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
/usr/bin/rsync -av --progress  /opt/solr-8.5.0/server/etc /opt/solr-8.11.1/server  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
else
sudo bash ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
mv /opt/solr-8.11.1/server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF
fi
if [[ -d /opt/solr-8.5.0 ]]; then
  echo "Removing Solr 8.5.0 installation files" | write_log
  rm -rf  /opt/solr-8.5.0
fi
if [[ -d /opt/solr-8.2.0 ]]; then
  echo "Removing Solr 8.2.0 installation files" | write_log
  rm -rf  /opt/solr-8.2.0
fi
if [[ -d /opt/solr-7.6.0 ]]; then
  echo "Removing Solr 7.6.0 installation files" | write_log
  rm -rf  /opt/solr-7.6.0
fi
fi

if [[ $SOLR_DIR_PROC == 8.2.0 ]] ;then
if [ -d /opt/solr76data ]; then
echo "SOLRFCDIR /opt/solr76data exists" >> /var/log/filecloudcp_solr_upgrader.log
mv -vf /opt/solr76data /opt/solrfcdata 2>&1
sed -i 's/solr76data/solrfcdata/g'  /etc/default/solr.in.sh-8.2.0
systemctl daemon-reload
chown solr.solr /opt/solrfcdata -Rf
sudo bash ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
cp -rvf /etc/default/solr.in.sh-8.2.0  /etc/default/solr.in.sh
service solr stop
mv /opt/solr-8.11.1/server/etc /opt/solr-8.11.1/server/etc_8.11.1_default 2>&1  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
/usr/bin/rsync -av --progress  /opt/solr-8.2.0/server/etc /opt/solr-8.11.1/server  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
else
sudo bash ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
mv /opt/solr-8.11.1/server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF
fi
if [[ -d /opt/solr-8.2.0 ]]; then
  echo "Removing Solr 8.2.0 installation files" | write_log
  rm -rf  /opt/solr-8.2.0
fi
if [[ -d /opt/solr-7.6.0 ]]; then
  echo "Removing Solr 7.6.0 installation files" | write_log
  rm -rf  /opt/solr-7.6.0
fi
fi
if [[ $SOLR_DIR_PROC == 7.6.0 ]] ;then
if [ -d /opt/solr76data ]; then
echo "SOLRFCDIR /opt/solr76data exists" >> /var/log/filecloudcp_solr_upgrader.log
mv -vf /opt/solr76data /opt/solrfcdata 2>&1
sed -i 's/solr76data/solrfcdata/g'  /etc/default/solr.in.sh-7.6.0
systemctl daemon-reload
chown solr.solr /opt/solrfcdata -Rf
sudo bash ${SOLR_DOWNLOAD_DIR}/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
cp -rvf /etc/default/solr.in.sh-7.6.0 /etc/default/solr.in.sh
service solr stop
mv /opt/solr-8.11.1/server/etc /opt/solr-8.11.1/server/etc_8.11.1_default 2>&1  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
/usr/bin/rsync -av --progress  /opt/solr-7.6.0/server/etc /opt/solr-8.11.1/server  >> /var/log/filecloudcp_solr_upgrader.log > /dev/null
else
sudo bash ./install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
mv /opt/solr-8.11.1/server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF
fi
if [[ -d /opt/solr-7.6.0 ]]; then
  echo "Removing Solr 7.6.0 installation files" | write_log
  rm -rf  /opt/solr-7.6.0
fi
if [[ -d /opt/solr-5.3.1 ]]; then
  echo "Removing Solr 5.3.1 installation files" | write_log
  rm -rf  /opt/solr-5.3.1
fi
fi
if [[ $SOLR_DIR_PROC == 5.3.1 ]] ;then
sudo bash ./install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1 -d /opt/solrfcdata/var/solr -f 2>&1
service solr stop
mv /opt/solr-8.11.1server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF
fi
if [[ -d /opt/solr-5.3.1 ]]; then
  echo "Removing Solr 5.3.1 installation files" | write_log
  rm -rf  /opt/solr-5.3.1
fi
echo "solr hard nofile 65535" >> /etc/security/limits.conf  > /dev/null
echo "solr soft nofile 65535" >> /etc/security/limits.conf  > /dev/null
echo "solr soft nproc 65535" >> /etc/security/limits.conf > /dev/null
echo "solr hard nproc 65535" >> /etc/security/limits.conf  > /dev/null

service solr start
echo 'Solr port secured' 2>&1
echo  "Solr Upgradation Complete"



cd   > /dev/null
rm -rvf ${SOLR_DOWNLOAD_DIR}  > /dev/null
echo 'Solr Download directory is cleaned'  > /dev/null

 else

echo "Already running in the Latest SOLR VERSION which is $SOLR_VERSION"

fi

}
install_solr()
{
if [ -f "/etc/init.d/solr" ]; then
  # Control will enter here if $DIRECTORY exists.
echo "Solr exists"
upgrade_solr

else

echo -e "" > /dev/null
echo -e "###############################"  > /dev/null
echo -e "### INSTALLING SOLR         ###"  > /dev/null
echo -e "###############################" > /dev/null
if [ -n "$(command -v yum)" ]; then
 dependency_check_rpm
fi
if [ -n "$(command -v apt-get)" ]; then
 dependency_check_deb
fi
sudo mkdir -p /usr/local/src/solrinstall 2>&1
touch /var/log/filecloudcp_solr_installer.log
SOLR_URL="https://patch.codelathe.com/tonidocloud/live/3rdparty/solr/solr-8.11.1.tgz"


SOLR_DOWNLOAD_DIR=/usr/local/src/solrinstall

echo  "Downloading Solr zip"  >> /var/log/filecloudcp_solr_installer.log
cd ${SOLR_DOWNLOAD_DIR}
  {
sudo wget -N "$SOLR_URL"  2>&1 | \
 stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'
} | whiptail --gauge "Downloading Latest Solr " 7 60 0

echo  "Downloaded Solr zip"  >> /var/log/filecloudcp_solr_installer.log
tar -xzvf solr-8.11.1.tgz solr-8.11.1/bin/install_solr_service.sh 2>&1 >> /var/log/filecloudcp_solr_installer.log
cp solr-8.11.1/bin/install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/ 2>&1  >> /var/log/filecloudcp_solr_installer.log

bash ./install_solr_service.sh ${SOLR_DOWNLOAD_DIR}/solr-8.11.1.tgz -d /opt/solrfcdata/var/solr 2>&1  >> /var/log/filecloudcp_solr_installer.log

service solr stop
mv /opt/solr-8.11.1/server/etc/jetty-http.xml /opt/solr-8.11.1/server/etc/jetty-http.xml_old 2>&1
cat >/opt/solr-8.11.1/server/etc/jetty-http.xml<<'EOF'
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="acceptors" type="int"><Property name="solr.jetty.http.acceptors" default="-1"/></Arg>
        <Arg name="selectors" type="int"><Property name="solr.jetty.http.selectors" default="-1"/></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" default="127.0.0.1" /></Set>
        <Set name="port"><Property name="jetty.port" default="8983" /></Set>
        <Set name="idleTimeout"><Property name="solr.jetty.http.idleTimeout" default="120000"/></Set>
        <Set name="soLingerTime"><Property name="solr.jetty.http.soLingerTime" default="-1"/></Set>
        <Set name="acceptorPriorityDelta"><Property name="solr.jetty.http.acceptorPriorityDelta" default="0"/></Set>
        <Set name="acceptQueueSize"><Property name="solr.jetty.http.acceptQueueSize" default="0"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>
EOF


echo "solr hard nofile 65535" >> /etc/security/limits.conf  > /dev/null
echo "solr soft nofile 65535" >> /etc/security/limits.conf  > /dev/null
echo "solr soft nproc 65535" >> /etc/security/limits.conf > /dev/null
echo "solr hard nproc 65535" >> /etc/security/limits.conf  > /dev/null
sudo mkdir -p /opt/solrfcdata/var/solr/data/fccore  >> /var/log/filecloudcp_solr_installer.log > /dev/null
sudo rsync -r /var/www/html/thirdparty/solarium/fcskel/ /opt/solrfcdata/var/solr/data/fccore/   >> /var/log/filecloudcp_solr_installer.log > /dev/null
sudo chown solr.solr /opt/solrfcdata/var/solr/data/fccore -Rf  >> /var/log/filecloudcp_solr_installer.log > /dev/null
service solr restart
cd   > /dev/null
rm -rvf ${SOLR_DOWNLOAD_DIR}  >> /var/log/filecloudcp_solr_installer.log > /dev/null
echo 'Solr Download directory is cleaned'  >> /var/log/filecloudcp_solr_installer.log > /dev/null
fi
}

runupgrade(){
checkupgradefile
whiptail --title "FileCloud Control Panel" --textbox /dev/stdin 10 100 <<<"$(/usr/bin/php -f /usr/local/src/checkupgrade.php | sed -e 's/^[ \t]*//' | tr -d '\r')"
if [ -e /usr/local/src/upgrade_available.txt ]; then
CHECKUPDATE=$(cat /usr/local/src/upgrade_available.txt | sed -e 's/^[ \t]*//' |  tr -d '\r')
if [ $CHECKUPDATE == 2 ]; then
UPGRADE_SCRIPT_URL=http://patch.codelathe.com/tonidocloud/live/installer/upgrader.sh
curl -L $UPGRADE_SCRIPT_URL  --output /tmp/upgrader.sh --silent && bash /tmp/upgrader.sh
rm -rf /tmp/upgrader.sh
fi
fi
removeupgradefile
}

###################################################################################################################
###############################################FILECLOUD MONGODB UPGRADER##########################################
###################################################################################################################
mongodb_version_check()
{
if [ -n "$(command -v apt-get)" ]; then
v1=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
v2='4.0'
nv1=`echo $v1 | sed 's/\.//g'`
nv2=`echo $v2 | sed 's/\.//g'`
if [[ nv1 -lt nv2 ]]; then
echo "$v1 < $v2" |  write_log
echo "MongoDB version is less than v4.0 , Requires Manual Upgrade"  |  write_log
echo "Please upgrade MongoDB to v4.0"  |  write_log
exit 0
else
echo "$v1 >= $v2"
echo " MongoDB version is 4.0, Resuming Upgrade"  |  write_log
fi
fi
if [ -n "$(command -v yum)" ]; then
v1=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
v2='4.0'
nv1=`echo $v1 | sed 's/\.//g'`
nv2=`echo $v2 | sed 's/\.//g'`
if [[ nv1 -lt nv2 ]]; then
echo "$v1 < $v2" |  write_log
echo "MongoDB version is less than v4.0 , Requires Manual Upgrade" |  write_log
echo "Please upgrade MongoDB to v4.0" |  write_log
exit 0
else
echo "$v1 >= $v2"
echo " MongoDB version is 4.0, Resuming Upgrade"  |  write_log
fi
fi
}

mongodb_upgrade_40()
{

if [ -n "$(command -v apt-get)" ]; then
os_name_dbcheck="$(cat /etc/os-release |  grep '^VERSION_CODENAME' | awk -F\= '{print $2}' | tr -d '"')"
NOW="$(date '+%a %d-%m-%Y% %H:%M')"
MONGO_VERSION=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_MONGO_VERSION="4.0"


if [ "$(printf "$REQ_MONGO_VERSION\n$MONGO_VERSION" | sort -V | head -n1)" == "$MONGO_VERSION" ] && [ "$MONGO_VERSION" != "$REQ_MONGO_VERSION" ];then
echo " MongoDB version is older , Upgrade Required"  |  write_log


if [ -f /etc/apt/sources.list.d/mongodb-org-3.4.list ]; then
rm -rvf /etc/apt/sources.list.d/mongodb-org-3.4.list 2>&1 |  write_log
fi
if [ -f /etc/apt/sources.list.d/mongodb-org-3.6.list ]; then
rm -rvf /etc/apt/sources.list.d/mongodb-org-3.6.list 2>&1  |  write_log
fi

echo " Stopping MongoDB service prior to upgrade"  |  write_log
service mongod stop |  write_log
cp /etc/mongod.conf /etc/mongod.conf_backup

if  [[ -f /var/lib/apt/lists/lock && -f /var/cache/apt/archives/lock && -f /var/lib/dpkg/lock  ]];then
rm /var/lib/apt/lists/lock  |  write_log
rm /var/cache/apt/archives/lock  |  write_log
rm /var/lib/dpkg/lock-frontend |  write_log
rm /var/cache/debconf/*.dat  |  write_log
dpkg --configure -a  |  write_log
fi

if [[ $os_name_dbcheck == xenial ]] ; then
echo "$os_name_dbcheck found"
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add - |  write_log
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu  $(lsb_release -sc)/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list |  write_log
apt-get update -y |  write_log
apt-get upgrade -y --allow-downgrades  |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf |  write_log
service  mongod start
fi

if [[ $os_name_dbcheck == bionic ]] ; then
echo "$os_name_dbcheck found" |  write_log
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add - |  write_log
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu  $(lsb_release -sc)/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list |  write_log
apt-get update -y |  write_log
apt-get upgrade -y --allow-downgrades  |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf |  write_log
service  mongod start |  write_log
fi

if [[ $os_name_dbcheck == stretch ]]; then
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add - |  write_log
echo "deb http://repo.mongodb.org/apt/debian $(lsb_release -sc)/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list |  write_log
apt-get update -y |  write_log
apt-get upgrade -y |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf |  write_log
service  mongod start |  write_log
fi
else
echo " MongoDB version is 4.0"  |  write_log
fi

fi

if [ -n "$(command -v yum)" ]; then
a1=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var1=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b1=${var1%\.*}
os_name_dbcheck_rpm="${a1}${b1}"

NOW="$(date '+%a %d-%m-%Y% %H:%M')"
MONGO_VERSION=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_MONGO_VERSION="4.0"


if [ "$(printf "$REQ_MONGO_VERSION\n$MONGO_VERSION" | sort -V | head -n1)" == "$MONGO_VERSION" ] && [ "$MONGO_VERSION" != "$REQ_MONGO_VERSION" ];then
echo " MongoDB version is older , Upgrade Required"   |  write_log

if [[ $os_name_dbcheck_rpm == rhel7 && $os_name_dbcheck_rpm == centos7 ]]; then

if [ -f /etc/yum.repos.d/mongodb-org-3.4.repo ]; then
rm -rvf /etc/yum.repos.d/mongodb-org-3.4.repo 2>&1  |  write_log
fi
if [ -f /etc/yum.repos.d/mongodb-org-3.6.repo ]; then
rm -rvf /etc/yum.repos.d/mongodb-org-3.6.repo  2>&1  |  write_log
fi

echo " Stopping MongoDB service prior to upgrade"  |  write_log
service mongod stop  |  write_log

cp /etc/mongod.conf /etc/mongod.conf_backup


cat <<EOF > /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF

yum update -y
yum upgrade -y
mv /etc/mongod.conf_backup /etc/mongod.conf
service  mongod start
fi
else
echo " MongoDB version is 4.0"   |  write_log
fi
fi
}

mongodb_upgrade_42()
{

mongodb_version_check
if [ -n "$(command -v apt-get)" ]; then
os_name_dbcheck="$(cat /etc/os-release |  grep '^VERSION_CODENAME' | awk -F\= '{print $2}' | tr -d '"')"
NOW="$(date '+%a %d-%m-%Y% %H:%M')"
MONGO_VERSION=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_MONGO_VERSION="4.2"


if [ "$(printf "$REQ_MONGO_VERSION\n$MONGO_VERSION" | sort -V | head -n1)" == "$MONGO_VERSION" ] && [ "$MONGO_VERSION" != "$REQ_MONGO_VERSION" ];then
echo " MongoDB version is older , Upgrade Required" |  write_log

if [ -f /etc/apt/sources.list.d/mongodb-org-4.0.list ]; then
rm -rvf /etc/apt/sources.list.d/mongodb-org-4.0.list 2>&1 |  write_log
fi

echo " Stopping MongoDB service prior to upgrade"   |  write_log
service mongod stop  |  write_log
cp /etc/mongod.conf /etc/mongod.conf_backup

if  [[ -f /var/lib/apt/lists/lock && -f /var/cache/apt/archives/lock && -f /var/lib/dpkg/lock  ]];then
rm /var/lib/apt/lists/lock  |  write_log
rm /var/cache/apt/archives/lock  |  write_log
rm /var/lib/dpkg/lock-frontend |  write_log
rm /var/cache/debconf/*.dat  |  write_log
dpkg --configure -a  |  write_log
fi

if [[ $os_name_dbcheck == xenial ]]; then
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - |  write_log
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu  $(lsb_release -sc)/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list|  write_log
apt-get update -y |  write_log
apt-get upgrade -y --allow-downgrades |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf |  write_log
service  mongod start
fi

if [[ $os_name_dbcheck == bionic ]]; then
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - |  write_log
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu  $(lsb_release -sc)/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list |  write_log
apt-get update -y |  write_log
apt-get upgrade -y  --allow-downgrades |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf |  write_log
service  mongod start
fi

if [[ $os_name_dbcheck == stretch ]]; then
apt-get install gnupg -y |  write_log
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add - |  write_log
echo "deb http://repo.mongodb.org/apt/debian $(lsb_release -sc)/mongodb-org/4.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list |  write_log
apt-get update -y |  write_log
apt-get upgrade -y |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf
service  mongod start
fi
else
echo " MongoDB version is 4.2"  |  write_log

fi
fi

if [ -n "$(command -v yum)" ]; then
a1=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var1=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b1=${var1%\.*}
os_name_dbcheck_rpm="${a1}${b1}"

NOW="$(date '+%a %d-%m-%Y% %H:%M')"
MONGO_VERSION=$(mongo --version | head -n 1 | cut -d" " -f4 | cut -c2- | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_MONGO_VERSION="4.2"


if [ "$(printf "$REQ_MONGO_VERSION\n$MONGO_VERSION" | sort -V | head -n1)" == "$MONGO_VERSION" ] && [ "$MONGO_VERSION" != "$REQ_MONGO_VERSION" ];then
echo " MongoDB version is older , Upgrade Required"  |  write_log

if [[ $os_name_dbcheck_rpm == rhel7 && $os_name_dbcheck_rpm == centos7 ]]; then

if [ -f /etc/yum.repos.d/mongodb-org-4.2.repo ]; then
rm -rvf /etc/yum.repos.d/mongodb-org-4.2.repo  2>&1  |  write_log
fi

echo " Stopping MongoDB service prior to upgrade" |  write_log
service mongod stop   |  write_log

cp /etc/mongod.conf /etc/mongod.conf_backup


cat <<EOF > /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF

yum update -y |  write_log
yum upgrade -y |  write_log
mv /etc/mongod.conf_backup /etc/mongod.conf
service  mongod start
fi
else
echo " MongoDB version is 4.2"  |  write_log
fi
fi
}
###################################################################################################################
###################################################################################################################
###################################################################################################################
######################FILECLOUD OFFLINE INSTALLER STARTS HERE#################################################
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################
fc_offline_install()
{
FC_UPGRADE_PATH=/opt/app/configure/fcupgrade
WRKG_DIR=`echo $PWD`

write_log()
{
  if [[ -d /var/log/fcsetup ]];then
  while read text
  do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
LOG=/var/log/fcsetup/$(basename -- "$0" .sh ).log
if [[ ! -e "$LOG" ]] ; then
         touch ${LOG}
         chmod 744 ${LOG}
fi
     echo ${LOGTIME}": $text" | tee -a ${LOG};

  done
  else
    mkdir -p /var/log/fcsetup
     while read text
  do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
LOG=/var/log/fcsetup/$(basename -- "$0" .sh ).log
if [[ ! -e "$LOG" ]] ; then
         touch ${LOG}
         chmod 744 ${LOG}
fi
     echo ${LOGTIME}": $text" | tee -a ${LOG};

  done
  fi
}
####################################################################################################
dependency_check_deb_offline()
{
  if ! [[ -x "$(command -v unzip)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
dpkg  --install  /opt/fcsetupoffline/misc/unzip*.deb | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v lsof)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
dpkg  --install /opt/fcsetupoffline/misc/lsof*.deb | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v wget)" ]]; then
echo "Wget not installed" 2>&1 | write_log
dpkg  --install  /opt/fcsetupoffline/misc/wget*.deb | write_log
echo "Installing Wget" 2>&1 | write_log
else
echo "Wget Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
dpkg --install /opt/fcsetupoffline/misc/java*.deb | write_log
echo "Installing Java" | write_log
else
echo "Java Installed" | write_log
fi
}
dependency_check_rpm_offline()
{
if ! [[ -x "$(command -v unzip)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/unzip*.rpm | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v bc)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/bc*.rpm | write_log
echo "Installing BC" 2>&1 | write_log
else
echo "BC Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v lsof)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm  -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/lsof*.rpm | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v wget)" ]]; then
echo "Wget not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/wget*.rpm | write_log
echo "Installing Wget" 2>&1 | write_log
else
echo "Wget Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/java*.rpm | write_log
echo "Installing Java" | write_log
else
echo "Java Installed" | write_log
fi
}

 package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}
package_installer_rhel8_offline()
{
mkdir -p /opt/fcsetupoffline/appcomponents
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
}
package_installer_rhel_cen7_offline()
{
echo " Detected RHEL7/CentOS7 installation" | write_log
mkdir -p /opt/fcsetupoffline/appcomponents
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
}
package_rpm_installer_offline()
{
SESTATUS=$(sestatus | grep "SELinux status:" | awk '{print $3}')
if [[ "$SESTATUS" == enabled ]]; then
echo "SELinux is enabled" | write_log
echo "disabling selinux temporarily" | write_log
setenforce 0
fi
if [[ "$SESTATUS" == disabled ]]; then
echo "SELinux is disabled" | write_log
fi
dependency_check_rpm_offline
set +e
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_installer_rhel8_offline
 fi
if [[ $os_check_rpm == rhel7 ]]; then
package_installer_rhel_cen7_offline

 fi
if [[ $os_check_rpm == centos7 ]]; then
package_installer_rhel_cen7_offline
fi

fi

}
deb_installer_offline()
{
sudo rm /var/lib/apt/lists/lock | write_log
sudo rm /var/cache/apt/archives/lock | write_log
sudo rm /var/lib/dpkg/lock* | write_log
sudo dpkg --configure -a | write_log

mkdir -p /opt/fcsetupoffline/appcomponents/dpkg/
sudo dpkg -R --install /opt/fcsetupoffline/appcomponents/dpkg/
sudo dpkg -R --install /opt/fcsetupoffline/appcomponents/fcpackage/
}
filecloud_rpm_installer(){
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/fcpackage/*.rpm
}
rpm_installer_offline()
{
package_rpm_installer_offline
filecloud_rpm_installer
}

if [ -n "$(command -v yum)" ]; then
 mkdir -p /opt/fcsetupoffline
package_download_check_rpm
dependency_check_rpm_offline
rpm_installer_offline
rm -rf /opt/fcsetupoffline

fi

if [ -n "$(command -v apt-get)" ]; then
    mkdir -p /opt/fcsetupoffline
package_download_check_deb
dependency_check_deb_offline
deb_installer_offline
rm -rf /opt/fcsetupoffline

fi

}
######################FILECLOUD OFFLINE INSTALLER ENDS HERE#################################################
######################FILECLOUD OFFLINE UPGRADER STARTS HERE#################################################

fc_offline_upgrade()
{
FC_UPGRADE_PATH=/opt/app/configure/fcupgrade
WRKG_DIR=`echo $PWD`

write_log()
{
  if [[ -d /var/log/fcsetup ]];then
  while read text
  do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
LOG=/var/log/fcsetup/$(basename -- "$0" .sh ).log
if [[ ! -e "$LOG" ]] ; then
         touch ${LOG}
         chmod 744 ${LOG}
fi
     echo ${LOGTIME}": $text" | tee -a ${LOG};

  done
  else
    mkdir -p /var/log/fcsetup
     while read text
  do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
LOG=/var/log/fcsetup/$(basename -- "$0" .sh ).log
if [[ ! -e "$LOG" ]] ; then
         touch ${LOG}
         chmod 744 ${LOG}
fi
     echo ${LOGTIME}": $text" | tee -a ${LOG};

  done
  fi
}
####################################################################################################
dependency_check_rpm_offline()
{
if ! [[ -x "$(command -v unzip)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/unzip*.rpm | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v bc)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/bc*.rpm | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v lsof)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
rpm  -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/lsof*.rpm | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v wget)" ]]; then
echo "Wget not installed" 2>&1 | write_log
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/wget*.rpm | write_log
echo "Installing Wget" 2>&1 | write_log
else
echo "Wget Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/java*.rpm | write_log
echo "Installing Java" | write_log
else
echo "Java Installed" | write_log
fi
}
#FILECLOUD MQDRAINER
###################################
fcmqdrainer()
{
service_all_stop()
{
fc_process_prepstop=($( awk -F "," '{print $2}' /var/www/html/resources/preupgrade/manifest.dat | sed 's/^.\(.*\).$/\1/' | cut -d'=' -f2 | tr ';' ' '  ))
fc_prepstop=("${fc_process_prepstop[@]}")
for i in "${!fc_prepstop[@]}"; do
      if [[ ${fc_prepstop[$i]} == "mongodb" ]]; then
        fc_prepstop[$i]="mongod"
    fi
done
for var in "${fc_prepstop[@]}" ; do
#echo ${fc_process_stop[@]}
echo "Stopping $var Service"
service $var stop
done
}
service_preupgrade_stop()
{
#process_line_check=$(cat /var/www/html/resources/preupgrade/manifest.dat | awk 'END{print NR}')
#for line in $process_line_check; do

if [ -n "$(command -v yum)" ]; then
fc_process=("apache" "mongodb" "crond" "solr" "fcdocconverter" "fcmqserver" "fcmqparallel" "fcmqserial")
fc_all_process=("${fc_process[@]}")
for i in "${!fc_all_process[@]}"; do
if [[ ${fc_all_process[$i]} == "apache" ]]; then
fc_all_process[$i]="httpd"
fi
done
fc_process_run=($( awk -F "," '{print $2}' /var/www/html/resources/preupgrade/manifest.dat | sed 's/^.\(.*\).$/\1/' | cut -d'=' -f2 | tr ';' ' '  ))
#echo ${fc_process_run[@]}
# shellcheck disable=SC2207
# shellcheck disable=SC2068
fc_process_stop=(`echo ${fc_all_process[@]} ${fc_process_run[@]} | tr ' ' '\n' | sort | uniq -u `)
#echo ${fc_process_stop[@]}
for var in "${fc_process_stop[@]}" ; do
#echo ${fc_process_stop[@]}
echo "stopping $var Service"
service $var stop
done

fi

}

preupgrade()
{
#lens=$(cat /var/www/html/resources/preupgrade/manifest.dat | awk 'END{print NR}')
#for line in $lens; do
preupgradeversion=$(awk -F "," '{print $1}' /var/www/html/resources/preupgrade/manifest.dat)
service_preupgrade_stop
php /var/www/html/resources/preupgrade/preupgrade.php --migration="$preupgradeversion"
#done
service_all_stop
rm -rf /var/www/html/resources/filecloudpreupgrade.zip
rm -rf /var/www/html/resources/preupgrade
}

if  [[ -f /var/www/html/resources/filecloudpreupgrade.zip ]] && [[ -d /var/www/html/resources/preupgrade ]]; then
rm -rf /var/www/html/resources/filecloudpreupgrade.zip
rm -rf /var/www/html/resources/preupgrade
fi
cp -rvf /opt/fcsetupoffline/misc/preupgrader/filecloudpreupgrade.zip /var/www/html/resources/ 2>&1 | write_log
cd /var/www/html/resources
unzip -q filecloudpreupgrade.zip
if [ -n "$(command -v apt-get)" ]; then
chown www-data:www-data /var/www/html/resources/preupgrade -Rf
fi
if [ -n "$(command -v yum)" ]; then
chown apache:apache /var/www/html/resources/preupgrade -Rf
fi
preupgrade
touch /usr/local/src/fcmqdrainer.lck
}


######################FILECLOUD CUSTOM CONFIG CHECK#################################################

 package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}
######################FILECLOUD CONFIG BACKUP FUNCTION#################################################
php_version_check_rpm_rhel8_offline()
{
PHP_VERSION_CHECK=$(php -v | head -n1 | awk '{print $2;}' | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_PHP_VERSION="8.1"
##Upgrade MongoDB##
#if [[ "$MONGO_VERSION" -le "3.6" ]]; then
if [ "$(printf "$REQ_PHP_VERSION\n$PHP_VERSION_CHECK" | sort -V | head -n1)" == "$PHP_VERSION_CHECK" ] && [ "$PHP_VERSION_CHECK" != "$REQ_PHP_VERSION" ];then
echo " PHP version is older , Upgrade Required"  | write_log
echo -e "\n### Removing Old PHP Version and Ioncube ###############\n" 2>&1 | write_log > /dev/null
rm -rf /var/lib/rpm/.rpm.lock
rpm -e --nodeps $(rpm -qa 'php*') | write_log
rpm -e  --nodeps mod_php71w | write_log
rpm -e  --nodeps mod_php72w | write_log
echo " Detected RHEL7/CentOS7 installation" | write_log
mkdir -p /opt/fcsetupoffline/appcomponents
rm -rf /var/lib/rpm/.rpm.lock
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
else
echo " PHP version is 8.1, Performing Minor version Upgrade"  | write_log
mkdir -p /opt/fcsetupoffline/appcomponents
rm -rf /var/lib/rpm/.rpm.lock
rpm -Uvh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
fi
}
php_version_check_rpm_rhel_cen7_offline()
{
PHP_VERSION_CHECK=$(php -v | head -n1 | awk '{print $2;}' | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_PHP_VERSION="8.1"
##Upgrade MongoDB##
#if [[ "$MONGO_VERSION" -le "3.6" ]]; then
if [ "$(printf "$REQ_PHP_VERSION\n$PHP_VERSION_CHECK" | sort -V | head -n1)" == "$PHP_VERSION_CHECK" ] && [ "$PHP_VERSION_CHECK" != "$REQ_PHP_VERSION" ];then
echo " PHP version is older , Upgrade Required"  | write_log
echo -e "\n### Removing Old PHP Version and Ioncube ###############\n" 2>&1 | write_log > /dev/null
rm -rf /var/lib/rpm/.rpm.lock
rpm -e --nodeps $(rpm -qa 'php*') | write_log
rpm -e  --nodeps mod_php71w | write_log
rpm -e  --nodeps mod_php72w | write_log
echo " Detected RHEL7/CentOS7 installation" | write_log
mkdir -p /opt/fcsetupoffline/appcomponents
rm -rf /var/lib/rpm/.rpm.lock
rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
else
echo " PHP version is 8.1, Performing Minor version Upgrade"  | write_log
mkdir -p /opt/fcsetupoffline/appcomponents
rm -rf /var/lib/rpm/.rpm.lock
rpm -Uvh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/rpms/*.rpm | write_log
fi
}
package_rpm_upgrader_offline()
{
SESTATUS=$(sestatus | grep "SELinux status:" | awk '{print $3}')
if [[ "$SESTATUS" == enabled ]]; then
echo "SELinux is enabled" | write_log
echo "disabling selinux temporarily" | write_log
setenforce 0
fi
if [[ "$SESTATUS" == disabled ]]; then
echo "SELinux is disabled" | write_log
fi
dependency_check_rpm_offline
echo -e "\n### Stopping Services ###############\n" 2>&1 | write_log
if [[ -e /var/www/html/src/Scripts/cron.php ]]; then
echo 'Filecloud server version is 20.2' | write_log
service solr stop
echo 'Stopped SOLR Service' | write_log
lsof -ti :8983 | xargs --no-run-if-empty kill -9
service fcdocconverter stop | write_log
lsof -ti :8080 | xargs --no-run-if-empty kill -9
echo 'Stopped Docconverter Service' | write_log
service httpd stop | write_log
echo 'Stopped Apache Service' | write_log
if [ -f /usr/lib/systemd/system/mongod.service ]; then
service mongod stop | write_log
echo 'Stopped MongoDB Service' | write_log
fi
if [ -f /etc/systemd/system/fcorchestrator.service ]; then
service fcorchestrator stop
else
service fcmqserver stop | write_log
service fcmqserial stop | write_log
service fcmqparallel stop | write_log
echo 'Stopped MQ Service' | write_log
fi
service crond stop
echo 'Stopping CronJob' | write_log
else
echo 'Filecloud server version is less than 20.2' | write_log
echo 'Draining Filecloud Queues' | write_log
fcmqdrainer  | write_log
service solr stop
echo 'Stopped SOLR Service' | write_log
lsof -ti :8983 | xargs --no-run-if-empty kill -9
service fcdocconverter stop | write_log
lsof -ti :8080 | xargs --no-run-if-empty kill -9
echo 'Stopped Docconverter Service' | write_log
service httpd stop | write_log
echo 'Stopped Apache Service' | write_log
service mongod stop | write_log
echo 'Stopped MongoDB Service' | write_log
service fcmqserver stop | write_log
service fcmqserial stop | write_log
service fcmqparallel stop | write_log
echo 'Stopped MQ Service' | write_log
service crond stop
echo 'Stopping CronJob' | write_log
fi
set +e
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
php_version_check_rpm_rhel8_offline
 fi
if [[ $os_check_rpm == rhel7 ]]; then
php_version_check_rpm_rhel_cen7_offline

 fi
if [[ $os_check_rpm == centos7 ]]; then
php_version_check_rpm_rhel_cen7_offline
fi

fi

}
deb_upgrader_offline()
{
  sudo rm /var/lib/apt/lists/lock | write_log
sudo rm /var/cache/apt/archives/lock | write_log
sudo rm /var/lib/dpkg/lock* | write_log
sudo dpkg --configure -a | write_log

PHP_VERSION_CHECK=$(php -v | head -n1 | awk '{print $2;}' | awk '{printf "%.1f\n", $NF}'| sed -e 's/^[ \t]*//' |  tr -d '\r')
REQ_PHP_VERSION="8.1"
##Upgrade MongoDB##
#if [[ "$MONGO_VERSION" -le "3.6" ]]; then
if [ "$(printf "$REQ_PHP_VERSION\n$PHP_VERSION_CHECK" | sort -V | head -n1)" == "$PHP_VERSION_CHECK" ] && [ "$PHP_VERSION_CHECK" != "$REQ_PHP_VERSION" ];then
echo " PHP version is older , Upgrade Required"  | write_log
echo -e "\n### Removing Old PHP Version and Ioncube ###############\n" 2>&1 | write_log > /dev/null
#rpm -e --nodeps $(rpm -qa 'php*')
#rpm -e  --nodeps mod_php71w

echo " Detected Filecloud supported OS installation"
mkdir -p /opt/fcsetupoffline/appcomponents/dpkg/
apt-get remove php8.1* -y
dpkg -R --install /opt/fcsetupoffline/appcomponents/dpkg/
dpkg -R --install /opt/fcsetupoffline/appcomponents/fcpackage/
apt-get purge php7.4-* php7.2-* php7.1-* php7.0-* -y
apt-get autoremove -y
rm -rvf /etc/php/7.4
rm -rvf /etc/php/7.3
rm -rvf /etc/php/7.2
rm -rvf /etc/php/7.1
rm -rvf /etc/php/7.0
rm -rvf /etc/php/5.6
else
echo " PHP version is 8.1, performing PHP minor version upgrade"  | write_log
dpkg -R --install /opt/fcsetupoffline/appcomponents/dpkg/
echo "Restarting Apache after PHP config changes"
systemctl restart apache2
echo "Stopping Apache after PHP config changes"
fi

}
dependency_check_deb_offline()
{
  if ! [[ -x "$(command -v unzip)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
dpkg  --install  /opt/fcsetupoffline/misc/unzip*.deb | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v lsof)" ]]; then
echo "Unzip not installed" 2>&1 | write_log
dpkg  --install /opt/fcsetupoffline/misc/lsof*.deb | write_log
echo "Installing Unzip" 2>&1 | write_log
else
echo "Unzip Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v wget)" ]]; then
echo "Wget not installed" 2>&1 | write_log
dpkg  --install  /opt/fcsetupoffline/misc/wget*.deb | write_log
echo "Installing Wget" 2>&1 | write_log
else
echo "Wget Installed" 2>&1 | write_log
fi
if ! [[ -x "$(command -v java)" ]]; then
echo "Java not installed"
dpkg -R --install /opt/fcsetupoffline/misc/java*.deb | write_log
echo "Installing Java" | write_log
else
echo "Java Installed" | write_log
fi
}
filecloud_rpm_upgrader(){
rpm -Uvh --nodeps --nosignature --force /opt/fcsetupoffline/appcomponents/fcpackage/*.rpm
}
rpm_upgrader_offline()
{
package_rpm_upgrader_offline
filecloud_rpm_upgrader
}

if [ -n "$(command -v yum)" ]; then
  mkdir -p /opt/fcsetupoffline
package_download_check_rpm
dependency_check_rpm_offline
rpm_upgrader_offline
rm -rf /opt/fcsetupoffline
fi

if [ -n "$(command -v apt-get)" ]; then
    mkdir -p /opt/fcsetupoffline
package_download_check_deb
dependency_check_deb_offline
deb_upgrader_offline
rm -rf /opt/fcsetupoffline
fi

}
######################FILECLOUD OFFLINE UPGRADER ENDS HERE#################################################
######################FILECLOUD OFFLINE MONGODB UPGRADER STARTS HERE#################################################

mongodb_upgrade_offline_44()
{

 package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}
  if [ -n "$(command -v yum)" ]; then
   mkdir -p /opt/fcsetupoffline
  package_download_check_rpm
  service mongod stop
  cp -rvf /etc/mongod.conf /etc/mongod.conf_old
  rpm -e mongodb-org*
  rpm -ivh --nodeps --nosignature --force /opt/fcsetupoffline/mongodb/44/*.rpm
  cp -rvf /etc/mongod.conf_old /etc/mongod.conf
  service mongod restart
  rm -rf /opt/fcsetupoffline

  fi
    if [ -n "$(command -v apt)" ]; then
  mkdir -p /opt/fcsetupoffline
 package_download_check_deb
  service mongod stop
  cp -rvf /etc/mongod.conf /etc/mongod.conf_old
  apt-get remove mongodb-org* -y
  dpkg -i /opt/fcsetupoffline/mongodb/44/*.deb
  cp -rvf /etc/mongod.conf_old /etc/mongod.conf
  service mongod restart
  rm -rf /opt/fcsetupoffline

  fi
}

mongodb_upgrade_offline_42()
{

 package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}
    if [ -n "$(command -v yum)" ]; then
        mkdir -p /opt/fcsetupoffline
        package_download_check_rpm
  service mongod stop
  rpm -Uvh --nodeps --nosignature --force /opt/fcsetupoffline/mongodb/42/*.rpm
  service mongod restart
   fi
      if [ -n "$(command -v apt)" ]; then
          mkdir -p /opt/fcsetupoffline
 package_download_check_deb
  service mongod stop
  dpkg -i /opt/fcsetupoffline/mongodb/42/*.deb
  service mongod restart
  fi
}
mongodb_upgrade_offline_40()
{

package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}
   if [ -n "$(command -v yum)" ]; then
       mkdir -p /opt/fcsetupoffline
       package_download_check_rpm
  service mongod stop
  rpm -Uvh --nodeps --nosignature --force /opt/fcsetupoffline/mongodb/40/*.rpm
  service mongod restart

  fi
     if [ -n "$(command -v apt)" ]; then
         mkdir -p /opt/fcsetupoffline
 package_download_check_deb
  service mongod stop
  dpkg -i /opt/fcsetupoffline/mongodb/40/*.deb
  service mongod restart

  fi
}
######################FILECLOUD OFFLINE MONGODB UPGRADER ENDS HERE#################################################
######################FILECLOUD OFFLINE LIBREOFFICE UPGRADER STARTS HERE#################################################

libreoffice_upgrade_offline()
{

package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file  and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}

LIBRE_VERSION=$(libreoffice --version | awk '{print $2}' )
REQ_VERSION="7.4.1.2"

if [ "$(printf "$REQ_VERSION\n$LIBRE_VERSION" | sort -V | head -n1)" == "$LIBRE_VERSION" ] && [ "$LIBRE_VERSION" != "$REQ_VERSION" ];then

  echo    "LibreVersion is $LIBRE_VERSION"

 if [ -n "$(command -v yum)" ]; then
     mkdir -p /opt/fcsetupoffline
   package_download_check_rpm
    echo    "Removing old Libreoffice installation"  |  write_log
  yum remove libreoffice* -y |  write_log
   set -euo pipefail
if [ -d  /usr/lib/libreoffice ]; then
echo "Libreoffice DIR exists"
unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice
rm -rvf  /opt/libreoffice6*
rm -rvf /usr/lib/libreoffice
else
echo "NO Libreoffice DIR exists"
fi

cd /opt/fcsetupoffline/libreoffice 2>&1 | write_log
echo "Installing LibreOffice ..." 2>&1 | write_log
tar -xzvf /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1_Linux_x86-64_rpm.tar.gz | write_log
rpm -ivh /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS/*.rpm 2>&1 | write_log
ln -sfn /usr/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1 | write_log
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1 | write_log
rm -rf /opt/fcsetupoffline
fi

  if [ -n "$(command -v apt)" ]; then
      mkdir -p /opt/fcsetupoffline
   package_download_check_deb
    echo    "Removing old Libreoffice installation"  |  write_log
 apt-get purge libreoffice* -y
if [ -d  /usr/lib/libreoffice ]; then
echo "Libreoffice DIR exists"
unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice
rm -rvf  /opt/libreoffice*
rm -rvf /usr/lib/libreoffice
else
echo "NO Libreoffice DIR exists"
fi

cd /opt/fcsetupoffline/libreoffice 2>&1 | write_log
echo "Installing LibreOffice ..." 2>&1 | write_log
tar -xzvf /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1_Linux_x86-64_deb.tar.gz | write_log
sudo dpkg -R --install /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS/ 2>&1 | write_log
ln -sfn /usr/local/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1
rm -rf /opt/fcsetupoffline
fi
 else
      echo "LibreVersion is $LIBRE_VERSION"  2>&1 | write_log


 fi

 }
######################FILECLOUD OFFLINE LIBREOFFICE UPGRADER ENDS HERE#################################################
######################FILECLOUD OFFLINE LIBREOFFICE INSTALLER STARTS HERE#################################################

 libreoffice_install_offline()
 {

package_download_check_focal()
{
filenamefocal=/opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz ]]; then
 if [[ $(find "$filenamefocal" -mtime +2 -print) ]]; then
echo "File $filenamefocal exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagedeb_focal.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rh7cen7()
{
filenamerhcn7=/opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz

if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz ]]; then
if [[ $(find "$filenamerhcn7" -mtime +2 -print) ]]; then
echo "File $filenamerhcn7 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloader"
exit 0

else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rhcn7.tgz -C /opt/fcsetupoffline
fi
  else
          echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0

    fi
}
package_download_check_rh8()
{
filenamerh8=/opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz
  if [[ -f /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz ]]; then
 if [[ $(find "$filenamerh8" -mtime +2 -print) ]]; then
echo "File $filenamerh8 exists and is older than 5 days,please redownload the file and place it at /opt/offlinefcpkg/downloade"
exit 0
  else
    tar -xzvf /opt/offlinefcpkg/downloader/fcpackagerpm_rh8.tgz -C /opt/fcsetupoffline
  fi
  else
      echo "Packager not found, please download the package from the server and place it at /opt/offlinefcpkg/downloader"
   exit 0
      fi
}
package_download_check_rpm()
{
a=$(cat /etc/os-release |  grep '^ID=' | awk -F\= '{print $2}' | tr -d '"')
var=$(cat /etc/os-release |  grep '^VERSION_ID' | awk -F\= '{print $2}' | tr -d '"')
b=${var%\.*}
os_check_rpm="${a}${b}"
supported_os_rpm=('rhel8''rhel7''centos7')
if [[ ${supported_os_rpm[*]} =~ ${os_check_rpm} ]];then
if [[ $os_check_rpm == rhel8 ]]; then
package_download_check_rh8
 fi
if [[ $os_check_rpm == rhel7 ]];then
package_download_check_rh7cen7
 fi
if [[ $os_check_rpm == centos7 ]]; then
package_download_check_rh7cen7
fi
fi
}
package_download_check_deb()
{
os_check_deb=$(lsb_release -c | awk '{print $2}' |  tr -d '"')
supported_os_deb=('bionic''focal')
if [[ ${supported_os_deb[*]} =~ ${os_check_deb} ]];then
if [[ $os_check_deb == bionic ]]; then
package_download_check_bionic
 fi
if [[ $os_check_deb == focal ]];then
package_download_check_focal
 fi
fi
}


 if [ -n "$(command -v yum)" ]; then
     mkdir -p /opt/fcsetupoffline

   package_download_check_rpm
    echo    "Removing old Libreoffice installation"  |  write_log
  yum remove libreoffice* -y |  write_log
   set -euo pipefail
if [ -d  /usr/lib/libreoffice ]; then
echo "Libreoffice DIR exists"
unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice
rm -rvf  /opt/libreoffice6*
rm -rvf /usr/lib/libreoffice
else
echo "NO Libreoffice DIR exists"
fi

cd /opt/fcsetupoffline/libreoffice 2>&1 | write_log
echo "Installing LibreOffice ..." 2>&1 | write_log
tar -xzvf /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1_Linux_x86-64_rpm.tar.gz | write_log
rpm -ivh /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_rpm/RPMS/*.rpm 2>&1 | write_log
ln -sfn /usr/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1 | write_log
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1 | write_log
rm -rf /opt/fcsetupoffline
fi

  if [ -n "$(command -v apt)" ]; then
      mkdir -p /opt/fcsetupoffline

   package_download_check_deb
    echo    "Removing old Libreoffice installation"  |  write_log
 apt-get remove libreoffice* -y
if [ -d  /usr/lib/libreoffice ]; then
echo "Libreoffice DIR exists"
unlink /usr/bin/libreoffice
unlink /usr/lib/libreoffice
rm -rvf  /opt/libreoffice*
rm -rvf /usr/lib/libreoffice
else
echo "NO Libreoffice DIR exists"
fi

cd /opt/fcsetupoffline/libreoffice 2>&1 | write_log
echo "Installing LibreOffice ..." 2>&1 | write_log
tar -xzvf /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1_Linux_x86-64_deb.tar.gz | write_log
sudo dpkg -R --install /opt/fcsetupoffline/libreoffice/LibreOffice_7.4.1.2_Linux_x86-64_deb/DEBS/ 2>&1 | write_log
ln -sfn /usr/local/bin/libreoffice7.4 /usr/bin/libreoffice 2>&1
ln -sfn /opt/libreoffice7.4 /usr/lib/libreoffice 2>&1
rm -rf /opt/fcsetupoffline

fi
 }
 ######################FILECLOUD OFFLINE LIBREOFFICE INSTALLER ENDS HERE#################################################
####################################################################################################
####################################################################################################
####################################################################################################
####################################################################################################

fc_version_check()
{
if [[ -z $(apache2 -v 2>/dev/null) ]] && [[ -z $(httpd -v 2>/dev/null) ]] && [[ -z $(nginx -v 2>/dev/null) ]] ;
then
echo "Filecloud Installation not be found, either it is FC is not installed or Suspecting to be MongoDB servers or Solr server" 2>&1
else
if [ -e /var/www/html/src/Scripts/cron.php ]; then
PREFIX=/var/www/html/public
else
PREFIX=/var/www/html/core
fi
version="$(sed -n '/<Version>/,/<\/Version>/H; /<Version>/h; /\/Version/{x;s/<Version>\(.*[^\n]\)\n*<\/Version>/\1/p;}'  $PREFIX/manifest.xml | sed -e 's/^[ \t]*//' |  tr -d '\r')\nCopyright (C) 2022 CodeLathe Technologies, Inc.\nThis is Commercial software.\nEnterprise File Sharing, Sync and Mobile Access for Business\nReport bugs and tickets to <support@codelathe.com>."
echo -e $version
fi
}
operands=
have_pat=0
files_with_matches=0
files_without_matches=0
no_filename=0
with_filename=0

while test $# -ne 0; do
  option=$1
  shift
  optarg=



  case $option in

   (-c |--check-upgrade )
    checkupgrade || exit 2
    exit;;
        (-u |--run-upgrade )
    runupgrade || exit 2
    exit;;
        (-r |--reset-password )
    whiptail --title "FileCloud Control Panel" --textbox /dev/stdin 10 100 <<<"$(/usr/bin/php -f /var/www/html/resources/backup/resetadminpw.php | sed -e 's/^[ \t]*//' | tr -d '\r')" || exit 2
    exit;;
   (-h |--help )
    usage || exit 2
    exit;;
    (-s | --install-solr )
    install_solr || exit 2
    exit;;
    (-p | --install-preview )
    install_preview || exit 2
    exit;;
   (-l | --libreoffice-check )
    libreoffice_check || exit 2
    exit;;
      (-t | --install-tesseract )
    install_tesseract || exit 2
    exit;;
        (-o | --compile-tesseract )
    compile_tesseract || exit 2
    exit;;
          (-m40 | --upgrade-mongodb40 )
    mongodb_upgrade_40 || exit 2
    exit;;
          (-m42 | --upgrade-mongodb42 )
    mongodb_upgrade_42 || exit 2
    exit;;
   (-v |--version )
    fc_version_check || exit 2
    exit;;
     (-iofc |--install-fc-offline )
    fc_offline_install || exit 2
    exit;;
     (-uofc |--upgrade-fc-offline )
    fc_offline_upgrade || exit 2
    exit;;
     (-om44 |--install-mongodb44-offline )
    mongodb_upgrade_offline_44 || exit 2
    exit;;
     (-om42 |--install-mongodb42-offline )
    mongodb_upgrade_offline_42 || exit 2
    exit;;
       (-om40 |--install-mongodb40-offline )
    mongodb_upgrade_offline_40 || exit 2
    exit;;
       (-lio |--install-libreoffice-offline )
    libreoffice_install_offline || exit 2
    exit;;
         (-luo |--upgrade-libreoffice-offline )
    libreoffice_upgrade_offline || exit 2
    exit;;

        \? )
      echo "Invalid Option: -$option" 1>&2
      exit 1
      ;;
  esac

done
exit $res


