#!/bin/sh

REAL_OPT_DIR=/volume1/@optware

if [ -e "$REAL_OPT_DIR" ] ; then
	if [ ! "$(ls -A $REAL_OPT_DIR)" ]; then
		if [ $(mount | grep opt | wc -l) -eq "1"]; then
			umount /opt;
		fi;
		rm -fR $REAL_OPT_DIR;
	else
		exit 1
	fi;
fi

if [ ! -e `dirname $REAL_OPT_DIR` ]; then
    exit 1
fi

BS_DIR="$REAL_OPT_DIR/ipkg-bootstrap"
rm -rf $BS_DIR
mkdir -p $BS_DIR
ln -s $BS_DIR /tmp/ipkg
cat >>$BS_DIR/ipkg.conf <<EOF
dest root /
lists_dir ext $BS_DIR/ipkg
EOF

if ! which md5sum >/dev/null && which openssl >/dev/null; then \
    sed -i -e "/md5sum.*sed/{s|\`md5sum|\`openssl md5|;s|sed 's/ .*//'|sed 's/.* //'|}" $SYNOPKG_PKGDEST/ipkg.sh
fi

export IPKG_CONF_DIR=$BS_DIR 
export IPKG_DIR_PREFIX=$BS_DIR 

sh $SYNOPKG_PKGDEST/ipkg.sh install $SYNOPKG_PKGDEST/optware-bootstrap.ipk
sh $SYNOPKG_PKGDEST/ipkg.sh install $SYNOPKG_PKGDEST/ipkg-opt.ipk
rm -rf $BS_DIR
rm /tmp/ipkg
/opt/bin/ipkg install wget.ipk || exit 1

[ ! -d /opt/etc/ipkg ] && mkdir -p /opt/etc/ipkg
if [ ! -e /opt/etc/ipkg/cross-feed.conf ]; then
	echo "src/gz cross http://ipkg.nslu2-linux.org/feeds/optware/syno-i686/cross/unstable" >/opt/etc/ipkg/cross-feed.conf
fi;

sed -i "s/export PATH/#export PATH/" /root/.profile
sed -i "s/PATH=/#PATH=/" /root/.profile
