#!/bin/sh
# ----------------------------------------------------------------------------
# /tmp/postinstall-eisman.sh - eisman installation
#
# Creation   :  2013-01-16 dv
# Last update:  $Id: install.sh 41956 2016-06-18 20:33:29Z hbfl $
#
# Copyright (c) 2013-2017 the eisfair team, team(at)eisfair(dot)org
#
# 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.
# ----------------------------------------------------------------------------

# set package name
package_name=eisman

# ---------------------------------------------------------------------------
# update-2.7.5
# ---------------------------------------------------------------------------
update_2_7_5()
{
    # check for correct eisfair system
    if [ -z "${EISFAIR_SYSTEM}" ]
    then
        # get eisfair system-type
        . /var/install/include/check-eisfair-version
    fi

    # write eisfair system type to include file
    echo "EISMAN_SYSTEM=\"${EISFAIR_SYSTEM}\"" >> \
        /var/install/include/eismanlib

    # call eisman init to check the database
    eisman init

    # -----------------------------------------------------------------------
    # extract eisman_info (tarball, if required)
    # -----------------------------------------------------------------------

    tar_file=$(ls /tmp/${package_name}_info.tar.*)

    if [ ! -d /var/lib/eisman/info ]
    then
        if tar --help | grep -q 'no-overwrite-dir'
        then
            taroption1='--no-overwrite-dir'
        else
            taroption1=''
        fi

        if tar --help | grep -q 'keep-directory-symlink'
        then
            taroption2='--keep-directory-symlink'
        else
            taroption2=''
        fi

        cd /
        tar ${taroption1} ${taroption2} -xf ${tar_file}  2>/tmp/install.log

        if [ ! -s /tmp/install.log ]
        then
            rm -f /tmp/install.log
        fi
    fi

    rm -f ${tar_file}
    rm -rf /var/lib/eisman/backup
    echo 'DateTime' >/run/eisman-installed
    rm -f /var/install/deinstall/eisman
    rm -f /var/install/packages/eisman
}

# ---------------------------------------------------------------------------
# update-2.7.6
# ---------------------------------------------------------------------------
update_2_7_6()
{
    # check for correct eisfair system
    if [ -z "${EISFAIR_SYSTEM}" ]
    then
        # get eisfair system-type
        . /var/install/include/check-eisfair-version
    fi

    # write eisfair system type to include file
    echo "EISMAN_SYSTEM=\"${EISFAIR_SYSTEM}\"" >> \
        /var/install/include/eismanlib

    # call eisman init to check the database
    eisman init


    # -----------------------------------------------------------------------
    # extract eisman_info (tarball, if required)
    # -----------------------------------------------------------------------

    tar_file=$(ls /tmp/${package_name}_info.tar.*)

    # save base folder
    mkdir -p /tmp/base_tmp
    cp -r /var/lib/eisman/info/base /tmp/base_tmp

    rm -rf /var/lib/eisman/info

    if tar --help | grep -q 'no-overwrite-dir'
    then
        taroption1='--no-overwrite-dir'
    else
        taroption1=''
    fi

    if tar --help | grep -q 'keep-directory-symlink'
    then
        taroption2='--keep-directory-symlink'
    else
        taroption2=''
    fi

    cd /
    tar ${taroption1} ${taroption2} -xf ${tar_file}  2>/tmp/install.log

    # readd base folder
    cp -r /tmp/base_tmp/base /var/lib/eisman/info
    rm -rf /tmp/base_tmp

    if [ ! -s /tmp/install.log ]
    then
        rm -f /tmp/install.log
    fi

    rm -f ${tar_file}
}

# ---------------------------------------------------------------------------
# update-2.7.7
# ---------------------------------------------------------------------------
update_2_7_7()
{
    # check for correct eisfair system
    if [ -z "${EISFAIR_SYSTEM}" ]
    then
        # get eisfair system-type
        . /var/install/include/check-eisfair-version
    fi

    # write eisfair system type to include file
    echo "EISMAN_SYSTEM=\"${EISFAIR_SYSTEM}\"" >> \
        /var/install/include/eismanlib

    # call eisman init to check the database
    eisman init
}

# ---------------------------------------------------------------------------
# update-2.7.8
# ---------------------------------------------------------------------------
update_2_7_8()
{
    # check for correct eisfair system
    if [ -z "${EISFAIR_SYSTEM}" ]
    then
        # get eisfair system-type
        . /var/install/include/check-eisfair-version
    fi

    # write eisfair system type to include file
    echo "EISMAN_SYSTEM=\"${EISFAIR_SYSTEM}\"" >> \
        /var/install/include/eismanlib

    # call eisman init to check the database
    eisman init
}

# ---------------------------------------------------------------------------
# update-2.7.9
# ---------------------------------------------------------------------------
update_2_7_9()
{
    # check for correct eisfair system
    if [ -z "${EISFAIR_SYSTEM}" ]
    then
        # get eisfair system-type
        . /var/install/include/check-eisfair-version
    fi

    # write eisfair system type to include file
    echo "EISMAN_SYSTEM=\"${EISFAIR_SYSTEM}\"" >> \
        /var/install/include/eismanlib

    # call eisman init to check the database
  #  eisman init
}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------

base_version=$(sed -e 's/[svn|cvs].*$//; s/\./_/g' /etc/version)

update_${base_version}

# remove filelist
rm -f /etc/filelist.d/${package_name}-files.txt

exit 0
# ----------------------------------------------------------------------------
# End
# ----------------------------------------------------------------------------
