#!/bin/sh

backtitle="Hintsoft Co., Ltd., Shanghai                             http://www.hintsoft.com.cn"
title="hintsys"

PATH=$PATH:/usr/bin

DIALOG=whiptail

out_file=/tmp/out_file
trap "return 1 2>/dev/null" INT

error()
{
	if [ -n "$1" ]; then
		$DIALOG --backtitle "$backtitle" --title "ERROR" --msgbox "$1" $2 $3
	fi
}

disk_list()
{

        dev_list=/tmp/dev_list
	fdisk -l 2>/dev/null |grep ^Disk |cut -c6- | cut -d, -f1 | tr :  ' ' | tr -s " " | sort > $dev_list
	cat $dev_list | while read line
	do
               if [ ${#line} -eq 0 ]; then
                        break
                fi

                name=$(echo $line | awk '{ print $1}')
                size=$(echo $line | awk '{ print $2$3}')
                if grep -q $name /proc/mounts 2>/dev/null; then
                        continue
                fi

                if grep -q ${name#/dev/*} /proc/mdstat 2>/dev/null; then
                        continue
                fi

#                if grep -q $name /etc/mdadm.conf 2>/dev/null; then
#                        continue
#                fi

                echo "$name $size off"
        done
}

disk_list_all()
{

        dev_list=/tmp/dev_list
	rm -fr $out_file
	fdisk -l 2>/dev/null |grep ^Disk |cut -c6- | cut -d, -f1 | tr :  ' ' | tr -s " " | sort > $dev_list
	cat $dev_list | while read line
	do
               if [ ${#line} -eq 0 ]; then
                        break
                fi

                name=$(echo $line | awk '{ print $1}')
                size=$(echo $line | awk '{ print $2$3}')
                if grep -q $name /proc/mounts 2>/dev/null; then
			
                        continue
                fi
		if [ -n "$1" ]	
		then
			if [ "$1" == "$name" ]
			then
				continue
			fi 
		fi
		if ! echo $name |grep -q "md" 2>/dev/null
		then
                	if grep -q ${name#/dev/*} /proc/mdstat 2>/dev/null; then
                        	continue
			else
				echo "$name $size off" >>$out_file
				for part in  $(fdisk $name -l 2>/dev/null|grep $name | grep -v "Disk"|awk '{ print$1 }')
				do
					disk_size=$(fdisk -l $part 2>/dev/null |grep ^Disk.*bytes | cut -d, -f2|awk '{ print$1 }')
					if [ -n "$disk_size" ]
					then
						size=$[$disk_size/1024/1024/1024]		
						echo "$part ${size}GB off" >>$out_file
					fi
				done	
			fi
	
                else
			echo "$name $size off" >>$out_file
		fi
#                if grep -q $name /etc/mdadm.conf 2>/dev/null; then
#                        continue
#                fi

        done
}

config_network()
{
tmpfile="/tmp/setup.$$"

[ -d /etc/sysconfig/network-scripts ] || mkdir -p /etc/sysconfig/network-scripts 
for iface in $(ifconfig -a |grep ^eth | cut -d" " -f1)
do
  no=$(echo $iface | cut -c4-)
  [ -z "$no" ] && no=0

  iface_file="/etc/sysconfig/network-scripts/ifcfg-$iface"
  if [ -f "$iface_file" ]
  then
      address_val=$(grep IPADDR $iface_file| cut -d= -f2)
      netmask_val=$(grep NETMASK $iface_file| cut -d= -f2)
  fi

  [ -z "$address_val" ] && address_val="192.168.$no.251"
  [ -z "$netmask_val" ] && netmask_val="255.255.255.0"
  hwaddress=$(ifconfig $iface | grep HWaddr | awk '{ print $5 }')

  while [ -z "$address" ]
  do
     $DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "Please input IP address for $iface:" 12 40 "$address_val" 2> $tmpfile
     if [ $? -eq 1 ]; then
     select_operation
     fi
     address=$(cat $tmpfile)
 done

  while [ -z "$netmask" ]
  do
     $DIALOG --backtitle "$backtitle" --title "$title"  --inputbox "Please input netmask for $iface:" 12 40 "$netmask_val" 2> $tmpfile
     if [ $? -eq 1 ]; then
     select_operation
     fi
     netmask=$(cat $tmpfile)
  done
  ifconfig $iface $address netmask $netmask

  echo "DEVICE=$iface" > /etc/sysconfig/network-scripts/ifcfg-$iface
  echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-$iface
  echo "IPADDR=$address" >> /etc/sysconfig/network-scripts/ifcfg-$iface
  echo "HWADDR=$hwaddress" >> /etc/sysconfig/network-scripts/ifcfg-$iface
  echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/ifcfg-$iface
  lastip=$address
  unset address
  unset netmask
  unset hwaddress
  unset address_val
  unset netmask_val
done

  if [ -f "/etc/sysconfig/network" ]
  then
      gateway_val=$(grep GATEWAY /etc/sysconfig/network | cut -d= -f2)
  fi

  while [ -z "$gateway" ]
  do
     $DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "Please input default gateway:" 12 40 "$gateway_val" 2> $tmpfile
    if [ $? -eq 1 ]; then
     select_operation
     fi
     gateway=$(cat $tmpfile)
  done

if grep -q GATEWAY /etc/sysconfig/network 2>/dev/null
then
   sed "s/GATEWAY=.*/GATEWAY=${gateway}/" /etc/sysconfig/network > $tmpfile
   /bin/mv -f $tmpfile /etc/sysconfig/network
else
   echo "GATEWAY=$gateway" >> /etc/sysconfig/network
fi
  route del default 2>/dev/null
  route add default gw $gateway 2>/dev/null

  if [ -f "/etc/sysconfig/network" ]
  then
     hostname_val=$(grep HOSTNAME /etc/sysconfig/network | cut -d= -f2)
  fi

  while [ -z "$hostname" ]
  do
     $DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "Please input server name " 12 40 "$hostname_val" 2> $tmpfile
    if [ $? -eq 1 ]; then
     select_operation
     fi
     hostname=$(cat $tmpfile)
  done

if grep -q HOSTNAME /etc/sysconfig/network 2>/dev/null
then
   sed "s/HOSTNAME=.*/HOSTNAME=${hostname}/" /etc/sysconfig/network > $tmpfile
   /bin/mv -f $tmpfile /etc/sysconfig/network
else
   echo "HOSTNAME=$hostname" >> /etc/sysconfig/network
fi
 hostname $hostname

  if [ -f "/etc/resolv.conf" ]
  then
      nameserver_val=$(grep nameserver /etc/resolv.conf |tr -s " " | cut -d' ' -f2)
  fi
  [ -z "$nameserver_val" ] && nameserver_val="202.96.209.5"

  while [ -z "$nameserver" ]
  do
     $DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "Please input nameserver(DNS):" 12 40 "$nameserver_val" 2> $tmpfile
     if [ $? -eq 1 ]; then
     select_operation
     fi
     nameserver=$(cat $tmpfile)
  done
  echo "nameserver $nameserver" > /etc/resolv.conf
}

select_kvdisk()
{

	disklist=`fdisk -l |grep ^Disk |cut -c6- | cut -d, -f1 | tr :  ' ' | sort`
	if [ -z "$disklist" ]; then
		error "\nSetup could't found any valid harddisk" 12 50
		return 1
	fi

	$DIALOG --backtitle "$backtitle" --title "$title" --menu "Please select disk" 12 40 4 $disklist 2> /tmp/kvdisk
	kvdisk_dev=`cat /tmp/kvdisk`
	if [ -n "$kvdisk_dev" ]; then
	 if grep -q "disk=/dev" /etc/kvdisk.conf 2>/dev/null
	 then
		sed "s#disk=.*#disk=$kvdisk_dev#" < /etc/kvdisk.conf > /etc/kvdisk.conf.new
		/bin/mv /etc/kvdisk.conf.new /etc/kvdisk.conf
	 else
		echo "disk=$kvdisk_dev" >> /etc/kvdisk.conf
	 fi
	else
		return 2
	fi
}

monitor()
{

	$DIALOG --backtitle "$backtitle" --title "monitor" --menu "Please select a task(Ctrl+c to return):" 18 50 7 "top" "monitor system processes" "ps" "check processes one by one" "ifstat" "monitor network status" "iostat" "monitor disk status" "mdstat" "software raid status" 2> /tmp/task
	if [ $? != 0 ]; then
		return 2
	fi

	task=`cat /tmp/task`
	if [ -z "$task" ]; then
		return 3
	fi
	/bin/rm -f /tmp/task
	case "$task" in
	"ifstat")
		if grep -q eth[0-9] /proc/net/dev 2>/dev/null; then
			ifstat
		else
			$DIALOG --backtitle "$backtitle" --title "Error" --msgbox "No network device found..."  12 50 
		fi
		;;
	"top")
		top
		;;
	"iostat")
		iostat 1
		;;
	"ps")
		ps -ef | less
		;;
	"mdstat")
		watch cat /proc/mdstat
		;;
	*)
		;;
	esac
}

