#!/bin/sh

CODE_TEMP=/tmp/ctrl_gen_code
PRIVATE_TEMP=/tmp/ctrl_private_tmp

if [ -f $CODE_TEMP ]; then
        CODE=`cat "$CODE_TEMP"`
        RET=`curl -s -G "https://localhost/cloudkey/internal/reset" --data "token=${CODE}" --insecure`
        if [ -n "$RET" ]; then
                if [ "$RET" -eq 0 ]; then
                        echo "Controller handle unbind success."
                        exit
                fi
        fi
fi

touch $PRIVATE_TEMP
private-data read $PRIVATE_TEMP
DATA=`sed ':a;N;s/[ \n\r\t]//g;ta' $PRIVATE_TEMP | grep '"accountId":"[0-9a-zA-Z]*"'`
RESET_FLAG=`echo "$DATA" | grep '"reset":true' -o`

if [ -n "$RESET_FLAG" ]; then
        echo "Already modified reset flag."
        exit
fi

ACCOUNT_ID=`echo "$DATA" | sed 's/.*"accountId":"\([0-9a-zA-Z]*\)".*/\1/'`
if [ -n "$ACCOUNT_ID" ]; then
        echo "{\"accountId\":\"$ACCOUNT_ID\",\"reset\":true}" > $PRIVATE_TEMP
        private-data write $PRIVATE_TEMP
        echo "Modify reset flag success."
fi

# modified 2018-8-2 11:43
