#!/bin/bash

## Copied & renamed from src/scripts/try-v3
## Detect machine architecture, pick corresponding CachyOS optimized repositories, create /etc/pacman-target.conf
## This is supposed to run on live system w/o chroot.

# Pacman Files
pacman_conf_live=/etc/pacman.conf
pacman_conf_cachyos="/etc/pacman-more.conf"
pacman_conf_target=/etc/pacman-target.conf

# Architecture Check
check_v3="$(/lib/ld-linux-x86-64.so.2 --help | grep 'x86-64-v3 (' | awk '{print $1}')"
check_v4="$(/lib/ld-linux-x86-64.so.2 --help | grep 'x86-64-v4 (' | awk '{print $1}')"
check_znver4_znver5="$(gcc -march=native -Q --help=target 2>&1 | head -n 37 | grep -E '(znver4|znver5)')"

if [ -n "$check_znver4_znver5" ]; then
  echo "znver4 or znver5 is supported (but we only have znver4
  available)"
  sed -e 's/#<disabled_znver4>//g' \
    -e '/disabled_v[34]/d' $pacman_conf_cachyos \
    > $pacman_conf_target
elif [ "$check_v4" == "x86-64-v4" ]; then
  echo "x86-64-v4 is supported"
  sed -e 's/#<disabled_v4>//g' \
    -e '/disabled_v3/d' \
    -e '/disabled_znver4/d' $pacman_conf_cachyos \
    > $pacman_conf_target
elif [ "$check_v3" == "x86-64-v3" ]; then
  echo "x86-64-v3 is supported"
  sed -e 's/#<disabled_v3>//g' \
    -e '/disabled_znver4/d' \
    -e '/disabled_v4/d' $pacman_conf_cachyos \
    > $pacman_conf_target
else
  echo "Legacy hardware assumed"
  # Assuming that Live OS needs to run on unoptimized x86_64 repo
  # for compatibility
  cp $pacman_conf_live $pacman_conf_target
fi

echo Repo configured in /etc/pacman-target.conf