raid_remove()
{
	if [ -f /proc/vDisk ]
	then	
		$DIALOG --backtitle "$backtitle" --title "$title" --yesno "Kvdisk is running,are you sure stop it?" 10 40 10  
		if [ $? -eq 0 ]; then
			/etc/init.d/kvdisk stop
		fi
	fi
	raidlist=$(grep md[0-9] /proc/mdstat | sed 's/.*\(md[0-9]\).*\(raid[0-9]\).*/\1 \2 off/')
 	if [ -n "$raidlist" ]; then 
		$DIALOG --backtitle "$backtitle" --title "$title" --checklist "Please select raid device to remove" 12 40 4 $raidlist 2> /tmp/toremove
		for md in `cat /tmp/toremove | sed 's/"//g'`
		do
			mount| grep -q /dev/$md && umount /dev/$md
			mdadm --stop /dev/$md
			mdadm --remove /dev/$md
		done
	fi
}

raid_config()
{
	RAID_NUM=$(grep md[0-9] /proc/mdstat | sed 's/.*md\([0-9]\).*/\1/')

 	if [ -n "$RAID_NUM" ];then 
		$DIALOG --backtitle "$backtitle" --title "$title" --menu "Choose task:" 20 70 10  "Add" "add software raid0 device" "Remove" "remove raid0 device" "Return" "quit from this menu" 2> /tmp/raid_operate
		operate=`cat /tmp/raid_operate`
		rm -f /tmp/raid_operate

		[ "$operate" == "Return" ]  && return 0
		[ "$operate" == "Remove" ]  && raid_remove && return 0
	fi
	
	disklist=`disk_list`
	if [ -z "$disklist" ]; then
		error "\nSetup could't found any valid harddisk" 12 50
		return 1
	fi

	$DIALOG --backtitle "$backtitle" --title "$title" --checklist "Please select raid device mumbers" 12 40 4 $disklist 2> /tmp/raidmumber
	if [ $? -eq 1 ]; then
		select_operation
	fi

	raid_devices=$(cat /tmp/raidmumber)
	raid_max_size_s=0
	raid_free_size_s=$raid_max_size_s
	disk_count=0
	min_size=0
	for disk in $(echo $raid_devices | sed 's/"//g')
	do
			if [ -n "$disk" ]
			then
				disk_count=$(($disk_count+1))
				echo initialize disk:$disk
			#		echo -e "o\nw\n" | /sbin/fdisk $disk &>/dev/null			
				echo Yes |/sbin/parted  $disk mktable
				disk_size=$(fdisk -l $disk 2>/dev/null|grep ^Disk.*bytes | cut -d, -f2|awk '{ print$1 }')
				disk_size_s=$[$disk_size/512]
#				raid_max_size_s=$[$raid_max_size_s+$disk_size_s]
				if [ $min_size -eq 0  ]
				then
					min_size=$disk_size_s
				else
					[ $min_size -gt $disk_size_s ] &&  min_size=$disk_size_s
				fi
				
				
			fi
	done
	if [ $disk_count -eq 0 ] 
	then
              error "\nSetup could't found any selected harddisk" 12 50
              return 1
	fi
	rm -fr /dev/fd0
	rm -fr /dev/fd1
	/sbin/partprobe
	raid_max_size_s=$[$min_size*$disk_count]
	raid_free_size_s=$raid_max_size_s
#	echo raid_max_size_s=$raid_max_size_s disk_count=$disk_count
	$DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "input create soft raid count(max=4,min=1),raid_max_size:$[$raid_max_size_s*512/1000/1000/1000]G" 12 80 "1" 2> /tmp/raid_operate
	if [ $? -eq 1 ]; then
		select_operation
	fi
	raidcount=$(cat /tmp/raid_operate)

	if [ $disk_count -gt 1 ]
	then
		
		for (( raidnum = 1; raidnum <= $raidcount; raidnum++ ))
		do
			RAID_NUM=$(grep md[0-9] /proc/mdstat | sed 's/.*md\([0-9]\).*/\1/')
			NUMBASE="0 1 2 3 4 5 6 7 9"
			foundnum=-2
			if [[ -z $RAID_NUM ]]
			then
			foundnum=0
			else
				for i in $NUMBASE
				do
					foundnum=-2
					for j in $RAID_NUM
					do
						if [[ $j == $i ]]
						then
							foundnum=-1
							break
						fi
					done
					if [[ $foundnum == -2 ]]
					then
						foundnum=$i
						break
					fi
				done
			fi

			$DIALOG  --backtitle "$backtitle" --title "$title" --inputbox "input /dev/md$foundnum size(M):" 12 40 "$[$raid_free_size_s*512/1000/1000]" 2> /tmp/raid_operate
			if [ $? -eq 1 ]; then
				select_operation
			fi
			raid_size=$(cat /tmp/raid_operate)

			devices=""
			raid_part_size=$[$raid_size*1000*1000/512/$disk_count]
			lit=$[$raid_part_size%$disk_count]
			raid_part_size=$[$raid_part_size-$lit]
			for disk in $(echo $raid_devices | sed 's/"//g')
			do
				raid_free_size_s_T=$[$raid_free_size_s-$raid_part_size*$disk_count]
				echo create soft raid partition ${disk}${raidnum} 
				#raid_size=$raid_size raid_part_size=$raid_part_size  raid_free_size_s_T=$raid_free_size_s_T raid_free_size_s=$raid_free_size_s 
				if [[ $raidnum == 1 ]]
				then
					#echo Yes|/sbin/parted $disk mkpart primary 1s $[$raid_part_size]s 
					if [ $raid_free_size_s_T -lt $[2000*$disk_count] ]
					then
						echo -e "n\np\n${raidnum}\n\n$[$raid_max_size_s/$disk_count-1]\nt\nfd\nw\n" |fdisk $disk -u 2>/dev/null >/dev/null 
					else
						echo -e "n\np\n${raidnum}\n\n$[$raid_part_size-1]\nt\nfd\nw\n" |fdisk $disk -u 2>/dev/null >/dev/null 
					fi
				else
					#echo Yes|/sbin/parted $disk mkpart primary $[$[$raid_max_size_s-$raid_free_size_s]/$disk_count+1]s $[$raid_part_size+$[$[$raid_max_size_s-$raid_free_size_s]/$disk_count]]s 
					if [ $raid_free_size_s_T -lt $[2000*$disk_count] ]
					then	
						echo -e "n\np\n${raidnum}\n$[$[$raid_max_size_s-$raid_free_size_s]/$disk_count]\n$[$raid_max_size_s/$disk_count-1]\nt\n${raidnum}\nfd\nw\n" |fdisk $disk -u 2>/dev/null >/dev/null
					else
						echo -e "n\np\n${raidnum}\n$[$[$raid_max_size_s-$raid_free_size_s]/$disk_count]\n$[$raid_part_size+$[$[$raid_max_size_s-$raid_free_size_s]/$disk_count]-1]\nt\n${raidnum}\nfd\nw\n" |fdisk $disk -u 2>/dev/null >/dev/null
					fi
				fi
				#read	
				if [ -z "$devices" ]; then
					devices="${disk}$[raidnum]"
				else
					devices="$devices ${disk}$[raidnum]"
				fi
			done
			/sbin/partprobe
			raid_free_size_s=$[$raid_free_size_s-$raid_part_size*$disk_count]
			echo  creating soft raid device  /dev/md$foundnum
			sleep 1
			echo yes|mdadm -Cv /dev/md$foundnum -l0 -n$disk_count $devices -c 4096
		#	echo yes | mdadm --create /dev/md$foundnum --force --auto=yes --level=0 --raid-devices=$disk_count $devices -c 4096
		done
	fi

}

