apiVersion: v1
kind: ConfigMap
metadata:
  name: rep-scripts
  namespace: tidb-serverless
data:
  replication-worker-startup-script: |
    #!/bin/sh

    ulimit -c 0
    set -uo pipefail

    ANNOTATIONS="/etc/podinfo/annotations"
    if [ -f "${ANNOTATIONS}" ]; then
      . "${ANNOTATIONS}" 2>/dev/null
    else
      echo "warning: ${ANNOTATIONS} doesn't exist, continue without pod annotations." >&2
    fi

    runmode=${runmode:-normal}
    if [ "X${runmode}" = "Xdebug" ]; then
      echo "entering debug mode."
      tail -f /dev/null
    fi

    CONTAINER_NAME=${CONTAINER_NAME:-$HOSTNAME}
    case "${CONTAINER_NAME}" in
      *-[0-9]*)
        REPLICATION_WORKER_INDEX=${CONTAINER_NAME##*-}
        ;;
      *)
        echo "failed to extract ordinal from CONTAINER_NAME=${CONTAINER_NAME}" >&2
        exit 1
        ;;
    esac
    export REPLICATION_WORKER_INDEX

    set -- \
      --addr=0.0.0.0:19000 \
      --pd-endpoints=serverless-cluster-pd.tidb-serverless.svc:2379 \
      --config=/etc/config/replication-worker.toml \
      --data-dir=/data \
      "$@"

    echo "starting tikv-worker ..."
    echo "REPLICATION_WORKER_INDEX=${REPLICATION_WORKER_INDEX}"
    echo "/tikv-worker $*"
    exec /tikv-worker "$@"

  rep-pd-startup-script: |
    #!/bin/sh

    ulimit -c 0
    set -uo pipefail

    ANNOTATIONS="/etc/podinfo/annotations"
    if [ -f "${ANNOTATIONS}" ]; then
      . "${ANNOTATIONS}" 2>/dev/null
    else
      echo "warning: ${ANNOTATIONS} doesn't exist, continue without pod annotations." >&2
    fi

    runmode=${runmode:-normal}
    if [ "X${runmode}" = "Xdebug" ]; then
      echo "entering debug mode."
      tail -f /dev/null
    fi

    CONTAINER_NAME=${CONTAINER_NAME:-$HOSTNAME}
    SERVICE_NAME=${REP_PD_STS_NAME}
    NAMESPACE="tidb-serverless"

    # TODO: generate certificates for rep-pd-ks1024-0.rep-pd-ks1024.tidb-serverless.svc
    # e.g. rep-pd-ks1024.tidb-serverless.svc
    domain="${SERVICE_NAME}.${NAMESPACE}.svc"

    wait_for() {
      local address=$1
      local elapseTime=0
      local period=1
      local threshold=30
      while true; do
        sleep ${period}
        elapseTime=$(( elapseTime+period ))

        if [ "${elapseTime}" -ge "${threshold}" ]; then
          echo "waiting for ${address} ready timeout" >&2
          exit 1
        fi

        if nslookup ${address} 2>/dev/null
          then
          echo "nslookup domain ${address} success"
          break
        else
          echo "nslookup domain ${address} failed" >&2
        fi
      done
    }

    wait_for ${domain}

    ARGS="--client-urls=https://0.0.0.0:2379 \
      --peer-urls=https://0.0.0.0:2380 \
      --data-dir=/data \
      --name=${CONTAINER_NAME} \
      --cacert /server-cert/ca.crt \
      --cert /server-cert/tls.crt \
      --key /server-cert/tls.key \
      --advertise-client-urls=https://${domain}:2379 \
      --advertise-peer-urls=https://${domain}:2380 \
      "

    echo "starting pd-server ..."
    sleep $((RANDOM % 10))
    echo "/pd-server ${ARGS}"
    exec /pd-server ${ARGS}

  rep-cdc-startup-script: |
    #!/bin/sh

    ulimit -c 0
    set -uo pipefail

    ANNOTATIONS="/etc/podinfo/annotations"
    if [ -f "${ANNOTATIONS}" ]; then
      . "${ANNOTATIONS}" 2>/dev/null
    else
      echo "warning: ${ANNOTATIONS} doesn't exist, continue without pod annotations." >&2
    fi

    runmode=${runmode:-normal}
    if [ "X${runmode}" = "Xdebug" ]; then
      echo "entering debug mode."
      tail -f /dev/null
    fi

    CONTAINER_NAME=${CONTAINER_NAME:-$HOSTNAME}
    SERVICE_NAME=${REP_CDC_STS_NAME}
    NAMESPACE="tidb-serverless"
    PD_SERVICE_NAME=${REP_PD_STS_NAME}

    GC_TTL=${REP_CDC_GC_TTL:-86400}

    # TODO: generate certificates for rep-cdc-ks1024-0.rep-cdc-ks1024.tidb-serverless.svc
    # e.g. rep-cdc-ks1024.tidb-serverless.svc
    domain="${SERVICE_NAME}.${NAMESPACE}.svc"
    pd_domain="${PD_SERVICE_NAME}.${NAMESPACE}.svc"
    pd_url="https://${pd_domain}:2379"

    wait_for() {
      local address=$1
      local elapseTime=0
      local period=1
      local threshold=30
      while true; do
        sleep ${period}
        elapseTime=$(( elapseTime+period ))

        if [ "${elapseTime}" -ge "${threshold}" ]; then
          echo "waiting for ${address} ready timeout" >&2
          exit 1
        fi

        if nslookup ${address} 2>/dev/null
          then
          echo "nslookup domain ${address} success"
          break
        else
          echo "nslookup domain ${address} failed" >&2
        fi
      done
    }

    wait_for ${domain}
    wait_for ${pd_domain}

    ARGS="--data-dir=/data \
      --ca=/server-cert/ca.crt \
      --cert=/server-cert/tls.crt \
      --key=/server-cert/tls.key \
      --addr=0.0.0.0:8300 \
      --advertise-addr=${domain}:8300 \
      --pd=${pd_url} \
      --gc-ttl=${GC_TTL} \
      "

    echo "starting cdc-server ..."
    sleep $((RANDOM % 10))
    echo "/cdc server ${ARGS}"
    exec /cdc server ${ARGS}
