#!/bin/bash 

#set -e 
#exit immediately if a command fails

CHANNEL="stable"
DOWNLOAD_URL="https://download.docker.com"

command_exists() {
	command -v "$@" > /dev/null 2>&1
}

get_distribution() {
	lsb_dist=""
	# Every system that we officially support has /etc/os-release
	if [ -r /etc/os-release ]; then
		lsb_dist="$(. /etc/os-release && echo "$ID")"
	fi
	# Returning an empty string here should be alright since the
	# case statements don't act unless you provide an actual value
	echo "$lsb_dist"
}

install_all() {
	apt update
}

install_docker() {
	sh_c='sh -c'

	echo "Checking distribution..."
	lsb_dist=$( get_distribution )
	lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"
	case "$lsb_dist" in

		ubuntu)
			if command_exists lsb_release; then
				dist_version="$(lsb_release --codename | cut -f2)"
			fi
			if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
				dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
			fi
		;;

		debian|raspbian)
			dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
			case "$dist_version" in
				10)
					dist_version="buster"
				;;
				9)
					dist_version="stretch"
				;;
				8)
					dist_version="jessie"
				;;
			esac
		;;

		centos|rhel)
			if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
				dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
			fi
		;;

		*)
			if command_exists lsb_release; then
				dist_version="$(lsb_release --release | cut -f2)"
			fi
			if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
				dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
			fi
		;;

	esac
	
	echo "Installing..."
	case "$lsb_dist" in
		ubuntu|debian|raspbian)
			pre_reqs="apt-transport-https ca-certificates curl"
			if [ "$lsb_dist" = "debian" ]; then
				if [ "$(uname -m)" = "aarch64" ] && [ "$dist_version" = "jessie" ]; then
					add_debian_backport_repo "$dist_version"
				fi
			fi

			if ! command -v gpg > /dev/null; then
				pre_reqs="$pre_reqs gnupg"
			fi
			apt_repo="deb [arch=$(dpkg --print-architecture)] $DOWNLOAD_URL/linux/$lsb_dist $dist_version $CHANNEL"
			(
				$sh_c 'apt-get update -qq >/dev/null'
				$sh_c "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $pre_reqs >/dev/null"
				$sh_c "curl -fsSL \"$DOWNLOAD_URL/linux/$lsb_dist/gpg\" | apt-key add -qq - >/dev/null"
				$sh_c "echo \"$apt_repo\" > /etc/apt/sources.list.d/docker.list"
				$sh_c 'apt-get update -qq >/dev/null'
			)
			pkg_version=""
			if [ -n "$VERSION" ]; then
				# Will work for incomplete versions IE (17.12), but may not actually grab the "latest" if in the test channel
				pkg_pattern="$(echo "$VERSION" | sed "s/-ce-/~ce~.*/g" | sed "s/-/.*/g").*-0~$lsb_dist"
				search_command="apt-cache madison 'docker-ce' | grep '$pkg_pattern' | head -1 | awk '{\$1=\$1};1' | cut -d' ' -f 3"
				pkg_version="$($sh_c "$search_command")"
				echo "INFO: Searching repository for VERSION '$VERSION'"
				echo "INFO: $search_command"
				if [ -z "$pkg_version" ]; then
					echo
					echo "ERROR: '$VERSION' not found amongst apt-cache madison results"
					echo
					return 1
				fi
				search_command="apt-cache madison 'docker-ce-cli' | grep '$pkg_pattern' | head -1 | awk '{\$1=\$1};1' | cut -d' ' -f 3"
				cli_pkg_version="$($sh_c "$search_command")"
				pkg_version="=$pkg_version"
			fi
			(
				if [ -n "$cli_pkg_version" ]; then
					$sh_c "apt-get install -y -qq --no-install-recommends docker-ce-cli=$cli_pkg_version >/dev/null"
				fi
				$sh_c "apt-get install -y -qq --no-install-recommends docker-ce$pkg_version >/dev/null"
			)
			if command_exists docker; then
				echo "Docker installed!"
			else
				echo "Problem with installing Docker!"
			fi
			return 0
			;;
		centos|fedora|rhel)
			yum_repo="$DOWNLOAD_URL/linux/$lsb_dist/$REPO_FILE"
			if ! curl -Ifs "$yum_repo" > /dev/null; then
				echo "Error: Unable to curl repository file $yum_repo, is it valid?"
				return 1
			fi
			if [ "$lsb_dist" = "fedora" ]; then
				pkg_manager="dnf"
				config_manager="dnf config-manager"
				enable_channel_flag="--set-enabled"
				disable_channel_flag="--set-disabled"
				pre_reqs="dnf-plugins-core"
				pkg_suffix="fc$dist_version"
			else
				pkg_manager="yum"
				config_manager="yum-config-manager"
				enable_channel_flag="--enable"
				disable_channel_flag="--disable"
				pre_reqs="yum-utils"
				pkg_suffix="el"
			fi
			(
				$sh_c "$pkg_manager install -y -q $pre_reqs"
				$sh_c "$config_manager --add-repo $yum_repo"

				if [ "$CHANNEL" != "stable" ]; then
					$sh_c "$config_manager $disable_channel_flag docker-ce-*"
					$sh_c "$config_manager $enable_channel_flag docker-ce-$CHANNEL"
				fi
				$sh_c "$pkg_manager makecache"
			)
			pkg_version=""
			if [ -n "$VERSION" ]; then
				pkg_pattern="$(echo "$VERSION" | sed "s/-ce-/\\\\.ce.*/g" | sed "s/-/.*/g").*$pkg_suffix"
				search_command="$pkg_manager list --showduplicates 'docker-ce' | grep '$pkg_pattern' | tail -1 | awk '{print \$2}'"
				pkg_version="$($sh_c "$search_command")"
				echo "INFO: Searching repository for VERSION '$VERSION'"
				echo "INFO: $search_command"
				if [ -z "$pkg_version" ]; then
					echo
					echo "ERROR: '$VERSION' not found amongst $pkg_manager list results"
					echo
					return 1
				fi
				search_command="$pkg_manager list --showduplicates 'docker-ce-cli' | grep '$pkg_pattern' | tail -1 | awk '{print \$2}'"
				cli_pkg_version="$($sh_c "$search_command" | cut -d':' -f 2)"
				pkg_version="-$(echo "$pkg_version" | cut -d':' -f 2)"
			fi
			(
				if [ -n "$cli_pkg_version" ]; then
					$sh_c "$pkg_manager install -y -q docker-ce-cli-$cli_pkg_version"
				fi
				$sh_c "$pkg_manager install -y -q docker-ce$pkg_version"
			)
			return 0
			;;
		*)
			if [ -z "$lsb_dist" ]; then
				if is_darwin; then
					echo "ERROR: Unsupported operating system 'macOS'"
					return 1
				fi
			fi
			echo "ERROR: Unsupported distribution '$lsb_dist'"
			return 1
			;;
	esac
	return 1
}

