#!/usr/bin/with-contenv bash

##############################################################################
# Migration sequence
# Detect if there is a mounted volume on /wazuh-migration and copy the data
# to /var/ossec, finally it will create a flag ".migration-completed" inside
# the mounted volume
##############################################################################

function __colortext()
{
  echo -e " \e[1;$2m$1\e[0m"
}

function echogreen()
{
  echo $(__colortext "$1" "32")
}

function echoyellow()
{
  echo $(__colortext "$1" "33")
}

function_wazuh_migration(){
  if [ -d "/wazuh-migration" ]; then
    if [ ! -e /wazuh-migration/.migration-completed ]; then
      if [ ! -e /wazuh-migration/global.db ]; then
        echoyellow "The volume mounted on /wazuh-migration does not contain all the correct files."
        return
      fi

      \cp -f /wazuh-migration/data/etc/ossec.conf /var/ossec/etc/ossec.conf
      chown root:ossec /var/ossec/etc/ossec.conf
      chmod 640 /var/ossec/etc/ossec.conf

      \cp -f /wazuh-migration/data/etc/client.keys /var/ossec/etc/client.keys
      chown ossec:ossec /var/ossec/etc/client.keys
      chmod 640 /var/ossec/etc/client.keys

      \cp -f /wazuh-migration/data/etc/sslmanager.cert /var/ossec/etc/sslmanager.cert
      \cp -f /wazuh-migration/data/etc/sslmanager.key /var/ossec/etc/sslmanager.key
      chown root:root /var/ossec/etc/sslmanager.cert /var/ossec/etc/sslmanager.key
      chmod 640 /var/ossec/etc/sslmanager.cert /var/ossec/etc/sslmanager.key

      \cp -f /wazuh-migration/data/etc/shared/default/agent.conf /var/ossec/etc/shared/default/agent.conf
      chown ossec:ossec /var/ossec/etc/shared/default/agent.conf
      chmod 660 /var/ossec/etc/shared/default/agent.conf

      \cp -f /wazuh-migration/data/etc/decoders/* /var/ossec/etc/decoders/
      chown ossec:ossec /var/ossec/etc/decoders/*
      chmod 660 /var/ossec/etc/decoders/*

      \cp -f /wazuh-migration/data/etc/rules/* /var/ossec/etc/rules/
      chown ossec:ossec /var/ossec/etc/rules/*
      chmod 660 /var/ossec/etc/rules/*

      if [ -e /wazuh-migration/data/agentless/.passlist ]; then
        \cp -f /wazuh-migration/data/agentless/.passlist /var/ossec/agentless/.passlist
        chown root:ossec /var/ossec/agentless/.passlist
        chmod 640 /var/ossec/agentless/.passlist
      fi

      \cp -f /wazuh-migration/global.db /var/ossec/queue/db/global.db
      chown ossec:ossec /var/ossec/queue/db/global.db
      chmod 640 /var/ossec/queue/db/global.db

      # mark volume as migrated
      touch /wazuh-migration/.migration-completed

      echogreen "Migration completed succesfully"
    else
      echoyellow "This volume has already been migrated. You may proceed and remove it from the mount point (/wazuh-migration)"
    fi
  fi
}

# Migrate data from /wazuh-migration volume
function_wazuh_migration

# Start Wazuh
/var/ossec/bin/ossec-control start
