#!/usr/bin/env bash

if [ ! $UID -eq 0 ]
then
    echo "You should run this script as root"
    exit 1
fi

echo "A new postgres user and database will be created"
echo "with opengnsys as name."
echo ""
echo "A password will be asked. Use opengnsys as password for default."
echo "If you use other password, change the line DB in config.py"
echo "before run this script"
echo "example DB = 'postgresql://dbuser:passwd@localhost/dbname'"

echo ""
echo "Continue? (Y/n)"
read cont
case $cont in n|N)
    echo "Doing nothing, bye"
    exit 1
    ;;
esac

echo ""
echo "creating user opengnsys..."
su -c "createuser -D -R -S -P opengnsys" postgres
echo ""
echo "creating database opengnsys..."
su -c "createdb -T template0 -E UTF8 opengnsys -O opengnsys" postgres

echo "trying to create database..."
/usr/bin/env python db.py
/usr/bin/env bash i18n_make.sh
/usr/bin/env python fill_data.py
