#!/bin/bash
#FILENAME: login.sh
#USAGE: login.sh USERNAME PASSWORD DOMAIN
USERNAME="$1"
DOMAIN="${2:-www.wikidata.org}"

read -s -p "Password: " PASSWD 
echo

RESP1=`wget -qO- --no-cookies --user-agent=LOGIN --post-data "action=login&lgname=${USERNAME}&lgpassword=${PASSWD}&format=xml" http://${DOMAIN}/w/api.php`
echo "${RESP1}
" |  hexdump -C

echo

TOKEN=`echo "${RESP1}" | sed -ne '/<login/ { s/.*token="\([^"]*\)".*/\1/; p }'`
SESSIONID=`echo "${RESP1}" | sed -ne '/<login/ { s/.*sessionid="\([^"]*\)".*/\1/; p }'`
COOKIEPREFIX=`echo "${RESP1}" | sed -ne '/<login/ { s/.*cookieprefix="\([^"]*\)".*/\1/; p }'`
 
wget -qO- --no-cookies --user-agent=LOGIN --post-data "action=login&lgname=${USERNAME}&lgpassword=${PASSWD}&lgtoken=${TOKEN}&format=xml" --header "Cookie: ${COOKIEPREFIX}_session=${SESSIONID}" http://${DOMAIN}/w/api.php | hexdump -C
echo " 
"

