#!/bin/sh
#
# shell script TO GRANT privileges to the bacula database
#
bindir=@INGRES_BINDIR@
PATH="$bindir:$PATH"
db_name=${db_name:-@db_name@}
db_user=${db_user:-@db_user@}

sql iidbdb $* <<END-OF-DATA
CREATE USER ${db_user}
\g
END-OF-DATA

if sql -u${db_user} ${db_name} $* <<END-OF-DATA

-- for tables
GRANT ALL ON TABLE Filename TO ${db_user};
GRANT ALL ON TABLE Path TO ${db_user};
GRANT ALL ON TABLE File TO ${db_user};
GRANT ALL ON TABLE RestoreObject TO ${db_user};
GRANT ALL ON TABLE Job TO ${db_user};
GRANT ALL ON TABLE JobHisto TO ${db_user};
GRANT ALL ON TABLE Location TO ${db_user};
GRANT ALL ON TABLE Fileset TO ${db_user};
GRANT ALL ON TABLE JobMedia TO ${db_user};
GRANT ALL ON TABLE Media TO ${db_user};
GRANT ALL ON TABLE MediaType TO ${db_user};
GRANT ALL ON TABLE Storage TO ${db_user};
GRANT ALL ON TABLE Device TO ${db_user};
GRANT ALL ON TABLE Pool TO ${db_user};
GRANT ALL ON TABLE Client TO ${db_user};
GRANT ALL ON TABLE Log TO ${db_user};
GRANT ALL ON TABLE LocationLog TO ${db_user};
GRANT ALL ON TABLE Counters TO ${db_user};
GRANT ALL ON TABLE BaseFiles TO ${db_user};
GRANT ALL ON TABLE UnsavedFiles TO ${db_user};
GRANT ALL ON TABLE CDImages  TO ${db_user};
GRANT ALL ON TABLE PathHierarchy TO ${db_user};
GRANT ALL ON TABLE PathVisibility TO ${db_user};
GRANT ALL ON TABLE Version TO ${db_user};
GRANT ALL ON TABLE Status TO ${db_user};

-- for sequences ON those tables
GRANT NEXT ON SEQUENCE Filename_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Path_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE File_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE RestoreObject_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Job_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE JobHisto_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Location_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Fileset_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Jobmedia_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Media_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE MediaType_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Storage_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Device_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Pool_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Client_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE Log_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE LocationLog_Seq TO ${db_user};
GRANT NEXT ON SEQUENCE BaseFiles_Seq TO ${db_user};
\g

END-OF-DATA
then
   echo "Privileges for ${db_user} granted on ${db_name}."
   exit 0
else
   echo "Error creating privileges."
   exit 1
fi
