#!/bin/sh

if [ -f "/userfs/bin/tcapi" ];then
apps_ipkg_old=`/userfs/bin/tcapi get Apps_Entry apps_ipkg_old`
else
apps_ipkg_old=`nvram get apps_ipkg_old`
fi
is_arm_machine=`uname -m |grep arm`

APPS_PATH=/opt
CONF_FILE=$APPS_PATH/etc/ipkg.conf
if [ -f "/userfs/bin/tcapi" ];then
ASUS_SERVER=`/userfs/bin/tcapi get Apps_Entry apps_ipkg_server`
wget_timeout=`/userfs/bin/tcapi get Apps_Entry apps_wget_timeout`
wget_options="-q"
apps_from_internet=`/userfs/bin/tcapi get Apps_Entry rc_support |grep appnet`
apps_local_space=`/userfs/bin/tcapi get Apps_Entry apps_local_space`
APPS_MOUNTED_PATH=`/userfs/bin/tcapi get Apps_Entry apps_mounted_path`
APPS_INSTALL_FOLDER=`/userfs/bin/tcapi get Apps_Entry apps_install_folder`
else
ASUS_SERVER=`nvram get apps_ipkg_server`
wget_timeout=`nvram get apps_wget_timeout`
#wget_options="-nv -t 2 -T $wget_timeout --dns-timeout=120"
wget_options="-q -t 2 -T $wget_timeout"
apps_from_internet=`nvram get rc_support |grep appnet`
apps_local_space=`nvram get apps_local_space`
APPS_INSTALL_FOLDER=`nvram get apps_install_folder`
APPS_MOUNTED_PATH=`nvram get apps_mounted_path`
fi
APPS_INSTALL_PATH=$APPS_MOUNTED_PATH/$APPS_INSTALL_FOLDER
ASUS_LIGHTTPD_VER=3.1.0.91
CPFLAG="NO"

if [ -d "/opt/etc/asus_lighttpd" ];then
	rm -rf /opt/etc/asus_lighttpd
fi
# $1: package name.
# return value. 1: have package. 0: no package.
_check_version(){
	lighttpd_control_file=$APPS_INSTALL_PATH/lib/ipkg/info/$1.control

	if [ -f "$lighttpd_control_file" ]; then
		        #app_remove.sh asuslighttpd
                if [ "$1" == "openssl" ] || [ "$1" == "asusopenssl" ]; then
                       rm -rf $APPS_INSTALL_PATH/share/openssl/openssl.cnf
				fi 
                 ipkg remove $1 -force-depends 1 >> $APPS_INSTALL_PATH/ipkg_log_1.txt
	fi
}
_check_package(){
	package_ready=`ipkg list_installed | grep "$1 "`
	package_ready2=`app_get_field.sh $1 Enabled 1`
    pkg_install_v=`app_get_field.sh $1 Version 1`
    pkg_version=`app_get_field.sh $1 Version 2`
	if [ -z "$package_ready" ] && [ -z "$package_ready2" ]; then
			return 0
	else
			if [ "$pkg_install_v" == "$pkg_version" ]; then	    
			    return 1
			else
				_check_version $1
		        return 0
			fi
		  		return 1
	fi
}

# $1: package name, $2: ipkg server name, $3: force(1/0).
_get_pkg_file_name_old(){
	pkg_file_full=`app_get_field.sh $1 Filename 2`
	old_pkg_file=`echo "$pkg_file_full" |awk 'BEGIN{FS=".ipk"}{print $1}'`
	pkg_file=`echo "$old_pkg_file" |sed 's/\./-/g'`

	if [ "$3" == "1" ] || [ "$2" != "$ASUS_SERVER" ]; then
		echo "$pkg_file_full"
	else
		echo "$pkg_file.tgz"
	fi
}

# $1: package name.
_get_pkg_file_name(){
	pkg_file_full=`app_get_field.sh $1 Filename 2`

	echo "$pkg_file_full"
}

# $1: package name, $2: mounted path.
_install_package(){
	_check_package $1
	if [ "$?" == "0" ]; then
		pkg_server=
		pkg_file=
		installed_ipk_path=
		need_download=1
        pkg_filename=`app_get_field.sh $1 Filename 2`
		installed_ipk_path=`ls $apps_local_space/$pkg_filename`
		if [ -n "$installed_ipk_path" ]; then
			need_download=0
		fi

		if [ "$need_download" == "1" ]; then
				# Geting the app's file name...
				server_names=`grep -n '^src.*' $CONF_FILE |sort -r |awk '{print $3}'`
		 		if [ -f "/userfs/bin/tcapi" ];then
				  	IS_SUPPORT_SSL=`/userfs/bin/tcapi get SysInfo_Entry rc_support|grep -i HTTPS`
				  	if [ -n "$IS_SUPPORT_SSL" ]; then
				  		wget_options="-q -t 2 -T $wget_timeout --no-check-certificate"
				    fi
		 		else
				  	IS_SUPPORT_SSL=`nvram get rc_support|grep -i HTTPS`
				    if [ -n "$IS_SUPPORT_SSL" ]; then
				       wget_options="-q -t 2 -T $wget_timeout --no-check-certificate"
				    fi
		  fi
		  for s in $server_names; do
				if [ -z "$is_arm_machine" ] && [ -n "$apps_ipkg_old" ] && [ "$apps_ipkg_old" == "1" ]; then
					pkg_file=`_get_pkg_file_name_old $1 $s 0`
				else
					pkg_file=`_get_pkg_file_name $1`
						fi
				     if [ -f "/userfs/bin/tcapi" ];then
							   #wget $wget_options $s/$pkg_file
							   #if [ "$?" == "0" ]; then
								  pkg_server=$s
								#break
							   #fi
				    else
							  wget --spider $wget_options $s/$pkg_file
							  if [ "$?" == "0" ]; then
								 pkg_server=$s
								 break
							  fi
				    fi
		  done
			if [ -z "$pkg_server" ]; then
				return 1
			fi

			# Downloading the app's file name...
			ipk_file_name=
			if [ -z "$is_arm_machine" ] && [ -n "$apps_ipkg_old" ] && [ "$apps_ipkg_old" == "1" ] && [ "$pkg_server" == "$ASUS_SERVER" ]; then
				ipk_file_name=`_get_pkg_file_name_old $1 $pkg_server 1`
			else
				ipk_file_name=$pkg_file
			fi

			wget -c $wget_options $pkg_server/$pkg_file -O $2/$ipk_file_name
			if [ "$?" != "0" ]; then
				rm -f $2/$ipk_file_name
				sync

				return 1
			fi

			installed_ipk_path=$2"/"$ipk_file_name
		fi

		# Installing the apps...
		ipkg install $installed_ipk_path 1 >> $APPS_INSTALL_PATH/ipkg_log_1.txt
		if [ "$?" != "0" ]; then
			return 1
		fi

		if [ "$need_download" == "1" ]; then
			rm -f $installed_ipk_path
		fi
	fi

	return 0
}


ipkg_ready=`which ipkg`
if [ -n "$ipkg_ready" ]; then
	_check_version libcurl
    _check_version openssl
	_check_version asusopenssl
	_check_version asuslibcurl
       wait
	_install_package asusopenssl $APPS_INSTALL_PATH
	
	wait
        _install_package asuslibcurl $APPS_INSTALL_PATH
        wait

        rm -rf $APPS_INSTALL_PATH/ipkg_log_1.txt
	rm -rf $APPS_INSTALL_PATH/asusopenssl_*.ipk
	rm -rf $APPS_INSTALL_PATH/asuslibcurl_*.ipk
	
fi

