#!/bin/bash

#
# Install Atrium
#

#
# Example : Generate user_password Argon2 encoded hash for Atrium
# echo $(scw-userdata user_password) | argon2 $(echo $RANDOM | md5sum | head -c 10) -id -l 32 -e
#

export USER_PASSWORD_HASH=$(echo -n "$(scw-userdata user_password)" | argon2 "$(echo $RANDOM | md5sum | head -c 12)" -id -l 32 -e)

mkdir -p /root/atrium/data && mkdir -p /root/atrium/letsencrypt_cache
curl https://forge.grandlyon.com/systemes-dinformation/alpha/projects/labo-rust/-/raw/develop/instance-scripts/config-files/atrium.yaml -o root/atrium/atrium.yaml

sed -i "s|%atrium_hostname%|$(scw-userdata atrium_hostname)|g" /root/atrium/atrium.yaml
sed -i "s|%project_domain%|$(scw-userdata project_domain)|g" /root/atrium/atrium.yaml
sed -i "s|%atrium_letsencrypt_email%|$(scw-userdata atrium_letsencrypt_email)|g" /root/atrium/atrium.yaml
sed -i "s|%atrium_count_index%|$(scw-userdata atrium_count_index)|g" /root/atrium/atrium.yaml
sed -i "s|%user_password_hash%|$USER_PASSWORD_HASH|g" /root/atrium/atrium.yaml
sed -i "s|%user_password%|$(scw-userdata user_password)|g" /root/atrium/atrium.yaml
chown -Rf 1000:1000 /root/atrium

#
# Run Atrium Container
#

docker run -d --name atrium \
       --restart unless-stopped \
       --net=host \
       -v /root/atrium/atrium.yaml:/app/atrium.yaml \
       -v /root/atrium/letsencrypt_cache:/app/letsencrypt_cache \
       -v /root/atrium/data:/app/data nicolaspernoud/atrium:latest


