#!/bin/sh
# Copyright (c) 2016-2019 Algo Communication Products Ltd.

PROV_CONF=$1
READCONF=/opt/algo/bin/readconf
HAS_SPWD=0

# If in a provisioning config the admin password appears both encrypted and
# unencrypted, remove the unencrypted password and use the encrypted one
if [ -n "$PROV_CONF" ]; then
	# Check that the admin.spwd config both exists and is not blank
	grep -q 'admin.spwd' $PROV_CONF && HAS_SPWD=1

	READCONF_OPT="-c $PROV_CONF"
	ADMIN_SPWD=$($READCONF admin.spwd $READCONF_OPT)

	if [ "$HAS_SPWD" -eq 1 ] && [ -n "$ADMIN_SPWD" ]; then
		sed -i '/admin.pwd/d' $PROV_CONF
	else
		sed -i '/admin.spwd/d' $PROV_CONF
	fi
fi

if [ -x /opt/algo/bin/prod-upgrade-settings ]; then
	/opt/algo/bin/prod-upgrade-settings $1
fi
