#!/bin/bash

set -e

# Get libcrypto.so full path from CCM
ccm="citrix-crypto-module"
libcrypto=$(rpm -ql ${ccm} | grep "/libcrypto.so" | head -n 1)
if [ "${libcrypto}" == "" ]; then
    echo "Error: could not find ${ccm}"
    exit 1
fi

# Generate pool secret
echo -n $"Creating pool secret using ${libcrypto} (this may take some seconds)"
pool_secret=$(/bin/pool_secret ${libcrypto})
if [ $? -ne 0 ]; then
    echo "Error: failed to generate pool secret"
    exit 1
fi

# Write pool secret to file
token_path="/etc/xensource/ptoken"
touch ${token_path} && chmod 640 ${token_path} && echo -n ${pool_secret} > ${token_path}
echo ' ...done.'

echo "Configured pool secret"
