#!/usr/bin/env bash
#
# Copyright 2008, Centova Technologies Inc.
#
# This script is used with re-issuable licenses to change the licensed domain
# name for your Centova Cast installation.
#
# Be sure to login to your client area at www.centova.com and click the
# 'Re-issue license' link before running this script.
#

if [ $# -lt 1 ]; then
	echo "Usage: $0 <new-domain-name>"
	echo ""
	echo "Note that if you have not re-issued your license from your client area at"
	echo "centova.com, this script will do absolutely nothing useful."
	exit 1
fi
RUNASCC=`dirname $0`/../system/runascc/runascc

NEWDOMAIN="$1"

echo "Attempting to re-issue license to $NEWDOMAIN ..."
RES=`$RUNASCC exec ccmanage reissuelicense "$NEWDOMAIN"`
OK=`echo "$RES" | grep -c "^OK"`
if [ $OK -gt 0 ]; then

	ACTIVEDOMAIN=`$RUNASCC exec ccmanage sanitycheck nopassword | sed 's/^OK //'`
	if [ "$ACTIVEDOMAIN" == "$NEWDOMAIN" ]; then
		echo "License successfully re-issued for $ACTIVEDOMAIN"
	else
		cat <<EOF
Re-issuance failed -- original license still active for your original domain:
     ${ACTIVEDOMAIN}

It's likely that you forgot to login to your client area at www.centova.com and
click the 'Re-issue license' link before running this script.
EOF
		exit 1
	fi
else
	echo "License re-issuance failed.  Output was:"
	echo $RES
	exit 1
fi
exit 0