select_eth()
{
	ethlist=`ifconfig  -a | grep ^eth | tr -s " " | cut -d' ' -f 1,5 | sort`
	[ -z $ethlist ] && return 3
	$DIALOG --backtitle "$backtitle" --title "$title" --menu "Please select ethernet device" 12 40 4 $ethlist 2> /tmp/ether_dev
	eth_dev=`cat /tmp/ether_dev`
	if [ -n "$eth_dev" ]; then
	 if grep -q iface=eth /etc/kvdisk.conf 2>/dev/null
	 then
		sed "s#iface=.*#iface=$eth_dev#" < /etc/kvdisk.conf > /etc/kvdisk.conf.new
		/bin/mv /etc/kvdisk.conf.new /etc/kvdisk.conf
	 else
		echo "iface=$eth_dev" >> /etc/kvdisk.conf
	 fi
	fi
}

config_now()
{
	if [ -f /etc/kvdisk.conf ]; then
		count_default=$(grep count=[0-9] /etc/kvdisk.conf | cut -d= -f2)
	fi
	[ -z "$count_default" ] && count_default=128
	$DIALOG --backtitle "$backtitle" --title "$title" --inputbox "How many virutal disk to open" 13 50 $count_default 2> /tmp/count
	COUNT=`cat /tmp/count`

	if [ -n "$COUNT" ]; then
	 if grep -q count=[0-9] /etc/kvdisk.conf 2>/dev/null
	 then
		sed "s#count=.*#count=$COUNT#" < /etc/kvdisk.conf > /etc/kvdisk.conf.new
		/bin/mv /etc/kvdisk.conf.new /etc/kvdisk.conf
	 else
		echo "count=$COUNT" > /etc/kvdisk.conf
	 fi

	 select_kvdisk
	 if [ $? -eq 0 ]; then
		select_eth
	 fi
	fi
}

