#!/bin/sh

getifbusinfo()
{
	local businfo
	local intf=$1

	businfo=`find /sys/devices -name "${intf}" | grep net | awk -F'/' '{print $(NF - 2)}'`
	echo ${businfo}
}

getiflist()
{
        local iflist
        local intf

	iflist=""
	for intf in `cat /proc/net/dev | grep ":" | awk -F':' '{print $1}'`
	do
		if [ "${intf}" != "lo" ]; then
			if [ "${iflist}" = "" ]; then
				iflist="${intf}"
			else
				iflist="${iflist} ${intf}"
			fi
		fi
	done

	echo "${iflist}"
}

curuser=`id -nu`
if [ "${curuser}" != "root" ]; then
	echo "This program can only run as root user!"
	echo "Please su to root and run it again!"
	exit 0
fi

# Verify the OS
os=`uname -s`
if [ "${os}" != "Linux" ]; then
	echo "Please run it under Linux!"
	exit 0
fi

# The CPU should support 1G super page.
###supertlb=`cat /proc/cpuinfo | grep "^flags" | head -1 | grep pdpe1gb`
###if [ "${supertlb}" = "" ]; then
###	echo "Linux panabit can't run on CPU without 1G page supporting.!"
###	exit 0
###fi

# See if a Panabit is installed in your system.
if [ -f /etc/PG.conf ]; then
	. /etc/PG.conf
	echo "There is a Panabit installed in your system, the installation"
	echo "directory is \"${PGPATH}\""
	echo ""
	echo -n "Do you want to overrite it(y/n[n])?"
	read answer
	[ "${answer}" != "y" -a "${answer}" != "Y" ] && exit 0
fi

echo ""
echo " Welcome installing panabit!"
echo ""
 
# Now we begin to install Panabit 
# Before installing, we should get the followings:
# 1) The directory that the user want to install, the default is
#    "/usr/panabit"
# 2) The data update internal, default is 300 seconds.
# 3) The directory that the user want the data to store, the
#    default is "/usr/panalog"
# 4) The configuration is default to "/usr/panaetc"
installdir="/usr/panabit"
datadir="/usr/panalog"
etcdir="/usr/panaetc"
updatetime=300

# Now we begin to install Panabit
# First we get currently directory
# You may run the script as ./ipeinstall, or xxx/yyy/ipeinstall,
curdir=`pwd`
wholepath="${curdir}/$0"
wholedir=`dirname ${wholepath}`

echo ""
echo -n "Creating directory \"${installdir}\" ..."
mkdir -p ${installdir}
echo "ok"

echo -n "Creating directory \"${datadir}\" ..."
mkdir -p ${datadir}
echo "ok"

echo ""
echo -n "Installing Panabit to \"${installdir}\" ..."
cp -Rf ${wholedir}/* ${installdir}/
rm -f ${installdir}/ipeinstall
echo "ok"

echo ""
echo -n "Initializing configurations ..."
mkdir -p ${etcdir}
cp -Rf ${wholedir}/etc/* ${etcdir}/
echo "ok"

echo -n "Saving configuration to /etc/PG.conf ..."
echo "PGPATH=${installdir}
UPDATE_INTERNAL=360
DATAPATH=${datadir}
PGETC=${etcdir}" > /etc/PG.conf

# create soft links
[ ! -e /usr/system ] && ln -s ${installdir} /usr/system
[ ! -e /conf ] && ln -s ${etcdir} /conf
[ ! -e /log ] && ln -s ${datadir} /log

echo ""
echo "****** Congratulations ******!"
echo "You have successfully installed Panabit on your system:"
. /etc/PG.conf
echo "(1) The installed path is: ${PGPATH}"
echo "(2) The log path is: ${DATAPATH}"
echo "(3) The configuration path is: ${PGETC}"

all_intfs=""
for intf in `getiflist`
do
	[ "${intf}" = "Iface" -o "${intf}" = "lo" ] && continue
	is_ether=`cat /sys/class/net/${intf}/type 2>/dev/null` 
	[ "${is_ether}" != "" ] && all_intfs="${all_intfs} ${intf}"
done

echo ""
echo "Following interfaces are installed in your system:"
echo "${all_intfs}"

while [ "1" = "1" ]; do
	echo -n "Please choose one of above as your admin interface: "
	read admin_intf
	yes=0
	for intf in ${all_intfs}; do
		if [ "${intf}" = "${admin_intf}" ]; then
			yes=1
			break
		fi
	done

	[ ${yes} -ne 0 ] && break
done

admin_ip=""
while [ "1" = "1" ]; do
	echo -n "Please input ip address of admin interface: "
	read admin_ip
	[ "{admin_ip}" != "" ] && break
done

admin_mask=""
while [ 1 -eq 1 ]; do
	echo -n "Please input network mask of admin interface: "
	read admin_mask
	[ "${admin_mask}" != "" ] && break
done

gateway=""
echo -n "Please input default gateway: "
read gateway
if [ "${gateway}" = "" ]; then
	gateway=`netstat -rn | grep "^default" | awk '{print $2}' 2>/dev/null`
fi

data_intfs=""
left_intfs=""
for intf in ${all_intfs}; do
	if [ "${intf}" != "${admin_intf}" ]; then
		left_intfs="${left_intfs} ${intf}"
	fi
done

# Verify left_intfs
alldataifs=""
for intf in ${left_intfs}
do
	businfo=`getifbusinfo ${intf}`
	[ "${businfo}" = "" ] && continue
	alldataifs="${alldataifs} ${intf}"
done

echo ""
echo "The following interfaces are left for your data processing:"
echo "${left_intfs}"
while [ "1" = "1" ]; do
	echo -n "Please select data interfaces: "
	read data_intfs
	[ "${data_intfs}" != "" ] && break
done

data_ports=""
for intf in ${data_intfs}
do
	businfo=`getifbusinfo ${intf}`
	[ "${businfo}" = "" ] && continue
	if [ "${data_ports}" = "" ]; then
		data_ports="${intf}|${businfo}"
	else
		data_ports="${data_ports} ${intf}|${businfo}"
	fi
done

echo "ADMIN_PORT=${admin_intf}
DATA_PORTS=\"${data_ports}\"" >> /etc/PG.conf

echo "ADMIN_IP=${admin_ip}
ADMIN_MASK=${admin_mask}
GATEWAY=${gateway}" > ${PGETC}/ifadmin.conf

# Let it auto start after system boot.
echo '#!/bin/bash' > /etc/rc.local
echo "touch /var/lock/subsys/local" >> /etc/rc.local
echo "${PGPATH}/bin/ipectrl start"  >> /etc/rc.local
chmod +x /etc/rc.local

echo ""
echo ""
echo "Your interface configurations are: "
echo "    Admin interface  : ${admin_intf}"
echo "    Admin ip address : ${admin_ip}"
echo "    Admin netmask    : ${admin_mask}"
echo "    Default gateway  : ${gateway}"
echo "    Data interfaces  : ${data_intfs}"
echo ""

echo ""
echo "Thank you for using panabit!"
echo "You can issue \"${PGPATH}/bin/ipectrl start\" to start panabit!"
echo "And you can also add the above command to /etc/rc.local file"
echo "to let the system automaticly start panabit at system startup!"
echo ""
echo "If you want to stop the currently running panabit, you can issue"
echo "\"${PGPATH}/bin/ipectrl stop\" to stop it!"

