# Calculate exec=/bin/bash ac_install_disk==off&&ac_install_unmerge==off&&os_install_root_type!=livecd cl_setup!=themes||cl_splash_image_update_set==on
#!/bin/bash

# the recreating of inird is performed for binary kernel only (mark .calculate in kernel sources)

version_sort ()
{
  case $version_sort_sort_has_v in
    yes)
      LC_ALL=C sort -V;;
    no)
      LC_ALL=C sort -n;;
    *)
      if sort -V </dev/null > /dev/null 2>&1; then
        version_sort_sort_has_v=yes
	LC_ALL=C sort -V
      else
        version_sort_sort_has_v=no
        LC_ALL=C sort -n
      fi;;
   esac
}

version_test_numeric ()
{
  version_test_numeric_a="$1"
  version_test_numeric_cmp="$2"
  version_test_numeric_b="$3"
  if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
    case "$version_test_numeric_cmp" in
      ge|eq|le) return 0 ;;
      gt|lt) return 1 ;;
    esac
  fi
  if [ "$version_test_numeric_cmp" = "lt" ] ; then
    version_test_numeric_c="$version_test_numeric_a"
    version_test_numeric_a="$version_test_numeric_b"
    version_test_numeric_b="$version_test_numeric_c"
  fi
  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
    return 0
  else
    return 1
  fi
}

version_test_gt ()
{
  version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//"`"
  version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//"`"
  version_test_gt_cmp=gt
  if [ "x$version_test_gt_b" = "x" ] ; then
    return 0
  fi
  case "$version_test_gt_a:$version_test_gt_b" in
    *.old:*.old) ;;
    *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
    *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
  esac
  version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
  return "$?"
}

version_find_latest ()
{
  version_find_latest_a=""
  for i in "$@" ; do
    if version_test_gt "$i" "$version_find_latest_a" ; then
      version_find_latest_a="$i"
    fi
  done
  echo "$version_find_latest_a"
}

get_latest_kernel() {
    version_find_latest /boot/vmlinuz-*
}

get_safemode_name() {
    local kernel=$1
    #echo ${kernel/-calculate/-SafeMode}
    [[ $kernel =~ ^([^-]+-[^-]+)-(.+)$ ]] && echo "${BASH_REMATCH[1]}-SafeMode-${BASH_REMATCH[2]}" || echo ${kernel}
}

get_kernel_version() {
    local kernel=$1
    file -L $kernel | sed -r 's/.*version (\S+)\s+.*/\1/'
}

get_kernel_initramfs() {
    local kernel=$1
    echo ${kernel/vmlinuz-/initramfs-}.img
}

is_calculate_kernel() {
    local kernel=$1
    local kernel_version=$(get_kernel_version $kernel)
    [[ -f /usr/src/linux-${kernel_version}/.calculate ]]
}

die() {
    echo $@ 1>&2
    exit 1
}

create_hostonly_initramfs() {
    local kernel_safemode=$(get_safemode_name $kernel)
    local initrd=$(get_kernel_initramfs $kernel)
    local initrd_tmp=$(get_kernel_initramfs $kernel).tmp
    local initrd_safemode=$(get_safemode_name $initrd)

    [[ -e $kernel_safemode ]] || (rm -f $kernel_safemode;ln -s $kernel $kernel_safemode &>/dev/null) || 
        die "Failed to create kernel for safemode"

    if ! [[ -e $initrd_safemode ]]
    then
        cp $initrd $initrd_safemode &>/dev/null || die "Failed to create initramfs safemode backup"
    fi
    if [[ -e $initrd_safemode ]]
    then
        local kernel_version=$(get_kernel_version $kernel)
        [[ -n $kernel_version ]] || die "Failed to detect version"
        echo "Creating Host-Only initramfs ..."
        local kernelconfig="/boot/config-$kernel_version"
        local compresstype=""
        if grep -q CONFIG_RD_ZSTD=y $kernelconfig &>/dev/null
        then
            compresstype="--zstd"
        elif grep -q CONFIG_RD_GZIP=y $kernelconfig &>/dev/null
        then
            compresstype="--gzip"
        elif grep -q CONFIG_RD_XZ=y $kernelconfig &>/dev/null
        then
            compresstype="--xz"
        else
            die "Failed to detect initramfs support compression"
        fi
#?os_install_x11_video_drv==#-os_x11_video_drv-##
        dracut -H $compresstype -f --kver $kernel_version $initrd_tmp &>/dev/null ||
            die "Failed to create Host-Only initramfs"
#os_install_x11_video_drv#
#?os_install_x11_video_drv!=#-os_x11_video_drv-##
        dracut -H $compresstype -f --add-drivers="i915 amdgpu uvesafb radeon nouveau" --kver $kernel_version $initrd_tmp &>/dev/null ||
            die "Failed to create Host-Only initramfs"
#os_install_x11_video_drv#
        mv $initrd_tmp $initrd &>/dev/null || die "Failed to rename initramfs"
        echo "Host-Only initramfs has been created"
    fi
}

kernel=$(get_latest_kernel)

if [[ -e $kernel ]] && [[ -e $(get_kernel_initramfs $kernel) ]]
then
    is_calculate_kernel $kernel && create_hostonly_initramfs $kernel
else
    echo "Creating Host-Only initramfs was skipped"
fi

true