restart_now()
{
	/etc/init.d/kvdisk restart
}

install_raid()
{
 tmpfile=`tempfile 2>/dev/null` || tmpfile=`mktemp /tmp/setup.XXXXX` || tmpfile="/tmp/setup.$$"
 trap "rm -f $tmpfile" 0 1 2 5 15
 [ -z $tmpfile ] && echo "can't create tempfile" && exit
$DIALOG  --title "Found disk" --scrolltext --yesno "$(fdisk -l |grep ^Disk)\n\n Is that true?"  22 45
if [ $? -eq 1 ]
then
 choosed=''
 while [ -z "$choosed" ]
 do
  echo '#!/bin/bash' > $tmpfile.sh 
  echo -n "$DIALOG  --radiolist" >> $tmpfile.sh
  echo -n ' "Please select a raid device:" 20 60 10 ' >> $tmpfile.sh
  echo -n $(cut -d: -f1,3 /usr/share/raiddrivers/list.txt | tr : " " | sed 's/\(.*\)/\1 off/' | xargs -d '\n') >> $tmpfile.sh
  echo -n ' 2>' >> $tmpfile.sh
  echo $tmpfile  >> $tmpfile.sh
  chmod +x $tmpfile.sh
  > $tmpfile
  $tmpfile.sh
  choosed=$(cat $tmpfile)
 done

 [ -f $tmpfile.sh ] && rm -f $tmpfile.sh
 module_name_list=$(grep "^${choosed}:" /usr/share/raiddrivers/list.txt | cut -d: -f2)
for module_name in $(echo  $module_name_list | tr ',' ' ')
do
	 if [ -f /usr/share/raiddrivers/$(uname -m)/${module_name} ]
 	then
		  echo "Installing module ${module_name}"
		  install -D -m 0644 /usr/share/raiddrivers/$(uname -m)/${module_name} /lib/modules/$(uname -r)/updates/${module_name}
		  depmod -a
#  lsmod |grep ahci && rmmod -f $ahci
		  modprobe $(echo $module_name | cut -d. -f1)
		  if [ -f /etc/modprobe.conf ]
		  then
		   grep -v scsi_hostadapter /etc/modprobe.conf > $tmpfile
		    /bin/mv $tmpfile /etc/modprobe.conf
		  fi
		  [ -f /boot/initrd-$(uname -r).img ] && mkinitrd -f /boot/initrd-$(uname -r).img  --with=$(echo $module_name | cut -d. -f1) $(uname -r) 
	 fi
done
#  $DIALOG  --title "Found disk" --scrolltext --yesno "$(fdisk -l |grep ^Disk)\n\n Is that true? !" 22 45
#  if [ $? -eq 1 ]
#  then
#    reboot
#    exit 1
#  fi
fi
}

