#!/bin/bash
#
# This script is executed only once, immediately after the image
# tarball is unpacked to the app partition (/usr/local).
#

NEW_CONF=/usr/local/conf
ADM_HOME=/home/admin


function need_replace {

	old_c=$(cat ${1})
	new_c=$(cat ${2})
	old_v=$(expr match "${old_c}" ".*VERSION=\([0-9]*\)")
	new_v=$(expr match "${new_c}" ".*VERSION=\([0-9]*\)")

	if [ -z "${old_v}" ] || [ "${old_v}" -le "${new_v}" ]; then
		echo 1
		return
	fi

	echo 0
}

# update ld.so.cache
mount -oremount,rw /
/sbin/ldconfig

# fix the rights of /dev/null and /dev/zero
chmod 0666 /dev/null
chmod 0666 /dev/zero

if [ ! -d /lib/modules ]; then
	mkdir /lib/modules
fi

if [ ! -f /lib/modules/resetjumper.ko ]; then
	cp /usr/local/modules/resetjumper.ko /lib/modules
fi

if [ ! -c /dev/bfpga ] ; then
	mknod /dev/bfpga c 254 0
fi

if [ ! -c /dev/analog0 ] ; then
    for i in $(seq 0 7) ; do
	mknod /dev/analog$i c 253 $i
    done
fi

# create gsm devices
for gsm_i in `seq 0 3`; do
	gsm_dev="/dev/gsm${gsm_i}"
	if [ ! -c ${gsm_dev} ] ; then
		mknod ${gsm_dev} c 252 ${gsm_i}
	fi
done

# remove libsqlite3 (if installed)
libsqlite3_files="/usr/bin/sqlite3 /usr/lib/libsqlite3.so /usr/lib/libsqlite3.so.0.8.6 /usr/lib/libsqlite3.so.0 /usr/lib/pkgconfig/sqlite3.pc /usr/include/sqlite3.h /usr/include/sqlite3ext.h"
for file in ${libsqlite3_files}; do
	if [ -f ${file} ]; then
		rm -f ${file}
	fi
done

if [ ! -f /usr/lib/mod_auth.so ] ; then
    cp ${NEW_CONF}/mod_auth.so /usr/lib/
    cp ${NEW_CONF}/lighttpd.conf /etc/
fi

# check if files in /etc need to be updated
if [ "$(need_replace "/etc/lighttpd.conf" "${NEW_CONF}/lighttpd.conf")" -eq 1 ] ; then
    cp ${NEW_CONF}/lighttpd.conf /etc/
fi

if [ "$(need_replace "/etc/php.ini" "${NEW_CONF}/php.ini")" -eq 1 ] ; then
    cp ${NEW_CONF}/php.ini /etc/
fi

if [ "$(need_replace "/usr/sbin/update-time" "${NEW_CONF}/update-time")" -eq 1 ] ; then
    cp ${NEW_CONF}/update-time /usr/sbin/
fi

if [ "$(need_replace "/usr/www/cgi-bin/update-tool.php" "${NEW_CONF}/update-tool.php")" -eq 1 ] ; then
    cp ${NEW_CONF}/update-tool.php /usr/www/cgi-bin/
fi

if [ "$(need_replace "/usr/share/udhcpc/default.script" "${NEW_CONF}/udhcp-conf.script")" -eq 1 ] ; then
    cp ${NEW_CONF}/udhcp-conf.script /usr/share/udhcpc/default.script
fi

if [ "$(need_replace "/usr/conf/snmpd_berofix.conf" "${NEW_CONF}/snmpd_berofix.conf")" -eq 1 ] ; then
    cp ${NEW_CONF}/snmpd_berofix.conf /usr/conf/snmpd_berofix.conf
fi

if [ "$(need_replace "/bin/vim" "${NEW_CONF}/vim")" -eq 1 ] ; then
    cp ${NEW_CONF}/vim /bin/vim
fi

# check if rootfs init-scripts need to be updated
for script_new in $(ls ${NEW_CONF}/init_root/S*); do
	script_ins="/etc/init.d/${script_new/\/usr\/local\/conf\/init_root\/}"

	if [ "$(need_replace "${script_ins}" "${script_new}")" -eq 1 ]; then
		cp ${script_new} ${script_ins}
	fi
done

# check if fallback scripts need to be updated
fb_script_src="/usr/local/php/beroConf.php /usr/local/bin/beroconf /usr/local/php/beroconf.bin /usr/local/sbin/install.sh /usr/local/sbin/update-interfaces /usr/local/php/update-interfaces.bin /usr/local/sbin/update-network"

for script_src in ${fb_script_src}; do
	script_tgt="/usr/fallback/${script_src/#\/usr\/local\/*\//}"

	if [ "$(need_replace "${script_tgt}" "${script_src}")" -eq 1 ]; then
		cp ${script_src} ${script_tgt}
	fi
done


# disable ttyS0, if this is a 6400 card
bftype=$(sed "s/.*carduuid=//" /proc/cmdline | sed "s/ revision.*//"  | cut -d "-" -f2)