install_ffmpeg() {
	sh_c='sh -c'
	echo "Installing FFMPEG..."
	$sh_c 'apt-get update >/dev/null'
	$sh_c 'apt-get install ffmpeg -y >/dev/null'
	echo "FFMPEG installed!"
}

start_ffmpeg_video_stream() {
	echo "Enter display (for example :0) ID: "
	read displayID
	screen ffmpeg -video_size 1920x1200 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 480x300 -b:v 1700k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim
}

start_ffmpeg_audio_stream() {
	echo "Enter audio device (for example hw:0,1) ID: "
	read hardwareID

	#insert loopback device
	sudo modprobe snd_aloop pcm_substreams=1
	ffmpeg -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k -muxdelay 0.001 http://localhost:8081/tzitzimgdolim
}

start_ffmpeg_audio_video_display0() {
displayID=:0
hardwareID=hw:Loopback,1,0
#insert loopback device
sudo modprobe snd_aloop pcm_substreams=1

#ffmpeg -video_size 1600x900 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 480x300 -b:v 1700k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k  </dev/null 1>/dev/null 2>/dev/null &

#16:9 -  ffmpeg -video_size 1600x900 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 640x360 -b:v 2000k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k  </dev/null 1>/dev/null 2>/dev/null &
#16:10 - ffmpeg -video_size 1920x1200 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 640x400 -b:v 2000k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k  </dev/null 1>/dev/null 2>/dev/null &

#PRESET 2k  
#ffmpeg -video_size 1600x900 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 800x450 -b:v 2000k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k  </dev/null 1>/dev/null 2>/dev/null &
#ffmpeg -video_size 1600x900 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 800x450 -b:v 1900k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k  </dev/null 1>/dev/null 2>/dev/null &

#LGN
#ffmpeg -video_size 1600x900 -f x11grab -i $displayID -f v4l2 -framerate 25 -f mpegts -codec:v mpeg1video -s 800x450 -b:v 1600k -bf 0 \
#-muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100 -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k </dev/null 1>/dev/null 2>/dev/null &

#NOTE -r je capture framerate , fps= je output framerate !!!
ffmpeg -hwaccel auto -video_size 1600x900 -f x11grab -r 25 -i $displayID -f alsa -ar 44100 -i $hardwareID -filter_complex \
"[0:v]crop=1280:720:160:55,split=3[s0][s1][s2]; \
 [s0]scale=800:-2,fps=25[v0]; \
 [s1]scale=800:-2,fps=30[v1]; \
 [s2]scale=400:-2,fps=30[v2]" \
-map "[v0]" -map 1:a -c:v mpeg1video -b:v 1600k -bf 0 -c:a mp2 -b:a 32k -muxdelay 0.001 -f mpegts http://localhost:8081/tzitzimgdolim \
-map "[v1]" -map 1:a -c:v libx264 -c:a aac -pix_fmt yuv420p -probesize 32 -g 15 -bf 2 -b:v 1600k -b:a 64k -crf 27 -profile:v baseline -flags +global_header -preset veryfast -tune fastdecode -tune zerolatency -flvflags no_duration_filesize -f flv "rtmp://localhost:1965/live/streamMedium" \
-map "[v2]" -map 1:a -c:v libx264 -c:a aac -pix_fmt yuv420p -probesize 32 -g 15 -bf 2 -b:v 1200k -b:a 32k -crf 27 -profile:v baseline -flags +global_header -preset veryfast -tune fastdecode -tune zerolatency -flvflags no_duration_filesize -f flv "rtmp://localhost:1965/live/streamLow" </dev/null 1>/dev/null 2>/dev/null &
}

