#!/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="postinst"
PKG_VERSION="20181001zypkg015"
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"

PreserveFiles()
{
	local preserve_root=${PKG_ROOT}
	[ "` basename ${preserve_root} `" = "${PKG_NAME}" ] && preserve_root=` dirname ${preserve_root} `
	local preserve_tar=${preserve_root}/preserve.${PKG_NAME}.tar
	if [ -f ${preserve_tar} ] ; then
		PRESERVED_FOUND=1
		cd ${PKG_ROOT}
		tar xf ${preserve_tar}
		rm ${preserve_tar}
	fi
}

PreserveFiles

CopyDesktopIcons()
{
	local pkgdir=/i-data/sysvol/.system/zy-pkgs
	if [ -d ${pkgdir} ] ; then
		pkgdir=${pkgdir}/pkggui
		for file in /MetaRepository/images/i_meta.png /MetaRepository/images/i_meta_active.png  /MetaRepository/MetaRepository.css
		do
			mkdir -p $( dirname ${pkgdir}${file} )
			cp ${PKG_ROOT}/gui${file} ${pkgdir}${file}
		done
	fi
}

CopyDesktopIcons

PKG_MEDION=0
 
if [ -f /zyxel/mnt/info/modelid ] ; then
	case ` cat /zyxel/mnt/info/modelid `
	in
		A403*)
			PKG_MEDION=1
			;;
		A803*)
			PKG_MEDION=1
			;;
		AB03*)
			PKG_MEDION=1
			;;
	esac
fi
	
[ $PKG_MEDION -eq 1 ] && sed -i "s|^PKG_MEDION=0|PKG_MEDION=1|" ${PKG_ROOT}/etc/init.d/${PKG_NAME}

[ ${PKG_MEDION} -eq 1 ] && sed -i "s|^ftp://ftp2.zyxel.com/+|ftp://ftp2.zyxel.com/NSA310/4.40/zypkg/|" ${PKG_ROOT}/etc/${PKG_NAME}.conf 

WEB_PREFIX=/i-data/md0/admin/zy-pkgs/web_prefix

# Check if a useable web_prefix will tell us where we are installed from
if [ -f ${WEB_PREFIX} ] ; then
	if grep localhost ${WEB_PREFIX} >/dev/null
	then
		echo nop >/dev/null
		# nothing we can do
	else
		url=$( cat ${WEB_PREFIX} )
		sed -i "s|http://downloads.zyxel.nas-central.org/Users/Mijzelf/zypkg-repo/|${url}|" ${PKG_ROOT}/etc/${PKG_NAME}.conf
	fi
fi

# Check if multithreading is supported:
echo no >/tmp/ismultithreadingsupported

Filter()
{
	while read line
	do
		echo $line >/tmp/ismultithreadingsupported
	done
}

Check()
{
	sleep 3
	echo yes
}

Check | Filter &
wait

ismultithreadingsupported=` cat /tmp/ismultithreadingsupported `
rm /tmp/ismultithreadingsupported

[ "no" = "$ismultithreadingsupported" ] && sed -i "s|^ENABLE_MULTITHREADING=1|ENABLE_MULTITHREADING=0|" ${PKG_ROOT}/gui/${PKG_NAME}/ZYPKG_INFO.sh

. ${PKG_ROOT}/etc/${PKG_NAME}.conf

rm ${SCRATCH}/*.md5


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