#!/bin/sh
# This script is run after upgrade to convert the old license file to
# the new license format.  The locking is needed because each node must
# run this to ensure the most up-to-date version of the licenses

LOCKFILE=/ifs/.ifsvar/LICENSE_LOCK
MIGRATED_FLAG=/ifs/.ifsvar/lic_migration.done
MIGRATION_FILE=/ifs/.ifsvar/lic_migration.xml

case $# in
	0)
		exec lockf $LOCKFILE $0 under-lock
		;;
	1)
		HOSTNAME=`hostname`
		# If migrated flag exists, that means we completed an upgrade
		# through to a elicensing enabled OneFS and we never need to
		# do another license migration again.
		# The migrated flag is touched in a post-commit hook.
		if [ ! -f $MIGRATED_FLAG ]; then
			# Migrate the licenses
			echo $HOSTNAME migrating licenses...
			/usr/sbin/isi_license_migration_tool || exit 1
			echo $HOSTNAME migrated licenses
			if [ -f $MIGRATION_FILE ]; then
				cat $MIGRATION_FILE
				echo $HOSTNAME adding migration licenses...
				/usr/sbin/isi_add_migration_license || exit 1
				echo $HOSTNAME added migration licenses
				# If $MIGRATION_FILE still exists after
				# isi_add_migration_license then we failed to add the
				# license and we should remove it so we don't have to
				# clean it up on rollback or in a plus 1 upgrade
				if [ -f $MIGRATION_FILE ]; then
					rm -f $MIGRATION_FILE
				fi
			else
				echo $HOSTNAME no migration license file to add
			fi
		else
			echo $HOSTNAME $MIGRATED_FLAG exists: no lic migration to do.
		fi
		;;
esac

