#!/bin/bash

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 [/path/to/db/database].db";
    exit 1;
fi

echo "Creating database file $1...";
touch "$1";
if ! [ -f "$1" ]; then
	echo "Cannot open or create database file $1, aborting.";
	exit 1;
fi

TABLE_NAME="ElectricityLog";

echo "Creating table $TABLE_NAME...";
sqlite3 $1 "CREATE TABLE IF NOT EXISTS $TABLE_NAME (\
Date TEXT NOT NULL,\
TimeUtc TEXT NOT NULL,\
CurrentPowerUsage REAL NOT NULL,\
TotalPowerConsumptionDay REAL NOT NULL,\
TotalPowerConsumptionNight REAL NOT NULL,\
CurrentPowerReturn REAL NOT NULL,\
TotalPowerReturnDay REAL NOT NULL,\
TotalPowerReturnNight REAL NOT NULL,\
DayTarifEnabled INTEGER NOT NULL,\
GasConsumptionInCubicMeters REAL NOT NULL);";

echo "Creating indexes on table $TABLE_NAME...";
sqlite3 $1 "CREATE INDEX IF NOT EXISTS idx_Date ON $TABLE_NAME (Date);"
sqlite3 $1 "CREATE INDEX IF NOT EXISTS idx_TimeUtc ON $TABLE_NAME (TimeUtc);"
