Name:           file-classify
Version:        0.0.2
Release:        1%{?dist}
Summary:        Classify the files.
License:        MulanPSL2
Source:         https://github.com/stevending1st/%{name}/archive/refs/tags/v%{version}.tar.gz

BuildRequires:  wget, dnf-plugins-core, binutils, glibc-devel, gcc-c++, openssl
Requires(pre):  wget

%description
Move files to different folders based on their extensions and types.

%global cangjie_zip_name Cangjie-0.53.13-linux.tar.gz
%global cangjie_x86_64_download_url "https://cangjie-lang.cn/v1/files/auth/downLoad?nsId=142267&fileName=Cangjie-0.53.13-linux_x64.tar.gz&objectKey=6719f1eb3af6947e3c6af327"
%global cangjie_aarch64_download_url "https://cangjie-lang.cn/v1/files/auth/downLoad?nsId=142267&fileName=Cangjie-0.53.13-linux_aarch64.tar.gz&objectKey=6719f1ec3af6947e3c6af328"

%global output_dir_name %{name}-%{version}-%{release}.%{_arch}
%global command main

%global debug_package %{nil}
%define __requires_exclude (libcangjie-runtime\.so|libsecurec\.so)

# 根据环境设置下载地址
%ifarch x86_64
# 这里的指令仅在 x86_64 架构下运行
   %global download_url %{cangjie_x86_64_download_url}
%endif

%ifarch aarch64
# 这里的指令仅在 ARM 架构下运行
   %global download_url %{cangjie_aarch64_download_url}
%endif


%prep
%autosetup -n %{name}-%{version}
cd %{_builddir}/%{name}-%{version}
bash ./create_crt_links.sh


%build
cd %{_builddir}
# 检查文件是否存在,并设置一个宏
if [ ! -f %{_builddir}/%{cangjie_zip_name} ]; then
   wget -O %{cangjie_zip_name} %{download_url}
fi
tar -xvf %{cangjie_zip_name}

cd %{_builddir}/%{buildsubdir}
source %{_builddir}/cangjie/envsetup.sh
cjpm build -o %{name}

echo "" > %{_builddir}/%{name}


%install
cd %{_buildrootdir}/%{output_dir_name}
%{__install} -p -D -m 755 %{_builddir}/%{buildsubdir}/target/release/bin/%{name} %{_buildrootdir}/%{output_dir_name}%{_bindir}/%{name}
ln -sf %{_bindir}/%{name} .%{_bindir}/%{command}
%{__install} -p -D -m 777 %{_builddir}/%{name} %{_buildrootdir}/%{output_dir_name}%{_sysconfdir}/%{name}


%clean
rm -rf %{buildroot}


%pre
if [ $1 == 1 ] ; then
  # 获取当前系统架构
  ARCH=$(uname -m)

  # 下载目录
  DOWNLOAD_CANGJIE_PATH=/tmp

  # 配置 Cangjie 语言环境
  if [ "$ARCH" == "x86_64" ]; then
    DOWNLOAD_URL=%{cangjie_x86_64_download_url}
  else
    DOWNLOAD_URL=%{cangjie_aarch64_download_url}
  fi
  if [ ! -f $DOWNLOAD_CANGJIE_PATH/%{cangjie_zip_name} ]; then
    wget -O $DOWNLOAD_CANGJIE_PATH/%{cangjie_zip_name} $DOWNLOAD_URL
  fi
  if [ ! -d $DOWNLOAD_CANGJIE_PATH/cangjie ]; then
    tar -xvf $DOWNLOAD_CANGJIE_PATH/%{cangjie_zip_name} -C $DOWNLOAD_CANGJIE_PATH
  fi

  %{__install} -d -m 755 /usr/local/lib64/%{name}/
  %{__install} -D -p -m 755 ${DOWNLOAD_CANGJIE_PATH}/cangjie/runtime/lib/linux_${ARCH}_llvm/* /usr/local/lib64/%{name}/

  echo "/usr/local/lib64/%{name}" >> /etc/ld.so.conf.d/%{name}.conf

  if [ -e /usr/lib/wsl/lib/libcuda.so.1 ]; then
    cd /usr/lib/wsl/lib
    rm libcuda.so libcuda.so.1
    ln -s libcuda.so.1.1 libcuda.so.1
  fi

  /usr/sbin/ldconfig
fi


%post
if [ $1 == 1 ] && [ -s %{_sysconfdir}/%{name} ] ; then
  if [ -z "$FILE_CLASSIFY_OUTPUT_FILE" ] ; then
    if [ -f "/var/tmp/%{name}" ]; then
      OUTPUT_PATH=$(cat /var/tmp/%{name})
    fi
  else
    OUTPUT_PATH="$FILE_CLASSIFY_OUTPUT_FILE"
  fi

  if [ -n "$OUTPUT_PATH" ]; then
    echo "$OUTPUT_PATH" > %{_sysconfdir}/%{name}
  fi
fi


%postun
if [ $1 == 0 ] ; then
  rm -rf /usr/local/lib64/%{name}/ 2>/dev/null ||:
  rm -f /etc/ld.so.conf.d/%{name}.conf 2>/dev/null ||:
  /usr/sbin/ldconfig
fi


%files
# %license add-license-file-here
# %doc add-docs-here
%defattr(-,root,root,-)
%{_bindir}/%{name}
%{_bindir}/%{command}
%config(noreplace) %{_sysconfdir}/%{name}


%changelog
* Sun Feb 23 2025 stevending1st <stevending1st@163.com> - 0.0.1-1
- Initial version.