#!/bin/bash

minio_local_path=/var/lib/docker/minio-pv/pv1
harbor_host=192.168.233.100:8033
inner_master_ip=192.168.233.100
minio_host_ip=192.168.233.100

install_minio(){
  
  docker login -u admin -p V2ryStr@ngPss ${harbor_host}

  echo "start to create minio local path !"
  mkdir -p  ${minio_local_path}
  chmod -R 777 ${minio_local_path}
  mkdir -p /root/wdd/install/

cat > /root/wdd/install/minio-docker-compose.yaml <<EOF
version: '2'
services:
  minio:
    image: ${harbor_host}/cmii/minio:RELEASE.2023-06-02T23-17-26Z
    container_name: live-minio
    ports:
      - "9000:9000"
      - "9001:9001"
    command: 'server /data --console-address ":9001"'
    restart: always
    environment:
      MINIO_ACCESS_KEY: cmii
      MINIO_SECRET_KEY: B#923fC7mk
    volumes:
      -  ${minio_local_path}:/data
EOF

  echo "start minio container !"
  docker-compose -f /root/wdd/install/minio-docker-compose.yaml up -d
  echo ""

}

install_docker_compose(){
  curl https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/amd/docker-compose-amd64 -o /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
}

init_minio(){
  echo "start to download mc!"
  if [[ !  -f  /usr/local/bin/mc ]]; then
      curl https://oss.demo.uavcmlc.com/cmlc-installation/downloadfile/amd/mc -o /usr/local/bin/mc
      chmod +x /usr/local/bin/mc
  fi

#  curl https://dl.min.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc

  echo ""
  sleep 5

  export tenant_name=outside
  mc alias set ${tenant_name} http://${minio_host_ip}:9000 cmii B#923fC7mk

  mc mb ${tenant_name}/jadenq ${tenant_name}/tus ${tenant_name}/thumbnail ${tenant_name}/pub-cms ${tenant_name}/live-srs-hls/ ${tenant_name}/mission/ ${tenant_name}/surveillance ${tenant_name}/playback ${tenant_name}/tower ${tenant_name}/modelprocess ${tenant_name}/srs-hls  ${tenant_name}/live-cluster-hls ${tenant_name}/geodata
  echo ""

  echo "set rabbit mq"
  mc admin config set ${tenant_name} notify_amqp:1 delivery_mode="2" exchange_type="direct" no_wait="off" queue_dir="" queue_limit="0"  url="amqp://admin:nYcRN91r._hj@${inner_master_ip}:35672" auto_deleted="off" durable="on" exchange="cmii.chinamobile.minio.event" internal="off" mandatory="off" routing_key="cmii.chinamobile.material.warehouse"
  echo ""

  echo "sleep 5 s!"
  sleep 5

  mc admin service restart ${tenant_name}

  echo "sleep 5 s!"
  sleep 5
  echo ""


  echo "start to add event notification !"

  mc event add ${tenant_name}/mission       arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/modelprocess  arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/live-srs-hls  arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/playback      arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/live-cluster-hls   arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/geodata   arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/surveillance  arn:minio:sqs::1:amqp  --event put

  mc event add ${tenant_name}/tus           arn:minio:sqs::1:amqp  --event delete

  mc ilm add --expiry-days "1" ${tenant_name}/tus

  echo ""
  echo "done of init !"

}

# install_docker_compose

#install_minio
#
#if [[ $(docker inspect -f '{{.State.Running}}' install-minio1-1) -eq "true" ]]; then
#      echo "minio is running now! start to init minio!"
#      init_minio
#fi

init_minio