if [ "$bftype" = "64" ] ; then
        if [ -c /dev/ttyS0 ] ; then
                mv /dev/ttyS0 /dev/ttyS0_off
        fi
fi

# set up new user-appfs-user
if [ ! -d /home/admin ]; then
	echo "admin:x:1001:1001:admin:${ADM_HOME}:/bin/bash" >> /etc/passwd
	echo "admin:$1$$CoERg7ynjYLsj2j4glJ34.:10933:0:99999:7:::" >> /etc/shadow
	addgroup -g 1001 admin

	mkdir ${ADM_HOME}
	chown admin:admin ${ADM_HOME}

	cp -R /mnt/images/* /tmp
	umount /mnt/images
	rm -rf /mnt/images
	ln -s /tmp /mnt/images

	cp /etc/fstab /etc/fstab_orig
	sed -e 's/\/mnt\/images    jffs2    rw,defaults/\/home\/admin    jffs2    ro,defaults,user=admin/' /etc/fstab_orig > /etc/fstab
	rm -f /etc/fstab_orig

	mount ${ADM_HOME} -orw
	rm -rf $(ls -A ${ADM_HOME})

	sleep 1; sync; sync; sleep 1

	mount ${ADM_HOME} -oremount,ro
else
	if [ -L ${ADM_HOME}/tmp ]; then
		mount -oremount,rw ${ADM_HOME}
		rm -f ${ADM_HOME}/tmp
		mount -oremount,ro ${ADM_HOME}
	fi
fi

# update userapp-root
if [ "$(need_replace "${ADM_HOME}/VERSION" "${NEW_CONF}/userapp_root/VERSION")" -eq 1 ]; then

	# save list of installed apps
	www_links=$(find ${ADM_HOME}/www -type l)
	www_links=${www_links//\/home\/admin\/www\//}

	mount -oremount,rw ${ADM_HOME}

	# cleanup
	for i in $(find ${ADM_HOME} | grep -v "${ADM_HOME}/apps"); do
		if [ "${i}" != "${ADM_HOME}" ]; then
			rm -rf ${i}
		fi
	done

	# copy new skel to /home/admin
	cp -r ${NEW_CONF}/userapp_root/* ${ADM_HOME}

	# create apps-directory (if it doesn't exist already)
	if [ ! -d ${ADM_HOME}/apps ]; then
		rm -f ${ADM_HOME}/apps
		mkdir -p ${ADM_HOME}/apps
	fi

	# restore symlinks to apps www-root
	for link in ${www_links}; do
		ln -s /apps/${link}/www /home/admin/www/${link}
	done

	# set permissions
	chown -R admin:admin ${ADM_HOME}/*

	sleep 1; sync; sync; sleep 1

	mount -oremount,ro ${ADM_HOME}
fi

if [ "$(need_replace "/usr/www/index.php" "${NEW_CONF}/root_index.php")" -eq 1 ] ; then
    cp ${NEW_CONF}/root_index.php /usr/www/index.php
fi

# disable UseDNS in sshd.conf
grep "#UseDNS" /etc/sshd_config > /dev/null
if [ ${?} -eq 0 ]; then
	sed 's/#UseDNS/UseDNS/g;' /etc/sshd_config > /etc/sshd_config.tmp
	sed 's/UseDNS yes/UseDNS no/g;' /etc/sshd_config.tmp > /etc/sshd_config
	rm -f /etc/sshd_config.tmp
fi

mount -oremount,ro /


if [ ! -d /usr/local/www/api ] ; then
    mkdir -p /usr/local/www/api
fi

if [ ! -f /usr/conf/apiusers.conf ] ; then
    echo "admin:admin" > /usr/conf/apiusers.conf
fi

if [ "$(need_replace "${NEW_CONF}/isgw.tones" "/usr/conf/isgw.tones")" -eq 1 ] ; then
    cp ${NEW_CONF}/isgw.tones /usr/conf
fi

if [ ! -f /usr/conf/isgw.causes ] ; then
	cp ${NEW_CONF}/isgw.causes /usr/conf
fi


if [ -f /usr/conf/isdn.conf ] ; then
#migrating from 1.0 to 2.0 Version
    if grep -A2 "type=$" /usr/conf/isdn.conf | grep "clock=1" > /dev/null ; then
        sed "s/type=$/type=bf1E1/" /usr/conf/isdn.conf > /usr/conf/hardware.conf
        rm /usr/conf/isdn.conf
    else
        mv /usr/conf/isdn.conf /usr/conf/hardware.conf
    fi
    rm /usr/conf/ari.db
    /usr/bin/env -i bash -c "/usr/bin/php-cgi /usr/local/www/berogui/includes/migration.php > /dev/null"
fi

MTU=$(/usr/local/bin/beroconf get root lan-mtu)
if [ "$MTU" = "500" ] ; then
        /usr/local/bin/beroconf set root lan-mtu 1500
fi


/usr/bin/env -i bash -c "/usr/bin/php-cgi /usr/local/www/berogui/misc/post_update.php  > /dev/null"
sync
sleep 1
/usr/bin/env -i bash -c "/usr/bin/php-cgi /usr/local/www/berogui/includes/create_files.php  > /dev/null"

