#!/bin/sh

CURDIR=`cd "$(dirname "$0")" && pwd`
cd "$CURDIR"

A=`id -u`

if [ "$A" != "0" ]; then
   echo "You have to be root to run this script"
   echo "Please run: sudo sh install_h2_lib.sh"
   exit 1
fi

if [ ! -d h2api ]; then
   echo "h2api directory was not found"
   echo "Please put this script in Linux/Install_H2_SDK/"
   exit 1
fi

# 获取当前系统架构
machine=`uname -m`

if [ "$machine" = "x86_64" ]; then
   arch=x86_64
elif [ "$machine" = "amd64" ]; then
   arch=x86_64
elif [ "$machine" = "aarch64" ]; then
   arch=aarch64
elif [ "$machine" = "arm64" ]; then
   arch=aarch64
elif [ "$machine" = "armv7l" ]; then
   arch=armv7
elif [ "$machine" = "armv7" ]; then
   arch=armv7
else
   echo "Unsupported architecture: $machine"
   exit 1
fi

#echo "Architecture = $arch"

# 检查当前架构的H2 API文件
if [ ! -d h2api/lib/${arch} ]; then
   echo "h2api/lib/${arch} was not found"
   exit 1
fi

if [ ! -f h2api/lib/${arch}/libgenSignalWave.so ]; then
   echo "h2api/lib/${arch}/libgenSignalWave.so was not found"
   exit 1
fi

