#!/bin/bash
set -x

SCRATCH_DIR="/home/fascam/.config/Fastec/scratch"

# Routine to compare version numbers in the form "x.y.z"
vercomp () {
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)

    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done

    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}

# We need to change the boot command line to make sure the intel_iommu is turned off.
update_grub() {
    # Make sure there is not already an intel_iommu entry on the command line.
    if ! grep -q intel_iommu /etc/default/grub; then
	# Log that we are making the change.
	/usr/bin/logger "GRUB modification: intel_iommu=off iommu=soft"

	# There is no intel_iommu, but we can safely (?) assume there is a "quiet"
	# so use that to anchor our change to turn the intel_iommu off and turn
	# on the software iommu.
	sed -i 's/quiet/quiet intel_iommu=off iommu=soft/g' /etc/default/grub

	# We need to rebuild the grub configuration file.
	grub-mkconfig -o /boot/grub/grub.cfg
    fi

    # Make sure we haven't already adjusted the memory.
    if grep -q 'mem=8192M' /etc/default/grub; then
	# Give Linux more of the memory
	/usr/bin/logger "GRUB modification: increase memory to 12G"
	sed -i 's/mem=8192M/mem=12288M/g' /etc/default/grub

	# We need to rebuild the grub configuration file.
	grub-mkconfig -o /boot/grub/grub.cfg
    fi
}

# There is a conflict with the yay package and the latest version of pacman.
# We need to clear out the old yay, update pacman, then re-install yay.
update_pacman() {
    /usr/bin/logger "Update pacman/yay"
    pacman -R yay-bin --noconfirm 
    pacman -Sy pacman --noconfirm --needed 

    # Make sure we have a clean folder to work in.
    [ -d /tmp/yay ] && rm -rf /tmp/yay
    mkdir /tmp/yay
    chmod a+w /tmp/yay

    cd /tmp

    # Download the yay-bin package
    wget https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=yay-bin
    mv PKGBUILD\?h\=yay-bin /tmp/yay/PKGBUILD
    cd /tmp/yay

    # Build and install yay.
    sudo -u fascam makepkg -i /tmp/yay --noconfirm

    # Cleanup after ourselves.
    [ -d /tmp/yay ] && rm -rf /tmp/yay
}

# Starting the pre-update process.
/usr/bin/logger "Pre-update script executing"

# Make sure the keyring is up to date.
/usr/bin/logger "Update ArchLinux key-ring"
pacman -Sy --noconfirm --needed archlinux-keyring

# We are only interested in the "x.y.z" format.
PVER=`pacman -Qi pacman | grep Version | cut -d':' -f2 | cut -d'-' -f1`

# Is the pacman installed on this system at a safe version?
# A result of '2' means that the pacman installed on this system
# is less than 6.0.0, which is the "safe" version.
vercomp $PVER "6.0.0"
CRESULT=$?
if [ $CRESULT -eq 2 ]; then
    update_pacman
fi

/usr/bin/logger "Remove unneeded packages"
# Make sure the "perf" package is uninstalled.
if pacman -Qi perf &> /dev/null; then
    pacman -Rd --noconfirm perf
fi
# ceph is sometimes installed on the older systems.
if pacman -Qi ceph-libs &> /dev/null; then
    sudo -u fascam /usr/bin/yay -R --noconfirm ceph-libs
fi
# qt5-webkit is sometimes installed.
if pacman -Qi qt5-webkit &> /dev/null; then
    sudo -u fascam /usr/bin/yay -R --noconfirm qt5-webkit
fi
# The clementine package is no longer needed.
if pacman -Qi clementine &> /dev/null; then
    sudo -u fascam /usr/bin/yay -R --noconfirm clementine
    pacman -Rdd --noconfirm clementine
fi
# The amtk package is no longer needed.
if pacman -Qi amtk &> /dev/null; then
    pacman -Rdd --noconfirm amtk
fi
# The jre-openjdk-headless package is no longer needed.
if pacman jre-openjdk-headless -Qi &> /dev/null; then
    pacman -Rdd --noconfirm jre-openjdk-headless
fi

# Update base-devel package.
/usr/bin/logger "Updating base-devel"
pacman -Sy --noconfirm base-devel

# Grab needed packages.
/usr/bin/logger "Installing third party packages"
pacman -S --noconfirm --needed minizip mimalloc
pacman -S --noconfirm --needed lsof
pacman -Sd --noconfirm --needed vtk ffmpeg4.4

# Check for either hdf5 or hdf5-openmpi
if ! pacman -Qi hdf5 &> /dev/null; then
    # Not installed and we prefer the openmpi version.
    pacman -S --noconfirm --needed hdf5-openmpi
fi

# We cannot run yay as root. Use sudo to change user to 'fascam'.
sudo -u fascam /usr/bin/yay -S libhugetlbfs --noconfirm --needed

# We may need Asian fonts.
/usr/bin/logger "Make sure we have our fonts"
pacman -S --noconfirm --needed adobe-source-han-sans-cn-fonts adobe-source-han-sans-tw-fonts adobe-source-han-sans-kr-fonts adobe-source-han-sans-jp-fonts
pacman -S --noconfirm --needed adobe-source-han-serif-cn-fonts adobe-source-han-serif-tw-fonts adobe-source-han-serif-kr-fonts adobe-source-han-serif-jp-fonts

#=============================================
# Rev3.3.0 update 
#=============================================

# Check to see if we've already created the sysctl file.
if [ ! -e /etc/sysctl.d/40-hugepage.conf ]; then
    /usr/bin/logger "Create sysctl file"
    echo "vm.nr_overcommit_hugepages=400" > /tmp/40-hugepage.conf
    mv /tmp/40-hugepage.conf /etc/sysctl.d/.
fi

# Update GRUB if needed.
update_grub

# Add in Audacity.
pacman -S --noconfirm --needed audacity

# Post-update will clean things up.
/usr/bin/logger "Pre-update script finished"
