#!/bin/bash
# wget -q -O - https://hacs.vip/get | DOMAIN=hacs REPO_PATH=hacs-china/integration bash -
# curl -fsSL get.hacs.vip | DOMAIN=hacs bash
set -e

[ -z "$DOMAIN" ] && DOMAIN="hacs"
[ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="$1"

case $DOMAIN in
    hacs)
        [ -z "$REPO_PATH" ] && REPO_PATH="hacs-china/integration"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="latest"
        ;;
    xiaomi_miot | miot)
        DOMAIN="xiaomi_miot"
        [ -z "$REPO_PATH" ] && REPO_PATH="al-one/hass-xiaomi-miot"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="latest"
        ;;
    xiaomi_home)
        [ -z "$REPO_PATH" ] && REPO_PATH="XiaoMi/ha_xiaomi_home"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="latest"
        ;;
    xiaomi_gateway3)
        [ -z "$REPO_PATH" ] && REPO_PATH="AlexxIT/XiaomiGateway3"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="master"
        ;;
    sonoff)
        [ -z "$REPO_PATH" ] && REPO_PATH="AlexxIT/SonoffLAN"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="master"
        ;;
    tianqi)
        [ -z "$REPO_PATH" ] && REPO_PATH="hasscc/tianqi"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    edge_tts)
        [ -z "$REPO_PATH" ] && REPO_PATH="hasscc/hass-edge-tts"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    yeelight_pro)
        [ -z "$REPO_PATH" ] && REPO_PATH="hasscc/yeelight-pro"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    ai_conversation)
        [ -z "$REPO_PATH" ] && REPO_PATH="hasscc/ai-conversation"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    ha_file_explorer)
        [ -z "$REPO_PATH" ] && REPO_PATH="shaonianzhentan/ha_file_explorer"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="master"
        ;;
    bemfa)
        [ -z "$REPO_PATH" ] && REPO_PATH="larry-wong/bemfa"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    midea_ac_lan)
        [ -z "$REPO_PATH" ] && REPO_PATH="wuwentao/midea_ac_lan"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="latest"
        ;;
    midea_ac)
        [ -z "$REPO_PATH" ] && REPO_PATH="mill1000/midea-ac-py"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    haier)
        [ -z "$REPO_PATH" ] && REPO_PATH="banto6/haier"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="master"
        ;;
    terncy)
        [ -z "$REPO_PATH" ] && REPO_PATH="rxwen/homeassistant-terncy-component"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="main"
        ;;
    pyscript)
        [ -z "$REPO_PATH" ] && REPO_PATH="custom-components/pyscript"
        [ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="latest"
        [ -z "$FILENAME" ] && FILENAME="hass-custom-pyscript.zip"
        ;;
esac

[ -z "$REPO_PATH" ] && REPO_PATH="hacs-china/integration"
REPO_NAME=$(basename "$REPO_PATH")

[ -z "$ARCHIVE_TAG" ] && ARCHIVE_TAG="master"
[ -z "$HUB_DOMAIN" ] && HUB_DOMAIN="ghrp2.hacs.vip"
ARCHIVE_URL="https://$HUB_DOMAIN/$REPO_PATH/archive/$ARCHIVE_TAG.zip"

if [ "$DOMAIN" = "hacs" ]; then
    if [ "$ARCHIVE_TAG" = "main" ] || [ "$ARCHIVE_TAG" = "china" ] || [ "$ARCHIVE_TAG" = "master" ]; then
        ARCHIVE_TAG="latest"
    fi
fi
if [ "$DOMAIN" = "hacs" ]; then
    ARCHIVE_URL="https://$HUB_DOMAIN/$REPO_PATH/releases/download/$ARCHIVE_TAG/$DOMAIN.zip"
fi
if [ "$ARCHIVE_TAG" = "latest" ]; then
    [ -z "$FILENAME" ] && FILENAME="$DOMAIN.zip"
    ARCHIVE_URL="https://$HUB_DOMAIN/$REPO_PATH/releases/$ARCHIVE_TAG/download/$FILENAME"
fi

RED_COLOR='\033[0;31m'
GREEN_COLOR='\033[0;32m'
GREEN_YELLOW='\033[1;33m'
NO_COLOR='\033[0m'

declare haPath
declare ccPath
declare -a paths=(
    "$PWD"
    "$PWD/config"
    "/config"
    "/homeassistant"
    "/var/lib/homeassistant/homeassistant"
    "/usr/share/hassio/homeassistant"
    "/var/lib/hassio/homeassistant"
    "/opt/hassio/homeassistant"
    "/mnt/data/supervisor/homeassistant"
    "/var/hass/config"
    "$HOME/.homeassistant"
)

function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}"; }
function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}"; }
function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; }

