#!/bin/bash

DIR=`dirname $0`
SCHEMA=$DIR/schema

if [ $# -ne 3 ]; then
    echo "Not enough arguments: <username> <password> <authdb filename>"
    exit
fi

if [ -n "$3" ]; then
    authdb=$3
else
    authdb=authdb
fi

if [ ! -f $authdb ]; then
    if [ ! -f $SCHEMA ]; then
        echo "Missing schema file."
        exit
    fi
    sqlite3 $authdb < $SCHEMA
fi

pw=`echo -n $2 | md5sum | awk '{print $1}'`

echo "insert into users values (Null, '$1', '${pw}');" | sqlite3 $authdb
echo "insert into permissions values ((select userId from users where user='$1'),Null,Null,1,1);" | sqlite3 $authdb

