#!/bin/bash
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
password=$(ynh_app_setting_get --key=password)
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression "Stopping $app's systemd service..."
ynh_systemctl --service=$app --action="stop" --log_path="systemd"
ynh_systemctl --service=${app}_ldap --action="stop" --log_path="systemd"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression "Ensuring downward compatibility..."
# For version 0.6~ynh1 and before
if [[ ! -d "$install_dir/live" ]]
then
tempdir="$(mktemp -d)"
mv $install_dir $tempdir
mkdir -p "$install_dir/live"
mv $tempdir/$app/galene "$install_dir/live/"
mv $tempdir/$app/data/ "$install_dir/live/"
mv $tempdir/$app/static/ "$install_dir/live/"
ynh_safe_rm "$tempdir"
fi
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression "Upgrading source files..."
ynh_setup_source --dest_dir="$install_dir/build" --full_replace
ynh_setup_source --dest_dir="$install_dir/build_ldap" --source_id="ldap" --full_replace
mkdir -p "$install_dir/live/data"
mkdir -p "$install_dir/live_ldap/data"
ynh_safe_rm "$install_dir/live/static/"
cp -r "$install_dir/build/static/" "$install_dir/live/"
ynh_replace --match="
" --replace="" --file="$install_dir/live/static/galene.html"
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod -R o-rwx "$install_dir"
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown -R $app:www-data "$install_dir"
#=================================================
# BUILD APP
#=================================================
ynh_script_progression "Building app..."
ynh_go_install
pushd $install_dir/build/galene-password-generator/
ynh_hide_warnings ynh_exec_as_app CGO_ENABLED=0 go build -o $install_dir/
popd
pushd $install_dir/build/
ynh_hide_warnings ynh_exec_as_app CGO_ENABLED=0 go build -ldflags='-s -w' -o $install_dir/live/
popd
pushd $install_dir/build_ldap/
ynh_hide_warnings ynh_exec_as_app CGO_ENABLED=0 go build -ldflags='-s -w' -o $install_dir/live_ldap/
popd
ynh_go_remove
ynh_safe_rm "$install_dir/build/"
ynh_safe_rm "$install_dir/build_ldap/"
ynh_safe_rm "$install_dir/.cache/"
ynh_safe_rm "$install_dir/go/"
ynh_safe_rm "$install_dir/.go-version"
chmod +x "$install_dir/galene-password-generator"
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression "Updating configuration..."
# If password_hash doesn't exist, create it
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=password_hash --value=$(echo $($install_dir/galene-password-generator $password))
if [ -z "${password_hash:-}" ]; then
password_hash=$(echo $($install_dir/galene-password-generator $password))
ynh_app_setting_set --key=password_hash --value="$password_hash"
fi
# Configure Galène
ynh_config_add --template="config.json" --destination="$install_dir/live/data/config.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod 400 "$install_dir/live/data/config.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown $app:$app "$install_dir/live/data/config.json"
# Configure Galène LDAP
# If key doesn't exist, create it
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=key --value=$(jose jwk gen -i '{"kty":"oct","alg":"HS256"}')
if [ -z "${key:-}" ]; then
key=$(jose jwk gen -i '{"kty":"oct","alg":"HS256"}')
ynh_app_setting_set --key=key --value=$key
fi
ynh_config_add --template="galene-ldap.json" --destination="$install_dir/live_ldap/data/galene-ldap.json"
chmod 400 "$install_dir/live_ldap/data/galene-ldap.json"
chown $app:$app "$install_dir/live_ldap/data/galene-ldap.json"
# Create a group name config
ynh_config_add --template="groupname.json" --destination="$data_dir/groups/$group_name.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod 400 "$data_dir/groups/$group_name.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown $app:$app "$data_dir/groups/$group_name.json"
# Create a group name authenticated on LDAP
ynh_config_add --template="groupname-ldap.json" --destination="$data_dir/groups/YunoHost_Users.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod 400 "$data_dir/groups/YunoHost_Users.json"
#REMOVEME? Assuming the file is setup using ynh_config_add, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown $app:$app "$data_dir/groups/YunoHost_Users.json"
#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression "Upgrading system configurations related to $app..."
public_ip4="$(curl -s ip.yunohost.org)" || true
# Create a dedicated NGINX config
ynh_config_add_nginx
# Create a dedicated systemd config
ynh_config_add_systemd
ynh_config_add_systemd --service=${app}_ldap --template="ldap.service"
yunohost service add $app --description="Videoconferencing server" --needs_exposed_ports="$port_turn"
yunohost service add ${app}_ldap --description="LDAP integration for the videoconferencing server"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression "Starting $app's systemd service..."
ynh_systemctl --service=$app --action="start" --log_path="systemd"
ynh_systemctl --service=${app}_ldap --action="start" --log_path="systemd"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression "Upgrade of $app completed"