#!/bin/bash
set -e

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m'

curl -s https://file.winsnip.xyz/file/uploads/Logo-winsip.sh | bash
echo "Starting Auto Install Nodes Glacier"
sleep 5

# Memeriksa apakah Docker terinstal
if ! command -v docker &> /dev/null; then
    echo -e "${RED}Docker tidak terinstal. Menginstal Docker...${NC}"
    
    # Memperbarui paket dan menginstal dependensi
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

    # Menambahkan GPG key resmi Docker
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    # Menambahkan repository Docker
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

    # Memperbarui paket dan menginstal Docker
    sudo apt-get update
    sudo apt-get install -y docker-ce

    # Memeriksa apakah instalasi Docker berhasil
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}Gagal menginstal Docker. Silakan periksa log untuk detail lebih lanjut.${NC}"
        exit 1
    fi
    echo -e "${GREEN}Docker berhasil diinstal.${NC}"
fi

# Meminta input PRIVATE_KEY
read -p "Masukkan PRIVATE_KEY Anda: " PRIVATE_KEY

# Memeriksa apakah container sudah ada
if [ "$(docker ps -aq -f name=glacier-verifier)" ]; then
    echo -e "${YELLOW}Container 'glacier-verifier' sudah ada. Melewati pembuatan container baru.${NC}"
else
    # Menjalankan Docker container
    echo -e "${GREEN}Menjalankan Docker container 'glacier-verifier'...${NC}"
    docker run -d -e PRIVATE_KEY="$PRIVATE_KEY" --name glacier-verifier docker.io/glaciernetwork/glacier-verifier:v0.0.1
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}Container 'glacier-verifier' berhasil dijalankan.${NC}"
    else
        echo -e "${RED}Gagal menjalankan container 'glacier-verifier'.${NC}"
    fi
fi
