#!/bin/sh
#-----------------------------------------------------------------------------
# /var/install/bin/postgresql-tools-restore
#
# Copyright (c) 2005 Daniel Vogel <daniel_vogel(at)t-online(dot)de>
#
# Creation:     09.10.2005 dv
# Last Update:  $Id$
#
# 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.
#-----------------------------------------------------------------------------

#include eislib
. /var/install/include/eislib

. /etc/config.d/postgresql

PROG=/usr/local/pgsql/bin/pg_restore

clrhome

# Mount backup media
if [ ! -z "$POSTGRESQL_BACKUP_MOUNT" ]
then
    eval ${POSTGRESQL_BACKUP_MOUNT} 2>&1
    if [ "$?" != "0" ]
    then
        echo "unable to execute: $POSTGRESQL_BACKUP_MOUNT"
        anykey
        exit 1
    fi
fi

# select backup file
mecho -info "Select backup file:"
backup_files=`/bin/ls -t "$POSTGRESQL_BACKUP_TARGET/"*.backup 2>/dev/null`
c="1"
for file in $backup_files
do
    mecho -info -n "$c  "
    mecho "$file"
    c=`/usr/bin/expr $c + 1`
done

if [ "$c" -le "1" ]
then
    mecho "No backup files found!"
else
    c=`/usr/bin/expr $c - 1`

    idx="1"
    mecho
    idx=`/var/install/bin/ask "Select (0 = Cancel)" "$idx" "0-$c"`

    if [ "$idx" -eq "0" ]
    then
        mecho -info "command canceled"
    else
        c="1"
        backupfile=""
        for file in $backup_files
        do
            if [ "$c" -eq "$idx" ]
            then
               backupfile="$file"
            fi
            c=`/usr/bin/expr $c + 1`
        done

        echo "File selected: $backupfile"

        echo
        mecho -info "Select database to restore to:"

        echo "select datname from pg_database order by datname;" | \
             /usr/local/pgsql/bin/psql template1 postgres
        echo -e 'database: \c'
        read database

        if [ "$database" = "" ]
        then
            mecho -info "command canceled"
        else
            echo
            mecho -info "Select database user for restore:"
            echo "select usename from pg_user;" | \
                 /usr/local/pgsql/bin/psql template1 postgres

            echo -e 'Database user [postgres]: \c'
            read dbuser

            if [ "$dbuser" = "" ]
            then
               dbuser='postgres'
            fi

            # perform backup
            $PROG -i -U "$dbuser" -v -d "$database" "$backupfile"
            if [ "$?" = "0" ]
            then
                echo ""
                echo "Database restored successfully!"
                echo ""
            else
                echo ""
                echo "Some errors occured!"
                echo ""
            fi
        fi
    fi

    # Unount
    if [ ! -z "$POSTGRESQL_BACKUP_UMOUNT" ]
    then
        eval ${POSTGRESQL_BACKUP_UMOUNT} 2>&1
        if [ "$?" != "0" ]
        then
            echo "unable to execute: $POSTGRESQL_BACKUP_UMOUNT"
        fi
    fi
fi

anykey
exit 0