#!/usr/bin/env bash
#
# Copyright (c) [2013-2014] Novell, Inc.  All Rights Reserved.
#
# THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.  IT
# MAY NOT BE USED, COPIED, DISTRIBUTED, DISCLOSED, ADAPTED, PERFORMED, DISPLAYED,
# COLLECTED, COMPILED, OR LINKED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.  USE OR
# EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE PERPETRATOR
# TO CRIMINAL AND CIVIL LIABILITY.
#
# NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
# INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, AND NON-INFRINGEMENT. NOVELL, THE AUTHORS OF THE WORK, AND
# THE OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
# FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN
# THE WORK.
#

# define alias to python based on the below checks
[[ -f /usr/bin/python3 ]] && alias python=/usr/bin/python3 || { [[ -f /usr/bin/python2 ]] && alias python=/usr/bin/python2 || { [[ -f /usr/bin/python ]] && alias python=/usr/bin/python; }; }
shopt -s expand_aliases

# For time to sync at then end of installation
#rcntpd ntptimeset
# Workaround til Yast fixes this
rcnetwork restart

# Get HostName
 export VAHOSTNAME=`hostname -f`
 echo "VAHOSTNAME is $VAHOSTNAME"

# If no keystore file yet exists then generate a self-signed cert and put in /vastorage/conf/certs
 if [ ! -f /vastorage/conf/certs/keystore ];
 then
    mkdir -p /vastorage/conf/certs
    openssl req -x509 -days 730 -subj "/CN=$VAHOSTNAME" -newkey rsa:2048 -sha256 -reqexts v3_req -extensions v3_req -config <(cat /etc/ssl/openssl.cnf <(printf '[v3_req]\nsubjectAltName=DNS:%s' "$VAHOSTNAME")) -keyout /vastorage/conf/certs/vaserver.key -out /vastorage/conf/certs/vaserver.crt -passout pass:changeit 2>/dev/null
    openssl pkcs12 -export -inkey /vastorage/conf/certs/vaserver.key -in /vastorage/conf/certs/vaserver.crt -out /vastorage/conf/certs/vaserver.p12 -name self-signed_cert -passin pass:changeit -passout pass:changeit 2>/dev/null
    keytool -importkeystore -destkeystore /vastorage/conf/certs/keystore -deststorepass changeit -deststoretype jks -srckeystore /vastorage/conf/certs/vaserver.p12 -srcstorepass changeit -srcstoretype pkcs12
 fi

# If vachain.crt has not been created or is empty, create it.
# Note: when Apache is configured to use this file, it fails to start if the file is missing or empty
# Creating it with a single null character works around this.
if [ ! -s "/vastorage/conf/certs/vachain.crt" ]; then
    echo -ne "\0" > /vastorage/conf/certs/vachain.crt
fi

if [ ! -f /opt/novell/common-services/etc/keystore ]; then
    ln -s /vastorage/conf/certs/keystore /opt/novell/common-services/etc/keystore
fi

#Call script to add needed tweaks to memcached if installed.
 if [ -f /etc/sysconfig/memcached ];
 then
    echo "Echo configure memcached"
   #open port in firewall port for memcached
   /opt/novell/base_config/firewall/firewall_config.sh add_service memcached
   # Get Cachesize for Memcached
   export VAMEMCACHESIZE=`python /opt/novell/base_config/getMemcachedSize.py`
   #Set up and start memcached config file 
   python /opt/novell/base_config/updateMemcached.py "(-m $VAMEMCACHESIZE -t 8 -d -l $VAHOSTNAME)"
   # The shipping service was broken so we replace it with our own.
   /usr/bin/cp /opt/novell/base_config/memcached.service /etc/systemd/system/memcached.service
   #reload memcached with new prameters and service.
   /usr/bin/systemctl daemon-reload
   /usr/bin/systemctl enable memcached.service
   /usr/bin/systemctl stop memcached.service
   /usr/bin/systemctl start memcached.service
  
 fi

# Novell Virtual Applicance
# Firewall 9443-administration
/opt/novell/base_config/firewall/firewall_config.sh add_allowed_port 9443/tcp
/opt/novell/base_config/firewall/firewall_config.sh reload

usermod -a -G vaadmin root
# if the vaadmin user exists, add it to the vaadmin group
getent passwd vaadmin && usermod -a -G vaadmin vaadmin

#Call appliance specific Custom-build script
## After this point, the firewall cannot be reloaded. It will break Docker-based appliances.
python /opt/novell/base_config/execApplianceScript.py /opt/novell/va_install/va_firstboot


#Restore any chkconfig settings that might be saved in /vastorage/conf/chkconfig.bak for upgrade scenario
 python /opt/novell/base_config/chkconfigRestore.py
 rm -f /vastorage/conf/chkconfig.bak


#Set up Java certificate store
echo "Import any trusted certificates from /vastorage or /vashare into ca-certificates"
/opt/novell/base_config/certificates.sh reimport_trusted_certificates

#Datamodel Section
systemctl enable vabase-datamodel.service
systemctl start vabase-datamodel.service

if [ -f /vastorage/conf/vaconfig.zip ]; then
  #Autoupdate
  echo Calling /opt/novell/base_config/autoupdate.sh
  /opt/novell/base_config/autoupdate.sh
else
  #Autoinstall
  echo Calling /opt/novell/base_config/autoinstall.sh
  /opt/novell/base_config/autoinstall.sh
fi

systemctl enable vabase-jetty.service
systemctl start vabase-jetty.service

#Clean up

 #Copy Novell-VA-release file over to vastorage if it does not exist.
if [ ! -f /vastorage/conf/Novell-VA-release ];
 then
cp /etc/Novell-VA-release /vastorage/conf/
fi 

#Disable the service that runs this script
systemctl disable vabase-firstboot.service

#Enable vabase.service 
systemctl enable vabase.service
systemctl start vabase.service



