#!/bin/sh

                local_sha=$(docker inspect --format='{{index .RepoDigests 0}}' ddsderek/xiaoya-proxy:latest  |cut -f2 -d:)
                remote_sha=$(curl --ipv4 -s "https://hub.docker.com/v2/repositories/ddsderek/xiaoya-proxy/tags/latest"|grep -o '"digest":"[^"]*' | grep -o '[^"]*$'  |tail -n1 |cut -f2 -d:)
                if [ ! "$local_sha" == "$remote_sha" ]; then
                        docker rmi ddsderek/xiaoya-proxy:latest
                fi

docker run -d --restart=always  --name=xiaoya-proxy --net=host ddsderek/xiaoya-proxy:latest
config_dir="$(docker inspect --format='{{range $v,$conf := .Mounts}}{{$conf.Source}}:{{$conf.Destination}}{{$conf.Type}}~{{end}}' xiaoya | tr '~' '\n' | grep bind | sed 's/bind//g' | grep ":/data$" | awk -F: '{print $1}')"
#localip=$(ip a | grep 192.168 |cut -f1 -d/|awk '{print $2}')
local_dev=$(ip route |grep default|awk '{print $5}'|head -n1)
localip=$(ip a | grep $local_dev |grep inet|head -n1 |cut -f1 -d/|awk '{print $2}')
echo "http://$localip:9988" > $config_dir/xiaoya_proxy.txt