sync_disk()
{
	$DIALOG --backtitle "$backtitle" --title "Sync disk role" --menu "     Please select a role:" 20 70 10 "source" "   is network sync disk server" "target" "   is network sync disk client" "local" "   is local disk sync" 2> /tmp/task
	if [ $? != 0 ]; then
		return 2
	fi

	task=`cat /tmp/task`
	if [ -z "$task" ]; then
		return 3
	fi
	/bin/rm -f /tmp/task
	case "$task" in
	"source")
		disk_list_all
		if [ ! -f $out_file ]; then
			error "\nSetup could't found any valid harddisk" 12 50
			return 1
		fi
		choosed=''
		while [ -z "$choosed" ]
		do
			$DIALOG --backtitle "$backtitle" --title "sync disk server" --radiolist "Please select source disk" 12 40 4 $(cat $out_file) 2> /tmp/disknum
		
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/disknum)
		done
		sourcedisk=$(cat /tmp/disknum)
		echo "source server begin ready"
		echo "Restore $sourcedisk, ctrl+c to cancel."
		cd /usr/local/bin && ./synctool $sourcedisk
		echo "please ENTER quit"
		read
		cd ~
		;;
	"target")
		disk_list_all
		if [ ! -f $out_file ]; then
			error "\nSetup could't found any valid harddisk" 12 50
			return 1
		fi
		choosed=''
		while [ -z "$choosed" ]
		do
			$DIALOG --backtitle "$backtitle" --title "sync disk target" --radiolist "Please select target disk" 12 40 4 $(cat $out_file)  2> /tmp/disknum
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/disknum)
		done

		 if [ -f /tmp/serverip ]
		 then
			serverip=$(cat /tmp/serverip)
		 else
			serverip="192.168.1.1"
		 fi
		 rm -fr /tmp/serverip
		choosed=''
		 while [ -z "$choosed" ]
		 do
		 	$DIALOG --backtitle "$backtitle" --title "sync disk target" --inputbox "Please input source server address :" 12 50 "$serverip" 2>/tmp/serverip	
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/serverip)
		done
		targetdisk=$(cat /tmp/disknum)
		serverip=$(cat /tmp/serverip)
		echo "target begin sync disk ready"
		echo "Restore $targetdisk from $serverip, ctrl+c to cancel."
		cd /usr/local/bin && ./synctool $targetdisk $serverip
		echo "please ENTER quit"
		read
		cd ~
	;;
	"local")
		disk_list_all
		if [ ! -f $out_file ]; then
			error "\nSetup could't found any valid harddisk" 12 50
			return 1
		fi
		choosed=''
		while [ -z "$choosed" ]
		do
			$DIALOG --backtitle "$backtitle" --title "sync disk server" --radiolist "Please select source disk" 12 40 4 $(cat $out_file) 2> /tmp/disknum
		
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/disknum)
		done
		sourcedisk=$(cat /tmp/disknum)

		disk_list_all $sourcedisk
		if [ ! -f $out_file ]; then
			error "\nSetup could't found any valid harddisk" 12 50
			return 1
		fi

		choosed=''
		while [ -z "$choosed" ]
		do
			$DIALOG --backtitle "$backtitle" --title "sync disk server" --radiolist "Please select target disk" 12 40 4 $(cat $out_file) 2> /tmp/disknum
		
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/disknum)
		done
		target=$(cat /tmp/disknum)
		echo "sourcedisk:$sourcedisk, target:$target"
		cd /usr/local/bin && ./synctool $sourcedisk $target
		echo "please ENTER quit"
		read
		cd ~
		;;
	"source")
		disk_list_all
		if [ ! -f $out_file ]; then
			error "\nSetup could't found any valid harddisk" 12 50
			return 1
		fi
		choosed=''
		while [ -z "$choosed" ]
		do
			$DIALOG --backtitle "$backtitle" --title "sync disk server" --radiolist "Please select source disk" 12 40 4 $(cat $out_file) 2> /tmp/disknum
		
			if [ $? -eq 1 ]; then
				select_operation
			fi
			choosed=$(cat /tmp/disknum)
		done
		sourcedisk=$(cat /tmp/disknum)
		echo "source server begin ready"
		echo "Restore $sourcedisk, ctrl+c to cancel."
		cd /usr/local/bin && ./synctool $sourcedisk
		echo "please ENTER quit"
		read
		cd ~
		;;

	*)
		;;
	esac

}
	
