#!/bin/bash
if [ "$2" == "debug" ]; then set -x; fi
applicationId="9nwGgQtrm7XXXXXXX"
secretkey="lsqxaXXXrdMcXXXXXXXXXXXXXXXXX"
URL="/api/0.6/pair/$1"
LATCH="/ftp-data/latch.accounts"
if [ -z "$1" ]; then
echo -e "\\nUsage: SITE LATCH-REG <pair-key>\\n"
exit 0
fi
if [ `grep "^$USER:" $LATCH | wc -l` -ne 0 ]; then
echo -e "\\nAlready registered\\n"
exit 0
fi
requestSignature+="GET\\n"
date=`date -u \'+%Y-%m-%d %H:%M:%S\'`
requestSignature+="$date\\n\\n$URL"
signed=`echo -en "$requestSignature" | openssl dgst -sha1 -hmac "$secretkey" -binary|sed -e \'s|.*= \\(.*\\)|\\1|g\'`
b64signed=`echo -n "$signed"|base64`
auth_header="Authorization:11PATHS $applicationId $b64signed"
date_header="X-11Paths-Date: $date"
JSON=`wget -q --no-check-certificate -O - --header "$auth_header" --header "$date_header" "https://latch.elevenpaths.com$URL"`
accountid=`echo "$JSON" | sed -e \'s|.*accountId":"\\(.*\\)"}.*|\\1|\'| sed -e \'s|\\(.*\\)".*|\\1|g\'`
if [ -z $accountid ]; then
echo "Error."
else
echo "$USER:$accountid" >> $LATCH
echo -e "\\nDone.\\n"
fi