#! /bin/sh -e
#  
#   Copyright (C) 2001  Gaetano Paolone (bigpaul) <bigpaul@debian.org>
#                       Roberto Kaitsas <robk@linuxlinks>
#  
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#  
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#  
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

progname=$(basename $0)

if [ "$USER" = "" ]
then
    USER=$(whoami)
fi

DATABASE='odontolinux'

function execute()
{
    echo -n "** Running:     "
    echo "$1"
    eval "$1"
    echo
}


function create_postgres_tables()
{
    echo | $DBCOMMAND $DATABASE <<EOF 
CREATE SEQUENCE "anagrafica_id_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "pianocura_idpc_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "storico_idst_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "fattura_idfattura_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "pagamenti_idpagamenti_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "fattura_idvocefattura_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;
CREATE SEQUENCE "pagamenti_idvocepagamenti_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1  cache 1 ;

CREATE TABLE "anagrafica" (
	"id" integer DEFAULT nextval('"anagrafica_id_seq"'::text) NOT NULL,
	"dataarrivo" date,
	"cognome" character varying(30),
	"nome" character varying(30),
	"sesso" character(1),
	"luogonascita" character varying(50),
	"datanascita" date,
	"via" character varying(100),
	"cap" character varying(5),
	"citta" character varying(50),
	"professione" character varying(20),
	"recapito" character varying(100),
	"telefono" character varying(100),
	"codicefiscale" character varying(16),
	"presentato" character varying(30)
);

CREATE TABLE "pagamenti" (
	"id" smallint,
	"cognome" character varying(30),
	"nome" character varying(30),
	"data" date,
	"prestazione" character varying(80),
	"quantita" smallint,
	"prezzounitario" double precision,
	"prezzodovuto" double precision,
	"prezzodatooggi" double precision,
	"rimanedapagare" double precision,
	"tipopagamento" character varying(80),
	"note" character varying(80),
	"idpagamenti" integer DEFAULT nextval('"pagamenti_idpagamenti_seq"'::text) NOT NULL,
	"idvocepagamenti" integer DEFAULT nextval('"pagamenti_idvocepagamenti_seq"'::text)
);

CREATE TABLE "storico" (
	"id" smallint,
	"cognome" character varying(30),
	"nome" character varying(30),
	"data" date,
	"prestazione" character varying(200),
	"operatore" character varying(5),
	"idst" integer DEFAULT nextval('"storico_idst_seq"'::text) NOT NULL
);

CREATE TABLE "pianocura" (
	"id" smallint,
	"data" date,
	"cognome" character varying(30),
	"nome" character varying(30),
	"prestazione" character varying(200),
	"posizione" character varying(50),
	"quantita" smallint,
	"prezzounitario" double precision,
	"prezzomoltiplicato" double precision,
	"idpc" integer DEFAULT nextval('"pianocura_idpc_seq"'::text) NOT NULL,
	"annotazioni" text,
	"vocepianocura" character varying(30)
);

CREATE TABLE "fattura" (
	"id" smallint,
	"data" date,
	"cognome" character varying(30),
	"nome" character varying(30),
	"luogonascita" character varying(50),
	"datanascita" date,
	"via" character varying(100),
	"cap" character varying(5),
	"citta" character varying(50),
	"codicefiscale" character varying(16),
	"prestazione" character varying(200),
	"quantita" smallint,
	"prezzounitario" double precision,
	"prezzomoltiplicato" double precision,
	"idfattura" integer DEFAULT nextval('"fattura_idfattura_seq"'::text) NOT NULL,
	"idvocefattura" integer DEFAULT nextval('"fattura_idvocefattura_seq"'::text) NOT NULL
);
	\q
EOF
}

function exit_if_exists()
{
    set +e
	result=`$DBCOMMAND template1 -l | grep $DATABASE`
    set -e
    if [ "$result" != "" ]
    then
	echo ""
	echo "*** Error: Database exists ***"
	echo ""
	echo "The $DATABASE database already exists"
	echo "This script is not needed to setup the database."
	echo "You might still want to look at its source to write"
	echo "your own script for populating the $DATABASE database."
	echo ""
	echo "If you want to delete the exiting database, use"
	echo "    dropdb $DATABASE "
	echo ""
	exit 1
    fi
}

function exit_if_root()
{
    if [ "$1" == "root" ]
    then
	echo ""
	echo "*** Error: Root usage ***"
	echo ""
	echo "We do not recommend that you run this script as root"
	echo "Please re-run it as a normal user to setup $DATABASE."
	echo "If you insist, then please uncomment this test first."
	echo ""
	exit 1
#    else
#	echo "Good: We are not running as root"
    fi
}

function exit_if_no_postgres_user()
{
    set +e
    ( $DBCOMMAND template1 -c "select * from pg_user;" | grep $1 ) \
			    >/dev/null 2>&1 
    if [ $? -eq 1 ]
    then
	echo ""
	echo "*** Error: No postgresql user '$1'"
	echo ""
	echo "We were unable to start psql as the user '$1' does not exist"
	echo "You need to create a Postgresql user '$1' first:"
	echo ""
	echo "    Change to user postgres:		$ su - postgres"
	echo "    Create the user:       		# createuser $1 -d -A"
	echo "    Exit from user postgres:		# exit"
	echo ""
	echo "and then run this script again."
	echo ""
	exit 1
#    else
#	echo "Good: Postgresql can be accessed by $1"
    fi
    set -e
}


DBCOMMAND="psql -q"

# test if we are running this as root
exit_if_root $USER

# test if database can be accessed by user
exit_if_no_postgres_user $USER

# test if $DATABASE exists and exit if true
exit_if_exists

echo "Creating $DATABASE database"
execute "createdb $DATABASE"
echo ""

echo "Creating $DATABASE database tables"
create_postgres_tables 