start_ffmpeg_audio_video_camera() {
hardwareID=hw:Loopback,1,0
#insert loopback device
sudo modprobe snd_aloop pcm_substreams=1

#PRESET 0
#ffmpeg -rtsp_transport tcp -i "rtsp://10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f v4l2 -framerate 30 -f mpegts -codec:v mpeg1video -s 600x450 -b:v 1800k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100 -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k </dev/null 1>/dev/null 2>/dev/null &

#LGN
#ffmpeg -rtsp_transport tcp -i "rtsp://10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f v4l2 -framerate 30 -f mpegts -codec:v mpeg1video -s 600x450 -b:v 1800k -bf 0 \
#-muxdelay 0.001 http://localhost:8081/tzitzimgdolim -f alsa -ar 44100 -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k </dev/null 1>/dev/null 2>/dev/null &
#todo grab from MIC_IN

ffmpeg  -rtsp_transport tcp -i "rtsp://10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f alsa -ar 44100 -i $hardwareID -filter_complex \
"[0:v]split=3[s0][s1][s2]; \
 [s0]scale=600:-2[v0]; \
 [s1]scale=600:-2[v1]; \
 [s2]scale=400:-2[v2]" \
-map "[v0]" -map 1:a -c:v mpeg1video -b:v 1600k -bf 0 -c:a mp2 -b:a 32k -muxdelay 0.001 -f mpegts http://localhost:8081/tzitzimgdolim \
-map "[v1]" -map 1:a -c:v libx264 -c:a aac -pix_fmt yuv420p -probesize 32 -g 15 -bf 2 -b:v 1600k -b:a 64k -crf 27 -profile:v baseline -flags +global_header -preset veryfast -tune fastdecode -tune zerolatency -flvflags no_duration_filesize -f flv "rtmp://localhost:1965/live/streamMedium" \
-map "[v2]" -map 1:a -c:v libx264 -c:a aac -pix_fmt yuv420p -probesize 32 -g 15 -bf 2 -b:v 1200k -b:a 32k -crf 27 -profile:v baseline -flags +global_header -preset veryfast -tune fastdecode -tune zerolatency -flvflags no_duration_filesize -f flv "rtmp://localhost:1965/live/streamLow" </dev/null 1>/dev/null 2>/dev/null &

#WORKS - only h264 vid+audio capture
#ffmpeg -rtsp_transport tcp -i "rtsp://10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f alsa -ar 44100 -i "hw:Loopback,1,0" -c:a aac -b:a 64k -pix_fmt yuv420p -framerate 30 -c:v libx264 -s 480x320 -probesize 32 -b:v 1600k -bf 2 -g 15 -crf 27 -profile:v baseline -flags +global_header -preset veryfast -tune fastdecode -tune zerolatency -flvflags no_duration_filesize -f flv "rtmp://localhost:1965/live/streamMedium" </dev/null 1>/dev/null 2>/dev/null &

#OBSOLETE
#ffmpeg -rtsp_transport tcp -i "rtsp://<USERNAME>:<PASSWORD>@10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f v4l2 -framerate 30 -f mpegts -codec:v mpeg1video -s 480x320 -b:v 700k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim </dev/null 1>/dev/null 2>/dev/null &
#ffmpeg -rtsp_transport tcp -i "rtsp://10.5.10.240/axis-media/media.amp?videocodec=h264&resolution=800x600" -f v4l2 -framerate 30 -f mpegts -codec:v mpeg1video -s 480x320 -b:v 700k -bf 0 -muxdelay 0.001 http://localhost:8081/tzitzimgdolim </dev/null 1>/dev/null 2>/dev/null &
#ffmpeg -f alsa -ar 44100  -i $hardwareID -f mpegts -codec:a mp2 -b:a 32k -muxdelay 0.001 http://localhost:8081/tzitzimgdolim </dev/null 1>/dev/null 2>/dev/null &
}


