#!/bin/bash

function acce_check()
{
  local IFS='.'
  read ver01 ver02 ver03 ver04 <<<"$1"
  sum01=$[$ver01*2**32]
  sum02=$[$ver02*2**16]
  sum03=$[$ver03*2**8]
  sum04=$[$ver04*2**0]
  sum=$[$sum01+$sum02+$sum03+$sum04]
  [ "$sum" -gt '12885627914' ] && echo "1" || echo "0"
}


Eth=`ip addr show|grep 'BROADCAST.* UP'|awk -F ':' {'print $2'}|sed -n '1p'|sed 's/[[:space:]]//g'`

[ -f /etc/redhat-release ] && KNA=$(awk '{print $1}' /etc/redhat-release)
[ -f /etc/os-release ] && KNA=$(awk -F'[= "]' '/PRETTY_NAME/{print $3}' /etc/os-release)
[ -f /etc/lsb-release ] && KNA=$(awk -F'[="]+' '/DISTRIB_ID/{print $2}' /etc/lsb-release)

KNK=$(uname -r)
KNB=$(getconf LONG_BIT)
Mac=$(cat /sys/class/net/${Eth}/address)
URLKernel="https://raw.githubusercontent.com/uxh/serverSpeeder_kernel/master/serverSpeeder.txt"
AcceData=$(wget --no-check-certificate -qO- "$URLKernel")
AcceVer=$(echo "$AcceData" |grep "$KNA/" |grep "/x$KNB/" |grep "/$KNK/" |awk -F'/' '{print $NF}' |sort -nk 2 -t '_' |tail -n1)
MyKernel=$(echo "$AcceData" |grep "$KNA/" |grep "/x$KNB/" |grep "/$KNK/" |grep "$AcceVer" |tail -n1)
echo "Kernel: ${KNK}"
[ -z "$MyKernel" ] && echo -ne "Kernel not be matched! \nYou should change kernel manually, and try again! \n\nView the link to get details: \n"$URLKernel" \n\n\n" && exit 1
echo "Matched Kernel: ${MyKernel}"


KNN=$(echo "$MyKernel" |awk -F '/' '{ print $2 }')
KNV=$(echo "$MyKernel" |awk -F '/' '{ print $5 }')

AcceBin="acce-"$KNV"-["$KNA"_"$KNN"_"$KNK"]"

acce_ver=$(acce_check ${KNV})
ApxKeyGenUrl1="https://api.52bwg.com/lotserver/keygen.php"
ApxKeyGenUrl2="https://apx.irsb.xyz/keygen.php"

ApxLicSaveTo="/tmp/apx.lic"
wget --no-check-certificate -qO "${ApxLicSaveTo}" "${ApxKeyGenUrl1}?ver=${acce_ver}&mac=${Mac}"
echo "Save to ${ApxLicSaveTo}"
