#!/bin/bash
docker_path="/data/aix-docker"
cd /data
print_error() {
    RED='\033[0;31m'
    NC='\033[0m'
    echo -e "${RED}[ERROR] $1${NC}" >&2
}
print_success() {
    GREEN='\033[0;32m'
    NC='\033[0m'
    echo -e "${GREEN}[SUCCESS] $1${NC}" >&2
}
query_euid=$(id -u)
if [ $query_euid -ne 0 ]; then
    print_error "Please running as root"
fi
if [ -e "/root/.aix_docker.lock" ]; then
    print_error "Initialization has been performed!"
    exit 1
fi
if ! command -v jq >/dev/null 2>&1 ; then
    if [ -f /etc/redhat-release ]; then
        yum install -y jq
    elif [ -f /etc/debian_version ]; then
        apt-get install -y jq
    else
        print_error "Unsupported Linux distribution"
        exit 1
    fi
fi
if ! command -v docker >/dev/null 2>&1 ; then
  wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-20.10.9.tgz
  if [ $? -ne 0 ]; then
    print_error "Download docker failed"
    exit 1 
  fi
  tar -zxvf docker-20.10.9.tgz
  mv docker/* /usr/bin/
  groupadd  docker
  cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
chmod +x /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl start docker
systemctl enable docker
fi

if ! command -v docker-compose >/dev/null 2>&1 ; then
  curl -L https://gh.api.99988866.xyz/https://github.com/docker/compose/releases/download/v2.18.1/docker-compose-linux-x86_64 -o  /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
fi

if ! command -v git > /dev/null 2>&1 ; then
  print_error '请安装Git'
  return 1;
fi


if [ ! -d "$docker_path" ]; then
  git clone https://opencode.jsaix.cn/chenc/aix-docker.git /data/aix-docker
  if [ $? -ne 0 ]; then
    print_error "git clone aix-docker failed"
    exit 1 
  fi
fi
cd /data/aix-docker

yaml_file=$(find "$(pwd)" -type f -name "docker-compose-*.yaml" | head -n 1)
if [ -n "$yaml_file" ]; then
  if [ -z "$AIX_DOCKER_COMPOSER_FILE" ]; then
    echo -e "export AIX_DOCKER_COMPOSER_FILE=${yaml_file}" >> ~/.bashrc
    export AIX_DOCKER_COMPOSER_FILE=$yaml_file
    source /root/.bashrc
    print_success "AIX_DOCKER_COMPOSER_FILE environment variable set to '$yaml_file'"
  else
    echo "AIX_DOCKER_COMPOSER_FILE is already set to: $AIX_DOCKER_COMPOSER_FILE"
  fi
else
  print_error "Please config docker composer startup yaml file"
  exit 1
fi
if grep -q "/data/aix-docker/.alias" ~/.bashrc; then
  print_success ".alias is already sourced in ~/.bashrc"
else
  echo "source /data/aix-docker/.alias" >> ~/.bashrc
  source /root/.bashrc
fi
touch /root/.aix_docker.lock
print_success "script init success!"