preinstall scriptlet (using /bin/sh):
# test for bacula database older than version 12
# note: this ASSUMES no password has been set for bacula database
DB_VER=`mysql 2>/dev/null bacula -e 'select * from Version;'|tail -n 1`



if [ -n "$DB_VER" ] && [ "$DB_VER" -lt "11" ]; then
    echo "This bacula upgrade will update a bacula database from version 11 to 12."
    echo "You appear to be running database version $DB_VER. You must first update"
    echo "your database to version 11 and then install this upgrade. The alternative"
    echo "is to use /usr/lib64/bacula/drop_mysql_tables to delete all your your current"
    echo "catalog information, then do the upgrade. Information on updating a"
    echo "database older than version 11 can be found in the release notes."
    exit 1
fi


# check for and copy /etc/bacula/console.conf to bconsole.conf
if [ -s /etc/bacula/console.conf ];then
    cp -p /etc/bacula/console.conf /etc/bacula/bconsole.conf
fi

# create the daemon users and groups
# first create the groups if they don't exist
HAVE_BACULA=`grep bacula /etc/group 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
        /usr/sbin/groupadd -r bacula > /dev/null 2>&1
        echo "The group bacula has been added to /etc/group."
        echo "See the manual chapter \"Running Bacula\" for details."
fi
HAVE_BACULA=`grep disk /etc/group 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
        /usr/sbin/groupadd -r disk > /dev/null 2>&1
        echo "The group disk has been added to /etc/group."
        echo "See the manual chapter \"Running Bacula\" for details."
fi
# now create the users if they do not exist
# we do not use the -g option allowing the primary group to be set to system default
# this will be a unique group on redhat type systems or the group users on some systems
HAVE_BACULA=`grep bacula /etc/passwd 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
        /usr/sbin/useradd -r -c "Bacula" -d /var/lib/bacula -g disk -M -s /sbin/nologin bacula > /dev/null 2>&1
        echo "The user bacula has been added to /etc/passwd."
        echo "See the manual chapter \"Running Bacula\" for details."
fi
HAVE_BACULA=`grep bacula /etc/passwd 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
        /usr/sbin/useradd -r -c "Bacula" -d /var/lib/bacula -g bacula -M -s /sbin/nologin bacula > /dev/null 2>&1
        echo "The user bacula has been added to /etc/passwd."
        echo "See the manual chapter \"Running Bacula\" for details."
fi
HAVE_BACULA=`grep root /etc/passwd 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
        /usr/sbin/useradd -r -c "Bacula" -d /var/lib/bacula -g bacula -M -s /sbin/nologin root > /dev/null 2>&1
        echo "The user root has been added to /etc/passwd."
        echo "See the manual chapter \"Running Bacula\" for details."
fi
# now we add the supplementary groups, this is ok to call even if the users already exist
# we only do this if the user is NOT root
IS_ROOT=bacula
if [ "$IS_ROOT" != "root" ]; then
/usr/sbin/usermod -G bacula bacula
fi
IS_ROOT=bacula
if [ "$IS_ROOT" != "root" ]; then
/usr/sbin/usermod -G bacula,disk bacula
fi
IS_ROOT=root
if [ "$IS_ROOT" != "root" ]; then
/usr/sbin/usermod -G bacula root
fi
postinstall scriptlet (using /bin/sh):
# add our links
if [ "$1" -ge 1 ] ; then
  /sbin/chkconfig --add bacula-dir
  /sbin/chkconfig --add bacula-fd
  /sbin/chkconfig --add bacula-sd
fi


#check, if mysql can be called successfully at all
if mysql 2>/dev/null bacula -e 'select * from Version;' ; then

# test for an existing database
# note: this ASSUMES no password has been set for bacula database
DB_VER=`mysql 2>/dev/null bacula -e 'select * from Version;'|tail -n 1`

# grant privileges and create tables if they do not exist
if [ -z "$DB_VER" ]; then
    echo "Hmm, it doesn't look like you have an existing database."
    echo "Granting privileges for MySQL user bacula..."
    /usr/lib64/bacula/grant_mysql_privileges
    echo "Creating MySQL bacula database..."
    /usr/lib64/bacula/create_mysql_database
    echo "Creating bacula tables..."
    /usr/lib64/bacula/make_mysql_tables

# check to see if we need to upgrade a 3.x database
elif [ "$DB_VER" -lt "11" ]; then
    echo "This release requires an upgrade to your bacula database."
    echo "Backing up your current database..."
    mysqldump -f --opt bacula | bzip2 > /var/lib/bacula/bacula_backup.sql.bz2
    echo "Upgrading bacula database ..."
    /usr/lib64/bacula/update_mysql_tables
    echo "If bacula works correctly you can remove the backup file /var/lib/bacula/bacula_backup.sql.bz2"

fi
fi



if [ -d /etc/bacula ]; then
   cd /etc/bacula
   for string in XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX; do
      pass=`openssl rand -base64 33`
      for file in *.conf; do
         sed "s@${string}@${pass}@g" $file > $file.new
         cp -f $file.new $file; rm -f $file.new
      done
   done
# put actual hostname in conf file
   host=`hostname`
   string="XXX_HOSTNAME_XXX"
   for file in *.conf; do
      sed "s@${string}@${host}@g" $file >$file.new
      cp -f $file.new $file; rm -f $file.new
   done
fi
/sbin/ldconfig
exit 0 # always exit successfull, as otherwise opensuse build service complains
preuninstall scriptlet (using /bin/sh):

# delete our links
if [ $1 = 0 ]; then
  /sbin/chkconfig --del bacula-dir
  /sbin/chkconfig --del bacula-fd
  /sbin/chkconfig --del bacula-sd
fi
/sbin/ldconfig

# added: run ldconfig in postun
postuninstall scriptlet (using /bin/sh):
/sbin/ldconfig
