#!/bin/bash
BASEDIR=$(dirname "$0")

mysql -uroot -ppnetlab -D pnetlab_db -e "CREATE TABLE if_sessions (
 if_session_id bigint(15) NOT NULL AUTO_INCREMENT,
 if_session_lab int(11) DEFAULT NULL,
 if_session_node int(11) DEFAULT NULL,
 if_session_ifid int(11) DEFAULT NULL,
 if_session_type varchar(150) DEFAULT NULL,
 if_session_quality text,
 if_session_suspend int(11) DEFAULT NULL,
 PRIMARY KEY (if_session_id),
 KEY if_session_ifid (if_session_ifid),
 KEY if_session_type (if_session_type),
 KEY if_session_suspend (if_session_suspend),
 KEY if_session_lab (if_session_lab) USING BTREE,
 KEY if_session_node (if_session_node) USING BTREE,
 CONSTRAINT if_sessions_ibfk_1 FOREIGN KEY (if_session_node) REFERENCES node_sessions (node_session_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1" 2>/dev/null

mysql -uroot -ppnetlab -D pnetlab_db -e "ALTER TABLE users CHANGE user_workspace user_workspace TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL" 2>/dev/null
mysql -uroot -ppnetlab -D pnetlab_db -e "ALTER TABLE users ADD max_node INT(11) NULL AFTER user_workspace, ADD INDEX (max_node);" 2>/dev/null
mysql -uroot -ppnetlab -D pnetlab_db -e "ALTER TABLE users ADD max_node_lab INT(11) NULL AFTER max_node, ADD INDEX (max_node_lab);" 2>/dev/null


repo=$(cat /etc/apt/sources.list | grep repo.pnetlab.com)
if [[ "$repo" == "" ]] ; then
	echo "deb [trusted=yes] http://repo.pnetlab.com ./" | tee -a /etc/apt/sources.list
fi


sudo rm -f /opt/unetlab/html/includes/interface
sudo rm -f /opt/unetlab/html/includes/__interfc.php

ln -s /opt/unetlab/html/store/app/Console/Commands/pnetlab /usr/local/sbin/pnetlab
chmod 755 /opt/unetlab/html/store/app/Console/Commands/pnetlab

##########################################

ubuntu=$(lsb_release -a | grep 18.04)
if [[ "$ubuntu" != "" ]] ; then
	sed -i 's/GRUB_DEFAULT=.*/GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 4.15.18-pnetlab2"/g' /etc/default/grub
		
	systemctl enable resolvconf
	sudo perl -i -pe 'BEGIN{undef $/;} s/#?DNSStubListener=[\w]*/DNSStubListener=no/smg' /etc/systemd/resolved.conf
	systemctl restart resolvconf
	
	service systemd-resolved restart
	service pnetnat restart
	
fi

sed -i '/i-share.top/d' /etc/apt/sources.list
echo "deb [trusted=yes] http://i-share.top/repo ./" >> /etc/apt/sources.list
apt-get update
apt-get install ishare -y

################################################
mysql -uroot -ppnetlab -D pnetlab_db -e "replace control (control_name, control_value) values ('ctrl_version','4.2.9')" 2>/dev/null
echo "Upgraded successfully";

###############################################