start_ffmpeg_nanocosmos_stream() {
displayID=:0
hardwareID=hw:Loopback,1,0
#insert loopback device
sudo modprobe snd_aloop pcm_substreams=1

ffmpeg -hwaccel auto -video_size 1600x900 -f x11grab -r 25 -i $displayID -f alsa -ar 44100 -i $hardwareID -vf crop=1280:720:160:55 -c:v libx264 -x264-params "nal-hrd=cbr" -c:a aac -pix_fmt yuv420p -preset veryfast -tune zerolatency -crf 18 -s 852x480 -b:v 800k -b:a 64k -g 60 -f flv rtmp://bintu-vtrans.nanocosmos.de:1935/live/Yeowq-yT42S
RESULT=$?
if ($RESULT -ne 255); then
  start_ffmpeg_nanocosmos_stream
fi
}

set_firewall_rules() {
	ufw allow 7997 comment "YServer WebSocket"
	ufw allow 8889 comment "Game HTTP Server"
	#ufw allow 8080 comment "FFMPEG Stream"
	#ufw allow 8081 comment "FFMPEG Stream"
	ufw allow 8082 comment "FFMPEG Stream"
	ufw allow 7072 comment "FFMPEG Stream2"
	#ufw allow 13273 comment "FireBird Events"
}

unset_firewall_rules() {
	ufw delete allow 7997
	ufw delete allow 8889
	#ufw delete allow 8080
	#ufw delete allow 8081
	ufw delete allow 8082
	ufw delete allow 7072
	#ufw delete allow 13273
}

download_WEBSERVER_image() {
#	echo "Enter FTP username:"
#	read username
#	stty -echo
#	printf "Enter FTP password: "
#	read password
#	stty echo
#	printf "\n"
	if command_exists docker; then
		IMAGE_FILE_NAME="imaxa_gameserver_image.tar"
		file=$IMAGE_FILE_NAME
		if [ -f $file ] ; then
    		rm $file
		fi
		HOST="igpupdates.4knh.com"
		USERNAME=roulette
		PASS=updates
		FOLDER="WEB_ROULETTE"
		wget ftp://$USERNAME:$PASS@$HOST/$FOLDER/$IMAGE_FILE_NAME
		# docker rm $(docker stop $(docker ps -a -q --filter ancestor=imaxaroulette --format="{{.ID}}"))
		# set +e
		# docker rmi imaxaroulette
		# set -e
		# docker load -i $file
	else
		echo "Cannot update the image, Docker is not installed!"
	fi
}

