#!/bin/bash
ROOT_DIR=$(cd $(dirname $0);pwd)
if [ ! -d /opt/actions-runner ];then
    mkdir /opt/actions-runner
else
    rm -rf /opt/actions-runner/*
fi

cd /opt/actions-runner
tar zxf /opt/thirdparty/source/actions-runner-linux-arm64-*.tar.gz -C .

egrep "^actions" /etc/group &>/dev/null
if [ $? -ne 0 ];then
    groupadd actions
fi

egrep "^actions" /etc/passwd &>/dev/null
if [ $? -ne 0 ];then
    useradd -g actions -s /bin/bash actions
fi

osName=$(cat /etc/os-release|sed -n "/^NAME/p"|awk -F'"' '{print $2}')
command -v expect &>/dev/null
if [ $? -ne 0 ];then
    if [ "${osName,,}" == "ubuntu" ];then
        apt update
        apt install -y expect
    else
        yum install -y expect
    fi
fi

hostName=$(cat /etc/hostname)

if [ -n "$2" ]; then
    labels="$2"
else
    labels="${osName,,}"
fi

expect <<END
set timeout 10
spawn su actions -c "./config.sh --url https://github.com/zxk114/modelbox --token $1"

expect "press Enter for Default"
send "\n"

expect "press Enter for $hostName"
send "123-$hostName\n"

expect "following labels"
send "${labels}\n"

expect "press Enter to skip"
send "\n"
expect eof
END

bash /opt/actions-runner/svc.sh install root
bash /opt/actions-runner/svc.sh start

git config --global http.sslVerify false
git config --global http.postBuffer 1048576000
