#!/bin/sh
SUPPORTED_FREEBSD=8

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}" != "FreeBSD" ]; then
	echo "Panabit now can only run under FreeBSD!"
	exit 0
fi
FreeBSD_ver=`uname -r | cut -d'.' -f1`

osver=`uname -r | cut -d'.' -f1`
if [ ${osver} -ne ${SUPPORTED_FREEBSD} ]; then
	echo "Only support FreeBSD ${SUPPORTEDVER}.x!"
	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

while [ "1" = "1" ]; do
	# Get install directory
	while [ "1" = "1" ]; do
		echo -n "Please input the directory to install panabit [${installdir}]:"
		read temp
		[ "${temp}" = "" ] && temp="${installdir}" 
	
		#make the directory absolute
		temp="/${temp}"
		installdir="${temp}"
		break
	done

	# Get data directory
	while [ "1" = "1" ]; do
		echo -n "Please input the directory to store log [${datadir}]:"
		read temp
		[ "${temp}" = "" ] && temp="${datadir}" 
	
		#make the directory absolute
		temp="/${temp}"
		datadir="${temp}"
		break
	done

	# Get etc directory
	while [ "1" = "1" ]; do
		echo -n "Please input the directory to store configuration [${etcdir}]:"
		read temp
		[ "${temp}" = "" ] && temp="${etcdir}" 
	
		#make the directory absolute
		temp="/${temp}"
		etcdir="${temp}"
		break
	done

	installdir="`echo "${installdir}" | sed -e "s/\/\//\//g"`"
	datadir="`echo "${datadir}" | sed -e "s/\/\//\//g"`"
	etcdir="`echo "${etcdir}" | sed -e "s/\/\//\//g"`"

	echo ""
	echo "The last time to verify your choice:"
	echo "(1) The directory that you want to install is \"${installdir}\""
	echo "(2) The directory that the log to be stored is \"${datadir}\""
	echo "(3) The directory that the configuration to be stored is \"${etcdir}\""
	echo -n "Above are right(y/n[y])?"
	read answer
	[ "${answer}" = "y" -o "${answer}" = "Y" -o "${answer}" = "" ] && break
done

# 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 -f ${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


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}"

echo ""
echo "To make your system more sutiable for panabit running, it's"
echo "recomended of tuning your system according your system memory"
echo "size and cpu frequence."
echo -n "Do you want to tune your system(y/n)[y]: "
read answer
[ "${answer}" = "" ] && answer="y"
if [ "${answer}" = "y" -o "${answer}" = "Y" ]; then
	new_nmbclusters=10240
	tmpfile=/var/loader.tmp
	[ -f ${tmpfile} ] && rm -f ${tmpfile}
	touch ${tmpfile}

	if [ -f /boot/loader.conf ]; then
		cat /boot/loader.conf | while read line; do
			varname=`echo "${line}" | cut -d'=' -f1`
			if [ "${varname}" = "kern.ipc.nmbclusters" ]; then
				echo "kern.ipc.nmbclusters=${new_nmbclusters}" >> ${tmpfile}
			elif [ "${varname}" = "kern.maxdsiz" ]; then
				echo "kern.maxdsiz=1600000000" >> ${tmpfile}
			elif [ "${varname}" = "vm.pmap.pg_ps_enabled" ]; then
				echo "vm.pmap.pg_ps_enabled=1" >> ${tmpfile}
			elif [ "${varname}" = "kern.hz" ]; then
				echo "kern.hz=1000" >> ${tmpfile}
			else
				echo "${line}" >> ${tmpfile}
			fi
		done
	fi

	if [ ${FreeBSD_ver} -ge 7 ]; then
		if [ "`cat ${tmpfile} | grep "vm.pmap.pg_ps_enabled"`" = "" ]; then
			echo "vm.pmap.pg_ps_enabled=1" >> ${tmpfile}
		fi
	fi
	
	if [ "`cat ${tmpfile} | grep "kern.ipc.nmbclusters"`" = "" ]; then
		echo "kern.ipc.nmbclusters=${new_nmbclusters}" >> ${tmpfile}
	fi

	# FreeBSD 8.x doesn't need this argument any more.
	if [ ${FreeBSD_ver} - lt 8 ]; then
		if [ "`cat ${tmpfile} | grep "kern.maxdsiz"`" = "" ]; then
			echo "kern.maxdsiz=2000000000" >> ${tmpfile}
		fi
	fi

	if [ "`cat ${tmpfile} | grep "kern.hz"`" = "" ]; then
		echo "kern.hz=1000" >> ${tmpfile}
	fi

	mv ${tmpfile} /boot/loader.conf

	echo "Your kernel configuration has been tuned as the followings:"
	echo ""
	cat /boot/loader.conf
	echo ""
	echo "You should restart your system to make they take effects."
	echo ""
	echo ""
fi

# try to load em kernel module.
if [ -f /boot/kernel/if_em.ko ]; then
	mv /boot/kernel/if_em.ko /boot/kernel/if_em.ko.bak
fi
cp -f ${PGPATH}/kernel/if_em.ko /boot/kernel
cp -f ${PGPATH}/kernel/ixgbe.ko /boot/kernel
sync

errmsg=`kldload ixgbe 2>/dev/null`
errmsg=`kldload if_em 2>/dev/null`
all_intfs=""
for intf in `ifconfig -l`
do
	is_ether=`ifconfig ${intf} | grep ether`
	[ "${is_ether}" != "" ] && all_intfs="${all_intfs} ${intf}"
done

echo ""
echo "Following interfaces are installed in your system:"
echo "    ${all_intfs}"
echo ""
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

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

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

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

echo "ifconfig_${ADMIN_PORT}=\"inet ${admin_ip} netmask ${admin_mask}\"" >> /etc/rc.conf

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!"

