#!/bin/bash

set -e

isSeed=0
withParam=0
input_string_my_ip=""
input_string_connected_ip=""
input_string_static_peer=""


if [ $# -eq 3 ]; then 
  withParam=1
  isSeed=1
  datadir=${1:-./data/}
  input_string_my_ip=${2}
  regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
  while true 
  do
    if [[ $input_string_my_ip =~ $regex ]]; then
      break 
    fi
  echo ${input_string_my_ip}" is not a valid my ip"
  echo "please input your p2p public ip address as peer: "
  read input_string_my_ip
  done 


  input_string_connected_ip=${3}

  while true 
  do
    if [[ $input_string_connected_ip =~ $regex ]]; then
      break 
    fi
  echo ${input_string_connected_ip}" is not a valid connected ip"
  echo "please input your p2p public ip address as connected ip: "
  read input_string_connected_ip
  done 

fi 


if [ $# -eq 4 ]; then 
  withParam=1
  datadir=${1:-./data/}
  input_string_my_ip=${2}
  input_string_connected_ip=${3}
  input_string_static_peer=${4}
  regex='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
  while true 
  do
    if [[ $input_string_my_ip =~ $regex ]]; then
      break 
    fi
    echo ${input_string_my_ip}" is not a valid my ip"
    echo "please input your p2p public ip address as my service ip: "
    read input_string_my_ip
  done

  while true 
  do
    if [[ $input_string_connected_ip =~ $regex ]]; then
      break 
    fi
    echo ${input_string_connected_ip}" is not a valid connected ip"
    echo "please input your connected sentry public ip address:"
    read input_string_connected_ip
  done


  while true 
  do
    break
    echo "peer----------------"
    echo ${input_string_static_peer##*/}
    echo ${input_string_static_peer##*/} | wc -L


    if [ $(echo ${input_string_static_peer##*/} | wc -L)  -eq 53 ] ; then
        break 
    fi 
    echo ${input_string_static_peer}" is not a valid peer id"
    echo "please input your p2p peer id: "
    read input_string_static_peer
  done 
fi 


if [ $withParam -eq 0 ]; then

echo "please run the cmd with parameter "
echo ""
echo "run the node as seed"
echo "./host.sh /datadir myIpAsP2pSeed connectedSentryIp"

echo "run the node as normal node"
echo "./host.sh /datadir myIpAsP2pSeed connectedSentryIp connectedPeerId"

fi


echo  "isSeed             " $isSeed
echo  "withParam          " $withParam
echo  "input_string_my_ip    " $input_string_my_ip
echo  "input_string_connected_ip    " $input_string_connected_ip
echo  "input_string_static_peer   " $input_string_static_peer


staticPeer=$input_string_static_peer
myIpAddr=$input_string_my_ip
connectedSentryIp=$input_string_connected_ip




N=3

# reset node data dir.
rm  -rf $datadir
mkdir -p $datadir

# prepare mainchain data.
cp  -r ../mainchain/  $datadir

## have two validators in consensus contract.
cp -r  $datadir/mainchain/conf/backv2  $datadir/mainchain/conf/deploy 
## have three validators in consensus contract.
#cp -r  $datadir/mainchain/conf/backv3  $datadir/mainchain/conf/deploy 

# prometheus config
prometheusDir=$datadir/prometheus

echo $prometheusDir

mkdir -p $prometheusDir
chmod 777 $prometheusDir


# nebula config
for i in $(seq 1 $N)
do
  echo "generate nebula config at $i"
  nebula_config_dir=$datadir/nebula/$i
  mkdir -p $nebula_config_dir
  nebula_data_dir=$nebula_config_dir/data
  mkdir -p $nebula_data_dir
  cp nebula/genesis.json $nebula_config_dir/
  configFile=$nebula_config_dir/config.toml
  cp nebula/node$i/config.toml $configFile
done

# sentry config
for i in $(seq 1 $N)
do
  echo "generate sentry config at $i"
  sentry_config_dir=$datadir/sentry/$i
  mkdir -p $sentry_config_dir
  cp sentry/node$i/app.json $sentry_config_dir/app.json
  jq '. + { "rpc_node": "http://'"$connectedSentryIp"':8545" }' $sentry_config_dir/app.json > $sentry_config_dir/app.json.tmp
  mv $sentry_config_dir/app.json.tmp $sentry_config_dir/app.json
done

# explorer config
echo "{"                          > ./explorer/config.json
echo "    \"rpcURL\": \"http://$myIpAddr:18545\""    >> ./explorer/config.json
echo "}"  >> ./explorer/config.json


sed "s/EXPORT_IP/$myIpAddr/g" ./nginx/conf.d/ethnode.conf.temp > /tmp/ethnode.conf.a
sed "s/LOCAL_IP/$connectedSentryIp/g" /tmp/ethnode.conf.a > ./nginx/conf.d/ethnode.conf
rm -f /tmp/ethnode.conf.a

# net config
for i in $(seq 1 $N)
do
  echo "generate metanet config at $i"
  configPath=$datadir/net/$i
  echo  $configPath
  mkdir -p $configPath
  configFile=$configPath/config.yaml
  rm -fv $configFile
  echo "single-node: true" >> $configFile
  echo "log-level: \"debug\"" >> $configFile
  echo "enable-prom: true" >> $configFile
  echo "use-discovery: false" >> $configFile
  echo "nebula-grpc-address: \"nebula:38004\"" >> $configFile
  echo "grpc-host: \"0.0.0.0\"" >> $configFile
  echo "grpc-port: 38010" >> $configFile
  echo "p2p-host-ip: \"$myIpAddr\"" >> $configFile
  private_key=$(openssl rand -hex 32)
  echo -n $private_key > $configPath/private-key
  echo "p2p-priv-key: \"/root/private-key\"" >> $configFile
  echo "generate net $i private key $private_key"
  if [ $i -ne 1 ]; then
    echo "peer: [\"${staticPeer}\"]" >> $configFile
  fi
done


resourcering="
    deploy:
      resources:
        limits:
          cpus: \"36.00\"
          memory: 32G
        reservations:
          cpus: \"30.00\"
          memory: 32G"
#      restart_policy:
#        condition: on-failure
#        delay: 15s
#        max_attempts: 100
#        window: 120s"

resourcenebula="
    deploy:
      resources:
        limits:
          cpus: \"36.00\"
          memory: 32G
        reservations:
          cpus: \"30.00\"
          memory: 32G
      restart_policy:
        condition: on-failure
        delay: 15s
        max_attempts: 100
        window: 120s"        


# deploy="    deploy:
#       restart_policy:
#         condition: on-failure
#         delay: 15s
#         max_attempts: 100
#         window: 120s"



for i in $(seq 1 $N)
do

  composefile="docker-compose_host-$i.yml"
  startfile="start-$i.sh"
  stopfile="stop-$i.sh"

  echo "#!/bin/bash"                                  >  $startfile
  echo "set -e"                                       >> $startfile

  if [ $i -eq 1 ]; then
  #echo "pwd=$PWD"                                   >> $startfile
  echo "cd  $datadir/mainchain"                       >> $startfile
  echo "./scripts/run-testnet.sh"                     >> $startfile
  echo "cd $PWD"                                      >> $startfile
  echo "sleep 10"                                      >> $startfile
  fi 
  echo "docker compose -f  "$composefile"  up    -d"  >> $startfile

  # while [ ! -e "$file_path" ]; do
    #    echo "文件不存在，等待中..."
    #    sleep 1  # 可以调整等待的时间间隔，单位为秒
    #done

  echo "while [ ! -e $datadir/net/$i/peer-id ];do" >> $startfile
  echo "  echo \"waiting for node $i peer-id\"">> $startfile
  echo "  sleep 1"                                     >> $startfile
  echo "done"                                          >> $startfile

  echo "ip=\$(cat $datadir/net/$i/config.yaml | grep "p2p-host-ip"|  grep -o '\".*\"'  | sed 's/[\"\"]//g') "  >> $startfile
  echo "peerid=\$(cat $datadir/net/$i/peer-id)"                  >> $startfile

  echo "sentryIp=\$(cat $datadir/sentry/$i/app.json   |   grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')"  >> $startfile

  echo "echo  ./host.sh  ./dir  replaceCunnentNodePublicIP \$sentryIp   /ip4/\$ip/tcp/30333/p2p/\$peerid"            >> $startfile

  echo "for i in {1..120}"   >> $startfile
  echo "do"                  >> $startfile
  echo "  echo -ne \">\" "   >> $startfile
  echo "done "               >> $startfile

  echo "  echo  \"\" "   >> $startfile
  echo "  echo  \"\" "   >> $startfile

  echo "for i in {1..120}"   >> $startfile
  echo "do"                  >> $startfile
  echo "  echo -ne \">\" "   >> $startfile
  echo "  sleep 1"           >> $startfile
  echo "done "               >> $startfile

  echo "  echo  \"\" "   >> $startfile




  chmod +x $startfile


  echo "#!/bin/bash"                                  >  $stopfile
  echo "set -e"                                       >> $stopfile
  if [ $i -eq 1 ]; then
  echo "cd  $datadir/mainchain"                             >> $stopfile
  echo "./scripts/stop-testnet.sh || true"            >> $stopfile
  echo "cd $PWD"                                     >> $stopfile
  fi 

  echo "docker compose -f  "$composefile"  down"      >> $stopfile

  chmod +x $stopfile



  echo 'version: "3.9"'  >  $composefile
  echo ''                >> $composefile
  echo 'services:'       >> $composefile
  echo ''                >> $composefile
  echo "  cryptor:"                                                    >> $composefile
  echo '    image: caduceus/cmp2-test-net-meta-crypto:v0.2.0 '         >> $composefile
  echo "    container_name: cryptor"                                   >> $composefile
  echo '    deploy:'                                                   >> $composefile
  echo '      restart_policy:'                                         >> $composefile
  echo '        condition: on-failure'                                 >> $composefile
  echo '        delay: 15s'                                            >> $composefile
  echo '        max_attempts: 100'                                     >> $composefile
  echo '        window: 120s'                                          >> $composefile
  echo '    volumes:'                                                  >> $composefile
  echo '      - ./cryptor/config.toml:/root/config.toml'               >> $composefile
  echo '    networks:'                                                 >> $composefile
  echo '      - meta'                                                  >> $composefile
  echo ""   >> $composefile
  echo ""   >> $composefile
  echo "  sentry:"                                                     >> $composefile
  echo '    image: caduceus/cmp2-test-net-meta-sentry:v0.2.0 '         >> $composefile
  echo "    container_name: sentry"                                    >> $composefile
  echo '    deploy:'                                                   >> $composefile
  echo '      restart_policy:'                                         >> $composefile
  echo '        condition: on-failure'                                 >> $composefile
  echo '        delay: 15s'                                            >> $composefile
  echo '        max_attempts: 100'                                     >> $composefile
  echo '        window: 120s'                                          >> $composefile
  echo '    volumes:'                                                  >> $composefile
  echo '      - '$datadir'/sentry/'$i'/app.json:/root/app.json'        >> $composefile
  echo '    networks:'                                                 >> $composefile
  echo '      - meta'                                                  >> $composefile
  echo ""   >> $composefile
  echo ""   >> $composefile
  echo "  net:"                                                        >> $composefile
  echo "    image: caduceus/cmp2-test-net-meta-net:v0.2.0"                  >> $composefile
  echo "    container_name: net"                                       >> $composefile
  echo "    entrypoint: /usr/bin/MetaNet -c /root/config.yaml > /root/net.log 2>&1"       >> $composefile
  # echo "$resourcenebula"                                  >> $composefile
  echo "    ports:"                                       >> $composefile
  echo "      - \"$((38010)):38010\""                     >> $composefile
  echo "      - \"$((30333)):30333\""                     >> $composefile
  echo "      - \"$((30334)):30334/udp\""                     >> $composefile
  echo "    volumes:"                                     >> $composefile
  echo "      - "$datadir"/net/$i:/root"                   >> $composefile
  echo "    networks:"                                    >> $composefile
  echo "      meta:"                                      >> $composefile
  echo "        ipv4_address: 172.99.1.$i"                >> $composefile
  # if [ $i -ne 1 ]; then
  #   echo "    depends_on:"                                >> $composefile
  #   echo "      - net1"                                   >> $composefile
  #fi
  echo ""                                                 >> $composefile



  # if [ $i -eq 1 ]; then
  #   #echo "p2p-priv-key: \"/etc/MetaNet/default-priv-key\"" >> $configFile
  #   echo "" 						                                     >> $composefile
  # else
    echo "  nebula:"                                         >> $composefile
    echo "    image: caduceus/cmp2-test-net-meta-nebula:v0.2.0"       >> $composefile
    echo "    container_name: nebula"                        >> $composefile
    echo "    volumes:"                                      >> $composefile
    echo "      - "$datadir"/nebula/"$i":/root"              >> $composefile
    echo "    depends_on:"                                   >> $composefile
    echo "      - net"                                       >> $composefile
    echo "      - sentry"                                    >> $composefile
    # echo "$resourcenebula"                                   >> $composefile
    echo "    ports:"                                        >> $composefile
    echo "      - $((38004)):38004"                          >> $composefile
    echo "      - $((28000)):28080"                          >> $composefile
    echo "    networks:"                                     >> $composefile
    echo "      - meta"                                      >> $composefile
    echo "" 						                                     >> $composefile
  #fi


  echo "  ring:"                                           >> $composefile
  echo "    image: caduceus/cmp2-test-net-meta-ring:v0.2.0"         >> $composefile
  echo "    container_name: ring"                         >> $composefile
  #echo "    entrypoint: sh -c 'echo "+"ring"+" && sleep 10 && ringd start  --ethconfig.httphost  0.0.0.0 --mempool.sentryurl  sentry:38003   --mempool.nebulaurl nebula:38004  --mempool.p2purl  net:38010  --mempool.cryptourl  cryptor:38001  --mempool.checkerurl txchecker:38002   --mempool.writeunreadytxs=false   --mempool.writerepeatedmsg=false     --mempool.batchconfirminittimeout=60   --mempool.batchconfirmtimeouti=1.5 --mempool.batchrepeatedqueuesize=0' "       >> $composefile
  echo "    entrypoint: sh -c 'echo "+"ring"+" && sleep 50 && ringd start  --ethconfig.httphost  0.0.0.0 --mempool.sentryurl  sentry:38003   --mempool.nebulaurl nebula:38004  --mempool.p2purl  net:38010  --mempool.cryptourl  cryptor:38001  --mempool.checkerurl txchecker:38002   --mempool.writeunreadytxs=false   --mempool.writerepeatedmsg=false     --mempool.batchconfirminittimeout=600   --mempool.batchconfirmtimeouti=1.5 --mempool.batchrepeatedqueuesize=0  --mempool.batchconfirmenable=false    --rpconfig.async=true >>/data/log 2>&1 ' "       >> $composefile
  echo "    volumes:"                                      >> $composefile
  echo "      - "$datadir"/ring/:/data"                        >> $composefile
  echo "    depends_on:"                                   >> $composefile
  echo "      - cryptor"                                   >> $composefile
  echo "      - net"                                       >> $composefile
  echo "      - sentry"                                    >> $composefile
  # echo "$resourcering"                                     >> $composefile
  echo "    ports:"                                        >> $composefile
  echo "      - $((28545)):8545"                           >> $composefile
  echo "      - $((5001)):5001"                            >> $composefile
  echo "      - $((38020)):38020"                          >> $composefile
  echo "      - $((6060)):6060"                            >> $composefile  
  echo "    networks:"                                     >> $composefile
  echo "      - meta"                                      >> $composefile



  # echo "  explorer:"                                           >> $composefile
  # echo "    image: caduceus/cmp2-node-explorer:v0.0.15"         >> $composefile
  # echo "    container_name: explorer"                          >> $composefile
  # echo "    ports:"                                            >> $composefile
  # echo "      - $((10080)):80"                                    >> $composefile

  # echo "    volumes:"                                           >> $composefile  
  # echo "      - ./explorer:/usr/share/nginx/html/config"         >> $composefile   

  # echo "    networks:"                                         >> $composefile
  # echo "      - meta"                                          >> $composefile


  echo "  cadvisor:"                                        >> $composefile
  echo "    image: google/cadvisor:latest"                  >> $composefile
  echo "    container_name: cadvisor"                       >> $composefile
  echo "    ports:"                                         >> $composefile
  echo "      - \"8081:8080\""                              >> $composefile
  echo "    volumes:"                                       >> $composefile
  echo "      - /:/rootfs:ro"                               >> $composefile
  echo "      - /var/run:/var/run:rw"                       >> $composefile
  echo "      - /sys:/sys:ro"                               >> $composefile
  echo "      - /var/lib/docker/:/var/lib/docker:ro"        >> $composefile
  echo "    depends_on:"                                    >> $composefile
  echo "      - sentry"                                     >> $composefile
  echo "      - net"                                        >> $composefile
  if [ $i -ne 1 ]; then
  echo "      - nebula"                                   >> $composefile
  fi
  #echo "      - nebula"                                     >> $composefile
  echo "      - ring"                                       >> $composefile
  echo "    networks:"                                      >> $composefile
  echo "      - meta"                                       >> $composefile




  echo "  prometheus:"                                        >> $composefile
  echo "    image: prom/prometheus:latest"                    >> $composefile
  echo "    container_name: prometheus"                       >> $composefile
  echo "    ports:"                                           >> $composefile
  echo "      - \"9000:9090\""                                >> $composefile
  echo "    command:"                                         >> $composefile
  echo "      - --config.file=/etc/prometheus/prometheus.yml" >> $composefile
  echo "      - --storage.tsdb.path=/prometheus"              >> $composefile
  echo "    volumes:"                                                           >> $composefile
  echo "      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro"  >> $composefile
  echo "      - "$datadir"/prometheus:/prometheus"                                 >> $composefile
  echo "    depends_on:"                                                        >> $composefile
  echo "      - cadvisor"                                                       >> $composefile
  echo "    networks:"                                                          >> $composefile
  echo "      - meta"                                                           >> $composefile

  # echo "  openresty:"                                        >> $composefile
  # echo "    image: openresty/openresty"                    >> $composefile
  # echo "    container_name: openresty"                       >> $composefile
  # echo "    ports:"                                           >> $composefile
  # echo "      - \"80:80\""                                >> $composefile
  # echo "      - \"18545:18545\""                                >> $composefile
  # echo "    volumes:"                                                           >> $composefile
  # echo "      - ./nginx/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"  >> $composefile
  # echo "      - ./nginx/conf.d:/etc/nginx/conf.d"                                 >> $composefile
  # echo "    networks:"                                                          >> $composefile
  # echo "      - meta"                                                           >> $composefile

  echo "networks:"                                                              >> $composefile
  echo "  meta:"                                                                >> $composefile
  echo "    driver: bridge"                                                     >> $composefile
  echo "    ipam:"                                                              >> $composefile
  echo "      config:"                                                          >> $composefile
  echo "        - subnet: 172.99.0.0/16"                                        >> $composefile  




done 