select_operation()
{
  while true
  do
	if [ !  -f /etc/.firstlogin ]
        then
       $DIALOG --backtitle "$backtitle" --title "$title" --menu "Select task:" 20 70 10  "softraid" "Config software raid0 device" "raiddrivers" "Install raid drivers to initrd file" "network" "Set TCP/IP Address" "monitor" "Monitor network/disk/system status"  "reboot" "Reboot the machine" "poweroff" "Poweroff the machine" "update" "Update soft" "clone" "Server disk sync" "exit" "Quit from this menu"  2> /tmp/sel_operate
    #    $DIALOG --backtitle "$backtitle" --title "$title" --menu "Select task:" 20 70 10  "softraid" "Config software raid0 device" "raiddrivers" "Install raid drivers to initrd file" "network" "Set TCP/IP Address" "monitor" "Monitor network/disk/system status"  "reboot" "Reboot the machine" "poweroff" "Poweroff the machine" "update" "Update soft"  "exit" "Quit from this menu"  2> /tmp/sel_operate

	 if [ $? -eq 1 ]; then
  	     exit 0
    	 fi
	operate=`cat /tmp/sel_operate`
	rm -f /tmp/sel_operate

	case "$operate" in
	"network")
		config_network
		;;
	"softraid")
		raid_config
		;;
	"raiddrivers")
		install_raid
		;;
	"poweroff")
		$DIALOG --backtitle "$backtitle" --title "$title" --yesno "Are you sure?" 10 40 10  
		if [ $? -eq 0 ]; then
			poweroff
		fi
		;;
	"reboot")
		$DIALOG --backtitle "$backtitle" --title "$title" --yesno "Are you sure?" 10 40 10  
		if [ $? -eq 0 ]; then
			reboot
		fi
		;;
	"exit")
		exit 0
		;;
	"monitor")
		monitor
		;;
	"update")
		/usr/local/bin/hintupdatever
		;;
	"clone")	
		sync_disk
		;;
	esac 
       else
		config_network
		exit 0
        fi
  done
}
select_operation
