#!/bin/sh #------------------------------------------------------------------------------- # Copyright (c) 2004 Media Development Loan Fund # # This file is part of the LiveSupport project. # http://livesupport.campware.org/ # To report bugs, send an e-mail to bugs@campware.org # # LiveSupport 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. # # LiveSupport 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 LiveSupport; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # Author : $Author$ # Version : $Revision$ # Location : $URL$ #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # This script sets up the development environment for a user. # # Invoke as: # ./bin/user_setup_root.sh # # To get usage help, try the -h option #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Determine directories, files #------------------------------------------------------------------------------- reldir=`dirname $0`/.. basedir=`cd $reldir; pwd;` bindir=$basedir/bin etcdir=$basedir/etc docdir=$basedir/doc srcdir=$basedir/src tmpdir=$basedir/tmp toolsdir=$srcdir/tools modules_dir=$srcdir/modules products_dir=$srcdir/products usrdir=`cd $basedir/usr; pwd;` #------------------------------------------------------------------------------- # Print the usage information for this script. #------------------------------------------------------------------------------- printUsage() { echo "LiveSupport user database setup script."; echo "parameters:"; echo ""; echo " -u, --user The user to set up the environment for."; echo " Required parameter."; echo " -h, --help Print this message and exit."; echo ""; } #------------------------------------------------------------------------------- # Process command line parameters #------------------------------------------------------------------------------- CMD=${0##*/} opts=$(getopt -o hu: -l help,user: -n $CMD -- "$@") || exit 1 eval set -- "$opts" while true; do case "$1" in -h|--help) printUsage; exit 0;; -u|--user) user=$2; shift; shift;; --) shift; break;; *) echo "Unrecognized option $1."; printUsage; exit 1; esac done if [ "x$user" == "x" ]; then echo "Required parameter user missing."; printUsage; exit 1; fi echo "Creating the LiveSupport user database"; echo "for user: $user."; echo "" #------------------------------------------------------------------------------- # The details of installation #------------------------------------------------------------------------------- postgres_user=postgres ls_database=LiveSupport-$user ls_dbuser=test ls_dbpassword=test ls_dbserver=localhost #------------------------------------------------------------------------------- # Function to check for the existence of an executable on the PATH # # @param $1 the name of the exectuable # @return 0 if the executable exists on the PATH, non-0 otherwise #------------------------------------------------------------------------------- check_exe() { if [ -x "`which $1 2> /dev/null`" ]; then echo "Executable $1 found..."; return 0; else echo "Executable $1 not found..."; return 1; fi } #------------------------------------------------------------------------------- # Check to see if this script is being run as root #------------------------------------------------------------------------------- if [ `whoami` != "root" ]; then echo "Please run this script as root."; exit ; fi #------------------------------------------------------------------------------- # Check for required tools #------------------------------------------------------------------------------- echo "Checking for required tools..." check_exe "psql" || exit 1; check_exe "odbcinst" || exit 1; #------------------------------------------------------------------------------- # Create the necessary database user and database itself #------------------------------------------------------------------------------- echo "Creating database and database user..."; # FIXME: the below might not work for remote databases su - $postgres_user -c "echo \"CREATE USER $ls_dbuser \ ENCRYPTED PASSWORD '$ls_dbpassword' \ CREATEDB NOCREATEUSER;\" \ | psql template1" \ || echo "Couldn't create database user $ls_dbuser."; su - $postgres_user -c "echo \"CREATE DATABASE \\\"$ls_database\\\" \ OWNER $ls_dbuser ENCODING 'utf-8';\" \ | psql template1" \ || echo "Couldn't create database $ls_database."; # TODO: check for the success of these operations somehow #------------------------------------------------------------------------------- # Create the ODBC data source and driver #------------------------------------------------------------------------------- echo "Creating ODBC data source and driver..."; odbcinst_template=$products_dir/scheduler/etc/odbcinst_template odbc_template=$products_dir/scheduler/etc/odbc_template odbc_template_tmp=/tmp/odbc_template.$$ replace_sed_string="s/ls_database/$ls_database/; \ s/ls_dbserver/$ls_dbserver/;" # check for an existing PostgreSQL ODBC driver, and only install if necessary odbcinst_res=`odbcinst -q -d | grep "\[PostgreSQL\]"` if [ "x$odbcinst_res" == "x" ]; then echo "Registering ODBC PostgreSQL driver..."; odbcinst -i -d -v -f $odbcinst_template || exit 1; fi echo "Registering LiveSupport ODBC data source..."; cat $odbc_template | sed -e "$replace_sed_string" > $odbc_template_tmp odbcinst -i -s -l -f $odbc_template_tmp || exit 1; rm -f $odbc_template_tmp #------------------------------------------------------------------------------- # Say goodbye #------------------------------------------------------------------------------- echo "Done."