#!/bin/bash

# Start a Ziniki running
# This assumes that a FLAS+Ziniki package has been downloaded and this script is $ZINIKI_ROOT/bin/ziniki

root="`dirname $0`"
root="`dirname $root`"

ZINIKI=""
CRED=""

while [ $# -gt 0 ] ; do
  case "$1" in
    *)
      if [ -z "$ZINIKI" ] ; then
        ZINIKI="$1"
      elif [ -z "$CRED" ] ; then
        CRED="$1"
      else
        echo "Usage: zinlog <ziniki-admin> <cred>"
        exit 1
      fi
      shift
      ;;
  esac
done

if [ -z "$CRED" ] ; then
  echo "Usage: zinlog <ziniki> <cred>"
  exit 1
fi

curl --silent --show-error -o $HOME/.zintok -d cred="$CRED" "$ZINIKI/fakelogin"
ZINTOK=`cat $HOME/.zintok`
case "$ZINTOK" in
  "{"*)
    echo "logged in"
    ;;
  *)
    echo $ZINTOK
    rm $HOME/.zintok
    ;;
esac