#!/bin/sh
# ISPsystem install v.4

Usage()
{
	cat << EOU >&2

Usage:
	$0 --help 	Print this help

	$0 [options] [mgrname]
	--os OS		Force use OS distribution
	--arch ARCH	Force use ARCH architecture
	--ip IP		Use IP for licence check
	
EOU
}

DetectFetch()
{
	if test -x /usr/bin/fetch; then
		fetch="/usr/bin/fetch -o "
	elif test -x /usr/bin/wget; then
		fetch="/usr/bin/wget -O "
	elif test -x /usr/bin/curl; then
		fetch="/usr/bin/curl -o "
	else
		echo "ERROR: no fetch program found."
		exit 1
	fi
}

DetectMd5()
{
	if test -x /sbin/md5; then
		md5="/sbin/md5 "
	elif test -x /usr/bin/md5sum; then
		md5="/usr/bin/md5sum "
	else
		echo "ERROR: no programm for checksum found."
		exit 1
	fi
}

DetectOS()
{
	kern=`uname -s`
	case "$kern" in
		FreeBSD)
			ver=`uname -r|sed -E 's/^([0-9]+\.[0-9]+).*$/\1/'`
			os="$kern-$ver"
			;;
		Linux)
			os="Linux-cc6"
#			if test -e "/usr/lib/libstdc++.so.7" -o -e "/usr/lib/libstdc++-v3/libstdc++.so.7"; then
#				os="Linux-cc7"
#			elif test -e "/usr/lib/libstdc++.so.6" -o -e "/usr/lib/libstdc++-v3/libstdc++.so.6"; then
#				os="Linux-cc6"
#			else
#				os="Linux-cc5"
#			fi	
			;;
		*)
			echo "Unknown OS type"
			exit 1
			;;
	esac
}

DetectArch()
{
	arch=`uname -m`
}

echo
echo "ISPsystem install v.4"
echo

while true
do
	case "$1" in 
		-h | --help)
			Usage
			exit 0
			;;
		--os)
			os=${2:-.}
			shift 2
			;;
		--arch)
			arch=${2:-.}
			shift 2
			;;
		--ip)
			ip=${2:-.}
			ipparam="ip=$ip"
			shift 2
			;;
		-*)
			echo Unrecognized flag : "$1" >&2
			Usage
			exit 1
			;;
		*)
			break ;;
	esac
done

DetectFetch
DetectMd5

if test "$os" = ""; then
	DetectOS
fi

if test "$arch" = ""; then
	DetectArch
fi

if test $# -eq 0 ; then
	# choose manager
	list=`$fetch - -q "http://lic.ispsystem.com/liclist.cgi?$ipparam"`
	
	if test "$list" = "no_license_found"; then
		echo "You have no active licenses"
		exit 1
	fi

	while true 
	do
		j="1"
		for i in $list; do
			echo "$j) $i"
			eval "mgrval$j=$i"
			j=$(($j+1))
		done
	
		echo "0) Exit"
		echo
		read -p "Please choose software to install: " n
		echo
		
		if test "$n" = "0"; then
			exit 0
		fi
	
		eval mgrname=\$mgrval$n
		if test "$mgrname" != ""; then
			break;
		fi
	done
else
	mgrname=$1
fi

if test -x /usr/bin/ntpdate; then
	/usr/bin/ntpdate -b pool.ntp.org
fi

tmpdir="/tmp/$mgrname"
mkdir -p $tmpdir
cd $tmpdir

goon="true"
while [ $goon = "true" ]
do
	goon="false"
	echo "1) ru.download.ispsystem.com"
	echo "2) us.download.ispsystem.com"
	echo "3) be.download.ispsystem.com"
	echo
	read -p "Please choose mirror to install from: " n
	echo

	if [ "$n" = "1" ]; then mirror="ru.download.ispsystem.com"
	elif [ "$n" = "2" ]; then mirror="us.download.ispsystem.com"
	elif [ "$n" = "3" ]; then mirror="be.download.ispsystem.com"
	else goon="true"; fi
done

url="http://$mirror/$os/$arch/$mgrname/install.tgz"
archive="$tmpdir/install.tgz"

test -f $archive && rm -f $archive
$fetch $archive "$url"

if ! test -s $archive; then
	echo "Can't download $mgrname distribution"
	echo "Make sure it is available for your platform ($os $arch)"
	echo "List of supported distribution you can see at"
	echo "http://download.ispsystem.com/"
	echo "see $0 --help for more information"
	rm -rf $tmpdir
	exit 1
fi

# check md5
$fetch $archive.md5 "$url.md5"
remotesum=`cat $archive.md5`
localsum=`$md5 install.tgz`

if test "$localsum" != "$remotesum"; then
	echo "Invalid MD5 signature"
	echo "Please try again."
	rm -rf $tmpdir
	exit 1
fi

mgrdir="/usr/local/ispmgr/"
mkdir -p $mgrdir
cd $mgrdir
bin=`tar xvzpf $archive 2>&1 | grep ' bin/.' | sed -e 's/^.*\///'`
rm -rf $tmpdir
echo "Mirror http://$mirror/" >> $mgrdir/etc/dist/$bin.conf

installname=`echo ${mgrname} | sed -e 's/-.*$//'`

sh sbin/${installname}-install.sh $ip $mirror