run_WEBSERVER_container() {
	if command_exists docker; then
#		docker run -p 8889:8043 --rm -d imaxaroulette
                docker run -v /etc/letsencrypt/:/etc/letsencrypt -v /home/ROULETTE/Games/:/usr/src/app/public  -p 8889:8043 --rm -d imaxagameserver
	else
		echo "Cannot run the container, Docker is not installed!"
	fi
}

download_video_relay_image() {
#	echo "Enter FTP username:"
#	read username
#	stty -echo
#	printf "Enter FTP password: "
#	read password
#	stty echo
#	printf "\n"
	if command_exists docker; then
		IMAGE_FILE_NAME="imaxa_wssrelay_image.tar"
		file=$IMAGE_FILE_NAME
		if [ -f $file ] ; then
    		rm $file
		fi
		HOST="igpupdates.4knh.com"
		USERNAME=roulette
		PASS=updates
		FOLDER="WEB_ROULETTE"
		wget ftp://$USERNAME:$PASS@$HOST/$FOLDER/$IMAGE_FILE_NAME
		set +e
		docker rmi imaxawsrelay
		docker rmi imaxawssrelay
		set -e
		docker load -i $file
	else
		echo "Cannot update the image, Docker is not installed!"
	fi
}

run_relay_container() {
	if command_exists docker; then
		docker run -v /etc/letsencrypt/:/etc/letsencrypt -p 8081:8081 -p 8082:8082 -d imaxawssrelay
	else
		echo "Cannot run the container, Docker is not installed!"
	fi
}

stop_relay_container() {
	if command_exists docker; then
		docker rm $(docker stop $(docker ps -a -q --filter ancestor=imaxawsrelay --format="{{.ID}}"))
		docker rm $(docker stop $(docker ps -a -q --filter ancestor=imaxawssrelay --format="{{.ID}}"))
	else
		echo "Cannot run the command, Docker is not installed!"
	fi
}


#------------------------------------------
download_nginx_image() {
#	echo "Enter FTP username:"
#	read username
#	stty -echo
#	printf "Enter FTP password: "
#	read password
#	stty echo
#	printf "\n"
	if command_exists docker; then
		IMAGE_FILE_NAME="nginx_https_image.tar"
		file=$IMAGE_FILE_NAME
		if [ -f $file ] ; then
    		rm $file
		fi
		HOST="igpupdates.4knh.com"
		USERNAME=roulette
		PASS=updates
		FOLDER="WEB_ROULETTE"
		wget ftp://$USERNAME:$PASS@$HOST/$FOLDER/$IMAGE_FILE_NAME
		set +e
		docker rmi imaxanginxhttps
		set -e
		docker load -i $file
	else
		echo "Cannot update the image, Docker is not installed!"
	fi
}

run_nginx_container() {
	if command_exists docker; then
		docker run -v /etc/letsencrypt/:/etc/letsencrypt -p 7072:7075 -p 1965:1965 -d imaxanginxhttps
	else
		echo "Cannot run the container, Docker is not installed!"
	fi
}

stop_nginx_container() {
	if command_exists docker; then
		docker rm $(docker stop $(docker ps -a -q --filter ancestor=imaxanginxhttps --format="{{.ID}}"))
	else
		echo "Cannot run the command, Docker is not installed!"
	fi
}
#------------------------------------


stop_WEBSERVER_container() {
	if command_exists docker; then
		docker rm $(docker stop $(docker ps -a -q --filter ancestor=imaxagameserver --format="{{.ID}}"))
	else
		echo "Cannot run the command, Docker is not installed!"
	fi
}

remove_image_from_docker() {
	if command_exists docker; then
		docker rmi imaxaroulette
	else
		echo "Cannot run the command, Docker is not installed!"
	fi
}

load_new_image_to_docker() {
	IMAGE_FILE_NAME="imaxa_gameserver_image.tar"
	if command_exists docker; then
		docker load -i $IMAGE_FILE_NAME
	else
		echo "Cannot run the command, Docker is not installed!"
	fi
}

