#!/bin/sh
# ============================================================================
#
# This file is part of the 'MetaRepository' package
# 
# 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, or (at your option) any
# later version.
#
# This program 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.
#
# Author: Mijzelf <Mijzelf@live.com>
#
# ============================================================================
PKG_NAME="MetaRepository"
THIS_SCRIPT="ReplaceRule"

#############################################################
## Extract the PKG_ROOT for this package
##
## Function sets PKG_ROOT (without trainling slash)
#############################################################
GetPkgRoot()
{
	if [ -d /zyxel/mnt/info ]
	then
		# No firmware 5.x (then it's /firmware/mnt/info
		PKG_ROOT="/usr/local/zy-pkgs"
	else
		PKG_ROOT=`grep "${PKG_NAME}" /etc/zyxel/pkg_conf/status |grep "Installed-Rule" |awk -F":" '{print $2}' |sed 's|/$||g'|sed 's| ||g'`
	fi
}

############################################################
## Check if we are in upgradig mode. From the point of view of the package
## an upgrade is a uninstall followed by a install. So if you want to preserve
## something over an upgrade, you have to detect it somehow
##
## Function sets PKG_UPGRADE > 0 when upgrading, 
## and PKG_UPGRADE=0 when not
############################################################
IsUpgrading()
{
	PKG_UPGRADING=0
	local dir=
	for dir in "/usr/local/zy_pkgs/" "/i-data/.system/zy-pkgs/"
	do
		local logfile=${dir}tmp/zypkg.log
		[ ! -f ${logfile} ] && continue

		local lastupdate=` grep "Upgrade Mode" ${logfile} | tail -n 1 | grep "\[${PKG_NAME}\]" | awk -v OFS=' ' '{print $1, $2}' `

	        [ "${lastupdate}" = "" ] && return 1

		local since1970=` TZ=GMT date -d "${lastupdate}" +%s `
		local now=`date +%s `
		local ago=""
		let ago=now-since1970
		[ $ago -eq 0 ] && ago=1
		[ 300 -gt $ago ] && PKG_UPGRADING=$ago && return 0

		return 1
	done

	# We shouldn't be here
	return 1
}

#############################################################
## Dummy logcode
#############################################################
Log() 
{
	return
}

IsUpgrading
Log ` [ $PKG_UPGRADING -gt 0 ] && echo upgrading `

GetPkgRoot
sed -i "/^PKG_ROOT/s|/usr/local/zy-pkgs|${PKG_ROOT}|" ${PKG_ROOT}/etc/init.d/${PKG_NAME}
sed -i "s|^PKG_FIRMWARE=4|PKG_FIRMWARE=5|" ${PKG_ROOT}/etc/init.d/${PKG_NAME}

INFODIR=${PKG_ROOT}/zypkg_conf/info/

for file in ` ls ${INFODIR} `
do
	[ "${file}" = "${PKG_NAME}.${THIS_SCRIPT}" ] && continue
	sed -i "/^PKG_ROOT/s|/usr/local/zy-pkgs|${PKG_ROOT}|" ${INFODIR}${file}
	sed -i "s|^PKG_FIRMWARE=4|PKG_FIRMWARE=5|" ${INFODIR}${file}
done


for file in "/etc/${PKG_NAME}.conf" "/etc/init.d/${PKG_NAME}" "/gui/MetaRepository/pkgcgi.cgi"
do
	path=${PKG_ROOT}/${file}
	sed -i "s|/usr/local/zy-pkgs|${PKG_ROOT}|g" ${path}
	sed -i "s|/i-data/md0/admin|/i-data/sysvol/admin|g" ${path}
	sed -i "s|/Mijzelf/zypkg-repo/|/Mijzelf/zypkg-repo/NAS500/|" ${path}
done

