#!/usr/bin/env bash
#
# Description: Get hardware info for AppUnlocker.exe
# based on the script from https://teddysun.com/444.html

clear
export myMail="ask@hpc4you.top"
export weChatID="hpc4you"
export currentTime=$(date "+%Y%m%d-%H%M")

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PLAIN='\033[0m'

getHardwareInfo() {
get_opsy() {
    [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
    [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
    [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}


dashLine() {
    printf "%-70s\n" "-" | sed 's/\s/-/g'
}

machineID=$(cat /etc/machine-id)
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
tram=$( free -g | awk '/Mem/ {print $2}' )

opsy=$( get_opsy )
arch=$( uname -m )
lbit=$( getconf LONG_BIT )
kern=$( uname -r )

dashLine
echo -e "CPU\t$cname"
echo -e "Cores\t$cores"
echo -e "Memory\t$tram GB"
echo -e "OS\t$opsy"
echo -e "Arch\t$arch ($lbit Bit)"
echo -e "Kernel\t$kern"

echo -e "ID\t${machineID}"
dashLine
echo ""
echo ""
echo -e "Disk Info:"
dashLine

if ! command -v lsblk &> /dev/null
then
    if [[ -f /etc/redhat-release ]]; then
        echo "lsblk not found. Installing on RHEL..."
        yum install util-linux -y
    elif [[ -f /etc/lsb-release ]]; then
        echo "lsblk not found. Installing on Ubuntu..."
        apt-get update
        apt-get install util-linux -y
    else
        echo "Unknown distribution. Cannot install lsblk."
        echo "Please install lsblk manually and try again."
        exit 1
    fi
fi

for disk in `lsblk | grep ^sd | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
for disk in `lsblk | grep ^nvme | awk '{print $1}'`; do echo -e "DiskID:\t`lsblk --nodeps -no serial /dev/$disk`"; done | sort | uniq
dashLine
}

rm -fr AppUnlocker-Info*.dat &> /dev/null

getHardwareInfo > AppUnlocker-Info-$$.dat
fingerPrint=`md5sum AppUnlocker-Info-$$.dat`
hardwareFile="AppUnlocker-Info-$$.dat"
serialNumber=`curl -k -s --upload-file ${hardwareFile} https://tophpc.top:8443 | awk -F '/' '{print $4}'`
echo ""
echo 'This software deployment solution is a paid service.
To verify payment eligibility, please send your hardware fingerprint (blue text) to:'
echo '"下载即用"软件部署方案需付费.为验证付费资格,请将硬件信息指纹(下文蓝色内容)发送至: '
echo ""
echo -e "${BLUE}${fingerPrint}${PLAIN}"
echo -e "${BLUE}SN: ${serialNumber}${PLAIN}"
echo ""
echo -e "- WeChat/Weixin/微信请联系: ${GREEN}${weChatID}${PLAIN} "
echo -e "- Email/电邮,请发送至: ${GREEN}${myMail}${PLAIN}"

echo ""
echo -e "To decrypt the App Image you downloaded from BaiduShare,
please run the following command on your server and send me the output:"
echo -e "从百度网盘下载的App Image文件需要解密,请在您的服务器上运行以下命令,并将输出内容发送给我."
echo -e "\t${GREEN}md5sum full_File_Name_From_BaiduShare${PLAIN}"
echo ""
echo "Good Luck."