install_libs() {
	apt update
	apt install libcpprest2.10
	apt install libdc1394-22
	apt install libzmq5
	apt install librubberband2
	apt install libmysofa
	apt install libmysofa0
	apt install libflite1
	apt install libjsoncpp-dev
}

update_db_fc5() {
	chroot /home/ROULETTE/FC5 /RouletteMaster/EMPTY_DATABASE/repair_db_level2.sh

}


is_user_root () { 
	[ ${EUID:-$(id -u)} -eq 0 ];
}

if ! is_user_root; then
    echo 'Please run as root!'
    exit 0 
fi


if [ -n "$1" ]; then
 choice=$1
else
echo "-----------------------------"
echo "   WEB ROULETTE INSTALLER    "
echo "-----------------------------"
echo "1  SETUP ALL"
echo "-----------------------------"
echo "2  INSTALL DOCKER"
echo "3  INSTALL FFMPEG"
echo "         "
echo "31 START FFMPEG VIDEO STREAM"
echo "32 START FFMPEG AUDIO STREAM"
echo "33 START FFMPEG AUDIO/VIDEO STREAM from DISPLAY=:0"
echo "34 START FFMPEG AUDIO/VIDEO STREAM from IPCAMERA"
echo "         "
echo "44 START FFMPEG NANOCOSMOS STREAM from DISPLAY=:0"
echo "         "
echo "-----------------------------"
echo "4  SET FIREWALL RULES"
echo "5  UNSET FIREWALL RULES"
echo "-----------------------------"
echo "6  UPDATE GAME IMAGE "
echo "                           "
echo "61 DOWNLOAD GAME IMAGE"
echo "62 STOP ROULETTE CONTAINER"
echo "63 REMOVE IMAGE FROM DOCKER"
echo "64 LOAD NEW IMAGE TO DOCKER"
echo "                            "
echo "7  RUN GAME CONTAINER"
echo "-----------------------------"
echo "8  DOWNLOAD VIDEO RELAY IMAGE"
echo "9  RUN RELAY CONTAINER"
echo "10 STOP RELAY CONTAINER"
echo "-----------------------------"
echo "13  DOWNLOAD NGINX IMAGE"
echo "14  RUN NGINX CONTAINER"
echo "15 STOP NGINX CONTAINER"
echo "-----------------------------"
echo "11 INSTALL LIBS"
echo "-----------------------------"
echo "12 UPDATE DB WITH NEW VERSION - FC5"
echo "-----------------------------"
echo "0  EXIT"
echo "-----------------------------"
read choice
fi

case "$choice" in

    1)
		install_docker
		install_ffmpeg
		set_firewall_rules
		install_libs
		update_db_fc5

		download_WEBSERVER_image
		stop_WEBSERVER_container
		remove_image_from_docker
		load_new_image_to_docker

		download_video_relay_image
		run_relay_container

		start_ffmpeg_audio_video_display0

		run_game_container
        	;;
        2)
	        install_docker
        	;;
	3)
		install_ffmpeg
		;;
	31)
		start_ffmpeg_video_stream
		;;
	32)
		start_ffmpeg_audio_stream
		;;
	33)
		start_ffmpeg_audio_video_display0
		;;
	34)
		start_ffmpeg_audio_video_camera
		;;
	44)
		start_ffmpeg_nanocosmos_stream
		;;
	4)
        	set_firewall_rules
		;;
	5)
	        unset_firewall_rules
		;;
	6)
        	download_WEBSERVER_image
		stop_WEBSERVER_container
		remove_image_from_docker
		load_new_image_to_docker
		run_WEBSERVER_container
		;;
	61) 
		download_WEBSERVER_image
		;;
	62) 
		stop_WEBSERVER_container
		;;
	63)
		remove_image_from_docker
		;;
	64)
		load_new_image_to_docker
		;;
	7)
		run_WEBSERVER_container
		;;
	8)
	        download_video_relay_image
		;;
	9)
		run_relay_container
		;;
	10)
		stop_relay_container
		;;
	13)
	        download_nginx_image
		;;
	14)
		run_nginx_container
		;;
	15)
		stop_nginx_container
		;;
	11)
		install_libs
		;;
	12)
		update_db_fc5
		;;
	0)
		exit 0
		;;
    *)
        echo "$choice is not a valid choice!"
        ;;
esac