function checkRequirement () {
    if [ -z "$(command -v "$1")" ]; then
        error "'$1' is not installed"
    fi
}

checkRequirement "unzip"

function cget () {
    if [ -z "$(command -v "wget")" ]; then
        curl -fL -o "$1" "$2"
    else
        checkRequirement "wget"
        wget -t 2 -O "$1" "$2"
    fi
}

info "Archive URL: $ARCHIVE_URL"
info "Trying to find the correct directory..."
for path in "${paths[@]}"; do
    if [ -n "$haPath" ]; then
        break
    fi

    if [ -f "$path/home-assistant.log" ]; then
        haPath="$path"
    else
        if [ -d "$path/.storage" ] && [ -f "$path/configuration.yaml" ]; then
            haPath="$path"
        fi
    fi
done

if [ -n "$haPath" ]; then
    info "Found Home Assistant configuration directory at '$haPath'"
    cd "$haPath" || error "Could not change path to $haPath"
    ccPath="$haPath/custom_components"
    if [ ! -d "$ccPath" ]; then
        info "Creating custom_components directory..."
        mkdir "$ccPath"
    fi

    info "Changing to the custom_components directory..."
    cd "$ccPath" || error "Could not change path to $ccPath"

    info "Downloading..."
    cget "$ccPath/$ARCHIVE_TAG.zip" "$ARCHIVE_URL"

    ver=${ARCHIVE_TAG/#v/}

    info "Unpacking..."
    str="/releases/"
    if [ ${ARCHIVE_URL/${str}/} = $ARCHIVE_URL ]; then
        unzip -o "$ccPath/$ARCHIVE_TAG.zip" -d "$ccPath" >/dev/null 2>&1
    else
        dir="$ccPath/$REPO_NAME-$ver/custom_components/$DOMAIN"
        mkdir -p $dir
        unzip -o "$ccPath/$ARCHIVE_TAG.zip" -d $dir >/dev/null 2>&1
    fi
    rm -rf "$ccPath/$ARCHIVE_TAG.zip"

    if [ ! -d "$ccPath/$REPO_NAME-$ver" ]; then
        ver=$ARCHIVE_TAG
    fi
    if [ ! -d "$ccPath/$REPO_NAME-$ver" ]; then
        error "Could not find $REPO_NAME-$ver directory" false
        error "找不到文件夹: $REPO_NAME-$ver"
    fi

    if [ -d "$ccPath/$DOMAIN" ]; then
        warn "custom_components/$DOMAIN directory already exist, cleaning up..."
        rm -R "$ccPath/$DOMAIN"
    fi

    info "Copying new files..."
    cp -rf "$ccPath/$REPO_NAME-$ver/custom_components/$DOMAIN" "$ccPath"

    info "Removing temp files..."
    rm -rf "$ccPath/$REPO_NAME-$ver"
    info "Installation complete."
    info "安装成功！"
    echo
    info "Remember to restart Home Assistant before you configure it."
    info "请重启 Home Assistant"

else
    echo
    error "Could not find the directory for Home Assistant" false
    error "找不到 Home Assistant 根目录" false
    echo "Manually change the directory to the root of your Home Assistant configuration"
    echo "With the user that is running Home Assistant and run the script again"
    echo "请手动进入 Home Assistant 根目录后再次执行此脚本"
    exit 1
fi
