#!/bin/bash

# VERSION=1
# CHANGES=mount mtdblock5 and mtdblock7 space

/bin/mount -oremount,rw /
/bin/mount -oremount,rw /home/admin

## can not source VERSION.openssl file since it will update the PKG_NAME / PKG_PATH of the parent process
OPENSSL_VERSION=$(/bin/cat /usr/local/conf/rootfs/VERSION.openssl | /bin/grep OPENSSL_VERSION | /usr/bin/cut -d '=' -f 2)

## check if libcrypto and libssl already installed
if [[ -f ${PKG_PATH}/lib/libcrypto.so.${OPENSSL_VERSION} ]] && [[ -f ${PKG_PATH}/lib/libssl.so.${OPENSSL_VERSION} ]]; then
	## already installed. removing the libraries from /tmp folder	to stop the installation (not needed)
	/bin/rm -rf /tmp/conf/rootfs/pkg/openssl
## libraries must be installed. checking if enough space
elif [ "$(/bin/df | /bin/grep mtdblock7 | /usr/bin/awk '{ print $4; }')" -lt "1500" ]; then
	## not enough space on the mtdblock7 space. stop the installation
	/bin/rm -rf /tmp/conf/rootfs/pkg/openssl
	## and remove VERSION.openssl from appfs and rootfs
	## to be reinstalled on recovery mode
	/bin/rm -rf /pkginfo/VERSION.openssl
	/bin/mount -oremount,rw /usr/local
	/bin/rm -rf /usr/local/conf/rootfs/VERSION.openssl
	/bin/mount -oremount,ro /usr/local
	## ask to reset the userappfs space
	/usr/fallback/beroconf set root userapps-mustbe-reset 1
fi

exit 0
