#!/bin/bash ######Version 2 By Maosen 20170419######
# @author Maosen Sun <sunnymaosen@gmail.com,sun285529808@qq.com>
# @copyright Maosen Sun
# @organization EmulatedLab(QQ Group:593920308)
# @link http://www.eve-ng.com
# @version 20170610
qemu=$(pwd)
icon="/opt/unetlab/html/images/icons"
include="/opt/unetlab/html/includes"
template="/opt/unetlab/html/templates" 
if [ ${qemu:25:10} == 'hillstone-' ]
then
	mv -f $qemu/Hillstone.png $icon/Hillstone.png
	mv -f $qemu/hillstone.php $template/hillstone.php
	if grep -i \'hillstone\' $include/init.php > /dev/null
	then
		echo -e "\033[33mSorry!!!\033[0m Maybe you has already import it!"
	else
		sed -i "/Virtual PC/i\\\t\t'hillstone'\t\t\t=>\t'HillStone SG'," $include/init.php
		echo -e "\033[31mGreat!!!\033[0m \"HillStone Device\" import successfully!!!"
	fi
	###Optimize the display name of the interface###
	if grep -i \'vsr\' $include/__node.php > /dev/null
	then
		touch $include/__node.php
	else
		cp /opt/unetlab/html/includes/__node.php{,.`date +%s`}&&sed -i "/case 'a10':/i\\\t\t\t\t\tcase 'hillstone':\n\t\t\t\t\t\tfor (\$i = 0; \$i < \$this -> ethernet; \$i++) {\n\t\t\t\t\t\t\t\$n = 'ethernet0/'.\$i;                // Interface name\n\t\t\t\t\t\t\tif (isset(\$old_ethernets[\$i])) {\n\t\t\t\t\t\t\t\t// Previous interface found, copy from old one\n\t\t\t\t\t\t\t\t\$this -> ethernets[\$i] = \$old_ethernets[\$i];\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\$this -> ethernets[\$i] = new Interfc(Array('name' => \$n, 'type' => 'ethernet'), \$i);\n\t\t\t\t\t\t\t\t} catch (Exception \$e) {\n\t\t\t\t\t\t\t\t\terror_log(date('M d H:i:s ').'ERROR: '.\$GLOBALS['messages'][40020]);\n\t\t\t\t\t\t\t\t\terror_log(date('M d H:i:s ').(string) \$e);\n\t\t\t\t\t\t\t\t\treturn 40020;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// Setting CMD flags (virtual device and map to TAP device)\n\t\t\t\t\t\t\t\$this -> flags_eth .= ' -device %NICDRIVER%,netdev=net'.\$i.',mac=50:'.sprintf('%02x', \$this -> tenant).':'.sprintf('%02x', \$this -> id / 512).':'.sprintf('%02x', \$this -> id % 512).':00:'.sprintf('%02x', \$i);\n\t\t\t\t\t\t\t\$this -> flags_eth .= ' -netdev tap,id=net'.\$i.',ifname=vunl'.\$this -> tenant.'_'.\$this -> id.'_'.\$i.',script=no';\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n" /opt/unetlab/html/includes/__node.php
	fi
	rm -rf $qemu/hillstone.sh
else
	echo -e "\033[31mSorry!!!\033[0m Your folder name is NOT legal, please modify the directory name, then run the script again !!! \n\033[32mFor example: \"hillstone-xxxx\"\033[0m"
fi

