#!/bin/sh
#Written by David

localip=`ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'`
IFS="." read a b c d <<< `echo $localip`
lip=$(echo $localip|cut -d\. -f4)
subnet=$a'.'$b'.'$c'.'

if [ "$(id -u)" = "0" ]; then
    echo "This script should not run as root" 1>&2
    exit 1
fi

if [ $d != 6 ] 
then
    echo 'put this on haproxy server'
    exit 1
else
    #generate r.sh
    echo 'sed -i -r "s/192.168.1.2/192.168.'$c'.2/g" $1' > ./r.sh    
    echo 'sed -i -r "s/192.168.1.3/192.168.'$c'.3/g" $1' >> ./r.sh    
    echo 'sed -i -r "s/192.168.1.4/192.168.'$c'.4/g" $1' >> ./r.sh    
    echo 'sed -i -r "s/192.168.1.5/192.168.'$c'.5/g" $1' >> ./r.sh    
    echo 'sed -i -r "s/192.168.1.6/192.168.'$c'.6/g" $1' >> ./r.sh    
    echo 'sed -i -r "s/80001/8000'$c'/g" $1' >> ./r.sh    
    echo 'sed -i -r "s/fb66680001/fb6668000'$c'/g" $1' >> ./r.sh    
    chmod +x ./r.sh

    #sudo sed zf
    if [ ! -f /etc/haproxy/forward ]
    then
        echo 'forward file not found!'
        exit 1
    else
        sudo ./r.sh /etc/haproxy/forward
        curl -O https://12cf.cc/share/update.sh
        chmod +x update.sh
        sudo ./update.sh
        #echo 'proxy modified'    
    fi


    #sudo start haproxy
    sudo systemctl start haproxy
    echo 'proxy started'    
fi

if [ ! -f ~/.ssh/zhihui ]
then
    echo 'key file not found!'
    exit 1
else
    ./r.sh ~/.ssh/config
fi

if ! ping -c1 -W1 $subnet'2' &> /dev/null
then
    echo 'hall not reachable'
    exit 1
else
    scp r.sh hall:/home/user
    ssh hall "chmod +x ~/r.sh"
    #edit nacos
    ssh hall "find /opt/gameserver -name nacos.dat 2>/dev/null 2>/dev/null |xargs -0 ~/r.sh"
    #sed hall
    ssh hall "find /opt/gameserver/ -name hall.dat 2>/dev/null |xargs -0 ~/r.sh"
    #start hall
    echo 'hall modifeid'
fi


if ! ping -c1 -W1 $subnet'3' &> /dev/null
then
   echo 'video not reachable'
   exit 1
else
    scp r.sh video:/home/user
    ssh video "chmod +x ~/r.sh"
    #edit nacos
    ssh video "find /opt/gameserver -name nacos.dat 2>/dev/null 2>/dev/null |xargs -0 ~/r.sh"
    #sed video 
    ssh video "find /opt/gameserver/ -name gameserver.dat 2>/dev/null |xargs -0 ~/r.sh"
    echo 'video modified'
fi

if ! ping -c1 -W1 $subnet'4' &> /dev/null
then
   echo 'fish not reachable'
   exit 1
else
    scp r.sh fish:/home/user
    ssh fish "chmod +x ~/r.sh"
    #edit nacos
    #ssh fish "find /opt/gameserver -name nacos.dat 2>/dev/null 2>/dev/null |xargs -0 ~/r.sh"
    #sed fish 
    ssh fish "find /opt/gameserver/ -name server.conf 2>/dev/null |xargs -0 ~/r.sh"
    echo 'fish modified'
fi

if ! ping -c1 -W1 $subnet'5' &> /dev/null
then
    echo 'game not reachable'
    exit 1
else
    scp r.sh game:/home/user
    ssh game "chmod +x ~/r.sh"
    #edit nacos
    ssh game "find /opt/gameserver -name nacos.dat 2>/dev/null 2>/dev/null |xargs -0 ~/r.sh"
    #sed game 
    ssh game "find /opt/gameserver/ -name gameserver.dat 2>/dev/null |xargs -0 ~/r.sh"
    echo 'game modified'
fi