# 获取H2 API版本号
file=`ls h2api/lib/${arch}/libh2api.so.[0-9]* 2>/dev/null | head -n 1`
file=`basename "$file"`
version=${file#*so.}
majornum=${version%%.*}

if [ -z "$version" ] || [ "$file" = "." ]; then
   echo "libh2api.so.* was not found"
   exit 1
fi

#echo "H2 API version = $version"

# 检查系统是否为glibc环境
if command -v ldd >/dev/null 2>&1; then
   ldd --version 2>&1 | grep -qi "musl"
   if [ $? -eq 0 ]; then
      echo "This SDK requires a glibc Linux system"
      echo "Alpine or other musl systems are not supported"
      exit 1
   fi
else
   echo "ldd was not found"
   exit 1
fi

# 查找系统自带的libgomp.so.1，只使用/lib或/usr/lib中的版本
systemlibgomp=""

if command -v ldconfig >/dev/null 2>&1; then
   systemlibgomp=`ldconfig -p 2>/dev/null | grep "libgomp.so.1" | awk '{print $NF}' | grep -E '^/(lib|usr/lib)' | head -n 1`
fi

if [ ! -f "$systemlibgomp" ]; then
   systemlibgomp=`find /lib /usr/lib /lib64 /usr/lib64 -name "libgomp.so.1" 2>/dev/null | head -n 1`
fi

# 如果系统没有libgomp，或者版本不支持GOMP_4.0，则只安装OpenMP运行库
need_install_libgomp=0

if [ ! -f "$systemlibgomp" ]; then
   need_install_libgomp=1
else
   LC_ALL=C grep -a -q "GOMP_4.0" "$systemlibgomp"
   if [ $? -ne 0 ]; then
      need_install_libgomp=1
   fi
fi

if [ "$need_install_libgomp" = "1" ]; then
   echo "A compatible system libgomp.so.1 was not found"
   echo "Installing only the required OpenMP runtime library"

   if command -v apt-get >/dev/null 2>&1; then
      apt-get update
      apt-get install -y libgomp1
   elif command -v dnf >/dev/null 2>&1; then
      dnf install -y libgomp
   elif command -v yum >/dev/null 2>&1; then
      yum install -y libgomp
   elif command -v zypper >/dev/null 2>&1; then
      zypper --non-interactive install libgomp1
   elif command -v pacman >/dev/null 2>&1; then
      pacman -Sy --needed --noconfirm gcc-libs
   else
      echo "No supported package manager was found"
      echo "Please install libgomp manually"
      exit 1
   fi

   systemlibgomp=""

   if command -v ldconfig >/dev/null 2>&1; then
      ldconfig
      systemlibgomp=`ldconfig -p 2>/dev/null | grep "libgomp.so.1" | awk '{print $NF}' | grep -E '^/(lib|usr/lib)' | head -n 1`
   fi

   if [ ! -f "$systemlibgomp" ]; then
      systemlibgomp=`find /lib /usr/lib /lib64 /usr/lib64 -name "libgomp.so.1" 2>/dev/null | head -n 1`
   fi
fi

if [ ! -f "$systemlibgomp" ]; then
   echo "System libgomp.so.1 was not found"
   exit 1
fi

LC_ALL=C grep -a -q "GOMP_4.0" "$systemlibgomp"
if [ $? -ne 0 ]; then
   echo "$systemlibgomp does not support GOMP_4.0"
   exit 1
fi

#echo "System libgomp = $systemlibgomp"

# 安装USB配置文件
if [ -f h2api/configs/h2usb_enum.conf ]; then
   cp h2api/configs/h2usb_enum.conf /etc/
   chmod 644 /etc/h2usb_enum.conf
fi

if [ -f h2api/configs/h2-cyusb.rules ]; then
   cp h2api/configs/h2-cyusb.rules /etc/udev/rules.d/
   chmod 644 /etc/udev/rules.d/h2-cyusb.rules
fi

# 安装H2 API
rm -rf /opt/h2api/
cp -r h2api/ /opt/

# 设置目录和文件权限
find /opt/h2api -type d -exec chmod 755 {} \;
find /opt/h2api/inc -type f -exec chmod 644 {} \; 2>/dev/null
find /opt/h2api/configs -type f -exec chmod 644 {} \; 2>/dev/null
find /opt/h2api/lib -type f -name "*.so*" -exec chmod 755 {} \;

# 创建当前架构的libh2api软连接
ln -sf /opt/h2api/lib/${arch}/libh2api.so.${version} /opt/h2api/lib/${arch}/libh2api.so.${majornum}
ln -sf /opt/h2api/lib/${arch}/libh2api.so.${majornum} /opt/h2api/lib/${arch}/libh2api.so

# 创建当前架构的libusb软连接
usbfile=`ls /opt/h2api/lib/${arch}/libusb-1.0.so.[0-9]* 2>/dev/null | grep -v '/libusb-1.0.so.0$' | head -n 1`

if [ -n "$usbfile" ]; then
   usbbase=`basename "$usbfile"`
   usbmajor=`echo "$usbbase" | sed 's/^\(libusb-1\.0\.so\.[0-9][0-9]*\).*/\1/'`

   ln -sf "$usbfile" /opt/h2api/lib/${arch}/${usbmajor}
   ln -sf /opt/h2api/lib/${arch}/${usbmajor} /opt/h2api/lib/${arch}/libusb-1.0.so
fi

# 如果存在数字解调库，则创建兼容软连接
if [ -f /opt/h2api/lib/${arch}/libDigitalSigDemod.so ]; then
   ln -sf /opt/h2api/lib/${arch}/libDigitalSigDemod.so /opt/h2api/lib/${arch}/libDigitalSigDemod.so.1
fi

# 备份安装包中不兼容的旧版libgomp，并使用系统版本
backup_time=`date +%Y%m%d%H%M%S`
mkdir -p /opt/h2api/lib/${arch}/libgomp_backup/${backup_time}

if ls /opt/h2api/lib/${arch}/libgomp.so* >/dev/null 2>&1; then
   mv /opt/h2api/lib/${arch}/libgomp.so* /opt/h2api/lib/${arch}/libgomp_backup/${backup_time}/
fi

ln -sf "$systemlibgomp" /opt/h2api/lib/${arch}/libgomp.so.1
ln -sf /opt/h2api/lib/${arch}/libgomp.so.1 /opt/h2api/lib/${arch}/libgomp.so

# 如果系统同时安装了HTRA API，避免其旧版libgomp覆盖系统版本
if [ -d /opt/htraapi/lib/${arch} ]; then
   mkdir -p /opt/htraapi/lib/${arch}/libgomp_backup/${backup_time}

   if ls /opt/htraapi/lib/${arch}/libgomp.so* >/dev/null 2>&1; then
      mv /opt/htraapi/lib/${arch}/libgomp.so* /opt/htraapi/lib/${arch}/libgomp_backup/${backup_time}/
   fi

   ln -sf "$systemlibgomp" /opt/htraapi/lib/${arch}/libgomp.so.1
   ln -sf /opt/htraapi/lib/${arch}/libgomp.so.1 /opt/htraapi/lib/${arch}/libgomp.so
fi

# 注册H2 API动态库路径
if command -v ldconfig >/dev/null 2>&1; then
   echo "/opt/h2api/lib/${arch}" > /etc/ld.so.conf.d/00-h2api.conf
   ldconfig
else
   echo "ldconfig was not found"
   exit 1
fi

# 刷新USB规则
if command -v udevadm >/dev/null 2>&1; then
   udevadm control --reload-rules 2>/dev/null
   udevadm trigger 2>/dev/null
fi

# 检查调制库依赖
missinglibs=`ldd /opt/h2api/lib/${arch}/libgenSignalWave.so 2>/dev/null | grep "not found"`

if [ -n "$missinglibs" ]; then
   echo "$missinglibs"
   echo "libgenSignalWave.so has missing dependencies"
   exit 1
fi

loadedlibgomp=`ldd /opt/h2api/lib/${arch}/libgenSignalWave.so 2>/dev/null | grep "libgomp.so.1" | awk '{print $3}' | head -n 1`

if [ ! -f "$loadedlibgomp" ]; then
   echo "libgenSignalWave.so did not load libgomp.so.1 correctly"
   exit 1
fi

LC_ALL=C grep -a -q "GOMP_4.0" "$loadedlibgomp"
if [ $? -ne 0 ]; then
   echo "$loadedlibgomp does not support GOMP_4.0"
   exit 1
fi

#echo "H2 API installation completed!"
