#!/bin/bash
#Copyright ITIS AG 2025
# VARS:
UID_BUSINESS=1000
GID_BUSINESS=1000
ADMIN_PW=$(pwgen  16 | head)

# Check if upgrade from Vers. 14 ( not supported - DBs need migration or new DBs)
while [ $# -gt 0 ]; do
  case "$1" in
    "--old_version")
      shift
      old_version="$1"
      shift
      ;;
  "--error-file")
      shift
      errorfile="$1"
      shift
      ;;
    *)
      shift
      ;;
  esac
done

if [ "$old_version" = "14.0"  ] && [ "$(ucr get itiseasybusiness/ready)" != "18" ]; then
  echo "Upgrading from version 14 to 18 requires a manual database migration. " >> $errorfile
  echo "Either you delete all ITISeasybusiness Databases from the system and then you set the UCR Variable  "itiseasybusiness/ready" to "18". Your partner can support you on this. If you want to keep the data please contact ITIS AG for data migration " >> "$errorfile"
  exit 1
fi

#Store master password for users
mkdir -p /var/lib/univention-appcenter/apps/itiseasybusiness/
echo "$ADMIN_PW" > /var/lib/univention-appcenter/apps/itiseasybusiness/ITIS_easy_business_DB_Password.txt

# Creating Extra Addons Folder for ITISEASYBUSINESS to store other modules 

mkdir -p /var/lib/univention-appcenter/apps/itiseasybusiness/data/extra-addons/
chown "$UID_BUSINESS:$GID_BUSINESS" /var/lib/univention-appcenter/apps/itiseasybusiness/data/extra-addons/

# Create folder for configuration
mkdir -p /var/lib/univention-appcenter/apps/itiseasybusiness/conf/
chown "$UID_BUSINESS:$GID_BUSINESS" /var/lib/univention-appcenter/apps/itiseasybusiness/conf/

# Create folder for Sessions and Filestore
mkdir -p /var/lib/univention-appcenter/apps/itiseasybusiness/data/
chown -R "$UID_BUSINESS:$GID_BUSINESS" /var/lib/univention-appcenter/apps/itiseasybusiness/data/

# Getting num of cores / memory information from system:
NUM_CPUS=$(nproc)
if [ $NUM_CPUS -gt 2 ]; then
# Reserve CPUs on the Host for other operations
NUM_CPUS=$((NUM_CPUS-2))
fi
# Calculating workers:
NUM_CPUS=$((NUM_CPUS*2))

# Calculating Memory configuration
# Minimum Values (Bytes) per worker
LIMIT_HARD=2684354560
LIMIT_SOFT=2147483648

# Getting System Memory:
TOT_MEM=$(grep MemTotal /proc/meminfo | awk '{print $2}') 
TOT_MEM=$((TOT_MEM*1024));
WORKER_MEM=$((LIMIT_SOFT*NUM_CPUS))

# Generating Odoo Server configuration

echo "
[options]
addons_path = /opt/itiseasybusiness/addons,/opt/itiseasybusiness/addons_itis,/opt/itiseasybusiness/addons_oca/,/opt/itiseasybusiness/addons_extra
admin_passwd = $ADMIN_PW
csv_internal_sep = ,
data_dir = /home/itiseasybusiness/.local/share/ITISeasybusiness/
db_maxconn = 1000
dbfilter = .*
demo = {}
email_from = False
import_partial =
limit_memory_hard = $LIMIT_HARD
limit_memory_soft = $LIMIT_SOFT
limit_request = 81920
limit_time_cpu = 6000
limit_time_real = 12000
limit_time_real_cron = -1
list_db = True
log_db = False
log_level = info
logfile = /var/log/itiseasybusiness-server.log
logrotate = False
longpolling_port = 8072
max_cron_threads = 1
osv_memory_age_limit = 1.0
osv_memory_count_limit = False
proxy_mode = True
reportgz = False
#server_wide_modules = none
smtp_password = False
smtp_port = 25
smtp_server = localhost
smtp_ssl = False
smtp_user = False
syslog = False
test_commit = False
test_enable = False
test_file = False
test_report_directory = False
translate_modules = ['all']
unaccent = False
without_demo = False
#workers = $NUM_CPUS
workers = 0
xmlrpc = True
xmlrpc_interface =
xmlrpc_port = 8069

" > /var/lib/univention-appcenter/apps/itiseasybusiness/conf/openerp-server.conf
chown "$UID_BUSINESS:$GID_BUSINESS" /var/lib/univention-appcenter/apps/itiseasybusiness/conf/openerp-server.conf

exit 0;