#!/bin/bash
set -x

init()
{
    DENYIP_HOME=~/DenyIP-go

}

main()
{
    if test "$1" = "rm"; then
        killall -15 denyip
        rm -rf $DENYIP_HOME
        exit 0
    fi

    if ! test -d $DENYIP_HOME; then
        mkdir -p $DENYIP_HOME
        #mkdir -p $DENYIP_HOME/ip2region
    fi

#    if ! test -f ~/DenyIp/ip2region/ip2region.xdb; then
#        wget -O $DENYIP_HOME/ip2region/ip2region.xdb https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/ip2region/ip2region.xdb

#        if test "$(md5sum $DENYIP_HOME/ip2region/ip2region.xdb | cut -d " " -f 1)" != "508c6b0257a02152b9d1b2b3792936e1"; then
#            echo "ip2region.xdb md5 check fail"
#            rm -rf $DENYIP_HOME
#            exit 1
#        else
#            echo "ip2region.xdb md5 check success"
#        fi
#    fi

    if ! test -f ~/DenyIp/denyip; then
        wget -O $DENYIP_HOME/denyip https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/denyip
        chmod +x $DENYIP_HOME/denyip

        if test -n "$(file $DENYIP_HOME/denyip | grep ELF)"; then
            echo "denyip install success"
        else
            echo "denyip install fail"
            rm -rf $DENYIP_HOME
            exit 1
        fi
    fi

}

init
main $@
exit 0
