#!/bin/bash

set -e

while [ $# -gt 0 ]; do
    case "$1" in
        "--error-file")
            shift
            errorfile="$1"
            shift
            ;;
        "--old-version")
            shift
            old_version="$1"
            shift
            ;;
        "--version")
            shift
            version="$1"
            shift
            ;;
        *)
            shift
            ;;
    esac
done

# Create Intercom Service secret
pwdfile="/etc/intercom.secret"
[ -e "$pwdfile" ] || makepasswd --chars 20 > "$pwdfile"
chmod 640 "$pwdfile"

# Check files exist
FILE=/etc/matrix.secret
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exists"
    exit 1
fi

FILE=/etc/intercom.secret
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exists"
    exit 1
fi

FILE=/etc/intercom-client.secret
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exists"
    exit 1
fi

FILE=/etc/intercom-portal.secret
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exists"
    exit 1
fi

exit 0

