#!/bin/bash

if [ $1 ]; then
	docker stop emby 2>/dev/null
        docker rm emby 2>/dev/null
	cpu_arch=$(uname -m)
	case $cpu_arch in
                "x86_64" | *"amd64"*)
                        docker pull emby/embyserver:4.8.0.56
			;;
                "aarch64" | *"arm64"* | *"armv8"* | *"arm/v8"*)
                        docker pull emby/embyserver_arm64v8:4.8.0.56
                        ;;
                *)
                        echo "目前只支持intel64和amd64架构，你的架构是：$cpu_arch"
                        exit 1
                        ;;
        esac
	
	docker_exist=$(docker images |grep emby/embyserver |grep 4.8.0.56)
	if [ -z "$docker_exist" ]; then
		echo "拉取镜像失败，请检查网络，或者翻墙后再试"
		exit 1
	fi

        if ! grep xiaoya.host /etc/hosts; then
                echo -e "127.0.0.1\txiaoya.host\n" >> /etc/hosts
                xiaoya_host="127.0.0.1"
        else
                xiaoya_host=$(grep xiaoya.host /etc/hosts |awk '{print $1}'|head -n1)
        fi	
	echo "重新安装Emby容器....."
	#wget -q -O /tmp/Emby.Server.Implementations.dll http://docker.xiaoya.pro/Emby.Server.Implementations.dll
	case $cpu_arch in
		"x86_64" | *"amd64"*)
			docker run -d --name emby -v $1/config:/config -v $1/xiaoya:/media -v /etc/nsswitch.conf:/etc/nsswitch.conf --network=host --add-host="xiaoya.host:$xiaoya_host" --user 0:0 --restart always emby/embyserver:4.8.0.56
			#docker cp /tmp/Emby.Server.Implementations.dll emby:/system/
			#docker exec -i emby chmod 644 /system/Emby.Server.Implementations.dll
			#docker restart emby
			echo "一键全家桶全部安装完成"
			;;
		"aarch64" | *"arm64"* | *"armv8"* | *"arm/v8"*)
		        docker run -d --name emby -v $1/config:/config -v $1/xiaoya:/media -v /etc/nsswitch.conf:/etc/nsswitch.conf --network=host  --add-host="xiaoya.host:$xiaoya_host" --user 0:0 --restart always emby/embyserver_arm64v8:4.8.0.56
			#docker cp /tmp/Emby.Server.Implementations.dll emby:/system/
                        #docker exec -i emby chmod 644 /system/Emby.Server.Implementations.dll
                        #docker restart emby
			echo "重新安装Emby容器完成"
                        ;;
		*)
			echo "目前只支持intel64和amd64架构，你的架构是：$cpu_arch"
			exit 1
			;;
	esac		

else
	echo "请在命令后输入 -s /媒体库目录 再重试"
	exit 1
fi

