#!/bin/bash

# -----------------------------------------------------------------------------
# 
#   This script is getting executed from "root" permissions AFTER files
# were placed into their destinations by "builder" via compiled/TRANSIT.
#
#   It is expected to perform final set of preparations here before switching
# into low-privileged "default" user.
#
#   Good examples are:
# - patch apache configuration
# - change ownership or permissions
#
# -----------------------------------------------------------------------------

START_VAE_ENGINE_PATH="/opt/vae/vaxocr/bin/start-vae-engine"

cat <<START_SCRIPT >${START_VAE_ENGINE_PATH}
#!/bin/bash
set -e

META_SCHEMA="/opt/vae/\${VAE_NAME}/copy-to-host-vae/etc/meta-schema.json"
[ -f "\${META_SCHEMA}" ] && export META_SCHEMA_VER=\$(cksum "\${META_SCHEMA}" | cut -d' ' -f1 | xargs printf "%X\n")

if [ "\$2" = "\${VAE_NAME}" ]; then
    uuidd
    cd /root/aksusbd-8.31.1 && ./dinst
    exec "/opt/vae/\${VAE_NAME}/bin/vae-engine" "\$@"
fi

exec "\$@"
START_SCRIPT
chmod +x ${START_VAE_ENGINE_PATH}

#chmod -R ugo+rwX /opt/vae
mkdir -p /run/uuidd
find /opt/vae /run/uuidd -type d | xargs chmod -v 777

echo === Finished executing \'install\' script ===
