#!/bin/sh

#if [ ! -f /usr/lib/x86_64-linux-gnu/libicui18n.so.63 ]; then
#    echo "ICU version 63 must be installed !"
#    echo "Please do not trick making symlinks from different version, this way is not valid"
#    echo "If you have a newer ICU version, please contact support for an updated version of DataGate"
#    exit
#fi

if [ ! -f /usr/bin/sed ]; then
    echo "sed must be installed !"
    exit
fi

#if [ ! -f /usr/lib/x86_64-linux-gnu/libtiff.so ]; then
#    echo "libtiff (libtiff5) must be installed !"
#    exit
#fi

#if [ ! -f /usr/lib/x86_64-linux-gnu/libfontconfig.so ]; then
#    echo "libfontconfig must be installed !"
#    exit
#fi

if [ ! -e /usr/lib/libfbclient.so.2 ]; then
    echo "Firebird (version 4) must be installed !"
    echo "Symlink /usr/lib/libfbclient.so.2 must be created manually and point to matching file in /opt/firebird/lib"
    exit
fi

# Ask for an instance name
echo "Please provide instance name for DataGate, if you plan to use several instances on the same server"
echo "If you have only one instance, just press ENTER (default)"
read instname
if [ -z "$instname" ]; then
  instname="default"
fi

echo
echo "Will install as instance $instname"
echo

# Check if user datagate exist, and create one if not
getent passwd datagate >/dev/null
if [ $? -eq 0 ]; then
  echo "User datagate exist, continue"
else
  echo "User datagate does not exist, adding user datagate"
  adduser --disabled-password --gecos "" --shell /bin/false datagate
fi

# Extract main image to folder datagate - all files will be symlinks for this folder
# NEVER store settings.ini and SSL certificate files in main folder !
echo "Extracting datagate files..."
if [ ! -f "/home/datagate/datagate" ]; then
  mkdir "/home/datagate/datagate/"
fi
tar -xjvf ./datagate.tar.bz2 -C "/home/datagate/datagate/"

echo "Applying file access rights and owner..."
chown -R datagate:datagate "/home/datagate/datagate/"
chmod 777 /home/datagate/datagate

# Copy required files not from main archive
if [ ! -f "/home/datagate/datagate/datagate-csr.pem" ]; then
  cp ./datagate-csr.pem "/home/datagate/datagate/"
fi
if [ ! -f "/home/datagate/datagate/datagate-key.pem" ]; then
  cp ./datagate-key.pem "/home/datagate/datagate/"
fi
if [ ! -d "/home/datagate/datagate/certificates" ]; then
  cp -r ./certificates "/home/datagate/datagate/"
fi

# Create instance folder and make symlinks
if [ ! -d "/home/datagate/datagate-$instname" ]; then
  mkdir "/home/datagate/datagate-$instname"
fi
if [ ! -d "/home/datagate/datagate-$instname/logs" ]; then
  mkdir "/home/datagate/datagate-$instname/logs"
fi
if [ ! -d "/home/datagate/datagate-$instname/certificates" ]; then
  mkdir "/home/datagate/datagate-$instname/certificates"
fi
if [ ! -d "/home/datagate/datagate-$instname/htdocs" ]; then
  ln -s "/home/datagate/datagate/htdocs" "/home/datagate/datagate-$instname/htdocs"
fi
# Create a hardlink to exe, as softlink works incorrectly
if [ -f "/home/datagate/datagate-$instname/datagate" ]; then
  unlink "/home/datagate/datagate-$instname/datagate"
fi
ln "/home/datagate/datagate/datagate" "/home/datagate/datagate-$instname/datagate"
if [ ! -f "/home/datagate/datagate-$instname/datagate-csr.pem" ]; then
  ln -s "/home/datagate/datagate/datagate-csr.pem" "/home/datagate/datagate-$instname/datagate-csr.pem"
fi
if [ ! -f "/home/datagate/datagate-$instname/datagate-key.pem" ]; then
  ln -s "/home/datagate/datagate/datagate-key.pem" "/home/datagate/datagate-$instname/datagate-key.pem"
fi
if [ ! -f "/home/datagate/datagate-$instname/certificates/100.public" ]; then
  ln -s "/home/datagate/datagate/certificates/100.public" "/home/datagate/datagate-$instname/certificates/100.public"
fi
chmod 777 /home/datagate/datagate-$instname/datagate

# Config file
if [ ! -f "/home/datagate/datagate-$instname/settings.ini" ]; then
  echo "Create default settings.ini file for datagate-$instname"
  echo "Please edit settings.ini to setup admin name and password, database access password, etc"
  cp ./settings.ini "/home/datagate/datagate-$instname"
  chmod 644 /home/datagate/datagate-$instname/settings.ini
fi

# Change owner for all the folder and inner
chown -R datagate:datagate "/home/datagate/datagate-$instname/"

if [ ! -f "/etc/init.d/rc-datagate-$instname" ]; then
  echo "Creating DataGate init.d links..."
  sed "s/XXX/$instname/g" <./rc-datagate >/etc/init.d/rc-datagate-$instname
  chown root:root /etc/init.d/rc-datagate-$instname
  chmod 755 /etc/init.d/rc-datagate-$instname

  ln -s /etc/init.d/rc-datagate-$instname /etc/rc2.d/S99rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc3.d/S99rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc4.d/S99rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc5.d/S99rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc0.d/K01rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc1.d/K01rc-datagate-$instname
  ln -s /etc/init.d/rc-datagate-$instname /etc/rc6.d/K01rc-datagate-$instname
fi

echo
echo "DataGate install completed."
echo "Please note important change for native Linux version (1.24.x.x):"
echo "Every DataGate instance can be started and stopped separately"
echo "Now DataGate can be stopped. process.id file in the instance folder contains PID for the currenty running process"
echo "Never delete process.id file manually, it should be automatically deleted after service stop !"
echo
echo "Run: /etc/init.d/rc-datagate-$instname start"
echo "Stop: /etc/init.d/rc-datagate-$instname stop"
echo
