#!/bin/sh
# ============================================================================
#
# This file is part of the 'RandomTools' 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="RandomTools"
THIS_SCRIPT="postinst"
PKG_VERSION="20170204zypkg011"
PKG_FIRMWARE=4

############################################################
## 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 `

PKG_ROOT="/usr/local/zy-pkgs"


AdaptSymlinks()
{
    local certsdir=${PKG_ROOT}/etc/ssl/
    
    # For some reason the symlinks get damaged when I package them directly in the zypkg
    # Don't know why. Here I use the full tar to extract an embedded .tar file
    /tmp/.MetaRepository/bin/tar xf ${certsdir}certs.tar -C ${certsdir}
    [ $? -eq 0 ] && rm ${certsdir}certs.tar
    certsdir=${certsdir}certs

    for link in $( ls ${certsdir}/ )
    do
	local target="$( readlink ${certsdir}/${link} | sed "s|^PKG_ROOT|${PKG_ROOT}|" )"
	[ "${target}" = "" ] && continue
	echo "${link} => ${target}" >>/tmp/certs
	rm ${certsdir}/${link}
	ln -s "${target}" ${certsdir}/${link}
	target=""
    done
}

AdaptSymlinks

infodir=/zyxel/mnt/info
[ -d /firmware/mnt/info ] && infodir=/firmware/mnt/info
model=unknown
[ -f /etc/modelname ] && model=` head -n 1 /etc/modelname `
modelid=` cat ${infodir}/modelid `
fwversion=` cat ${infodir}/fwversion `

[ ${PKG_UPGRADING} -gt 0 ] && upgrading="-upgrading"

# Say hello to the author of this package. The requested file doesn't exist, 
# so wget will get a 404, but it will give a logline in my webserver logs.
wget -q -t 2 -T 2 "http://mijzelf.duckdns.org/hello_author.php?${PKG_NAME}${upgrading}-${model}-${modelid}-${fwversion}" -O - 2>/dev/null
exit 0