#!/bin/bash
#This script should be used to update from R8.1 to R8.2 ONLY
#

#touch /bin/this_should_stop_it

echo ""
echo "About to update the OS to LinHES 8.2"
echo "This process will only work if currently running LinHES 8.1 or later"
echo "*  Please respond with Y to ALL the yes/no questions"
echo "*  Answer Y to remove sysvinit"
echo "*  If prompted about libgl, select the gl that is appropiate for your system"
echo ""
echo "Press Y to continue, any other key to stop"


read a
if [ x$a != "xY" ]
then
    echo " Y NOT selected, exiting"
    exit 3
fi

echo ""
echo "Syncing pacman repos..."
pacman -Ssyy LinHES-system | awk '{print $2}' | grep -q "8.2-"
pr=$?
if [ $pr != 0 ]
then
    echo ""
    echo "WARNING! The current repos in /etc/pacman.conf do not look like they"
    echo "         contain packages for LinHES 8.2."
    echo "The update cannot continue. Exiting."
    exit 3
fi

echo ""
echo "Downloading filesystem and glibc for later use, please wait..."
curl -o filesystem-itermediate.pkg.tar.xz http://linhes.org/repo/x86_64/filesystem-itermediate.pkg.tar.xz
rc=$?
if [ $rc != 0 ]
then
    echo "Couldn't download filesystem package"
    exit 1
else
    echo "filesystem package downloaded"
fi

curl -o glibc-2.18-11-x86_64.pkg.tar.xz http://linhes.org/repo/x86_64/core/glibc-2.18-11-x86_64.pkg.tar.xz
rc=$?
if [ $rc != 0 ]
then
    echo "Couldn't download glibc package"
    exit 1
else
    echo "glibc package downloaded"
fi

echo "Installing tdb..."
pacman -S --noconfirm tdb
rc=$?
if [ $rc != 0 ]
then
  echo "FAILED: pacman -S tdb"
  exit 2
fi

echo ""
echo "Removing miscellanous files not in packages..."
rm -f /usr/lib/libtalloc.so.1
rm -f /usr/lib/libtalloc.so.2
rm -f /etc/ssl/certs/java/cacerts

echo ""
echo "Removing symlinks not in poweroff-scripts package..."
if [ -L /usr/sbin ]
then
    rm -f /usr/sbin/reb*
    rm -f /usr/sbin/pow*
    rm -f /usr/sbin/hal*
fi

if [ -L /sbin ]
then
    rm -f /sbin/reb*
    rm -f /sbin/pow*
    rm -f /sbin/hal*
fi


echo ""
echo "Updating packages..."
pacman -Syu lirc lirc-utils --ignore filesystem,bash,glibc
rc=$?
if [ $rc != 0 ]
then
  echo "FAILED: pacman -Syu lirc lirc-utils --ignore filesystem,bash,glibc"
  exit 2
fi

pacman -R tcp_wrappers  --noconfirm
pacman -S bash --noconfirm
rc=$?
if [ $rc != 0 ]
then
  echo "FAILED: pacman -S bash"
  exit 2
fi

pacman -U ./glibc-2.18-11-x86_64.pkg.tar.xz ./filesystem-itermediate.pkg.tar.xz --noconfirm
rc=$?
if [ $rc != 0 ]
then
  echo "Filesystem-itermediate update was unable to install"
  echo "Please check /bin /usr/sbin /sbin "
  echo "for packages that need to be updated"
  exit 2
fi

pacman -Su --noconfirm
rc=$?
if [ $rc != 0 ]
then
  echo "FAILED: pacman -Su"
  exit 2
fi

if [ -e /etc/udev/rules.d/80-net-name-slot.rules ]
then
  rm -f /etc/udev/rules.d/80-net-name-slot.rules
fi

ln -s /dev/null /etc/udev/rules.d/80-net-name-slot.rules

pacman -S linux --noconfirm

pacman -S filesystem --noconfirm
rc=$?
if [ $rc != 0 ]
then
  echo "Filesystem update was unable to install"
  echo "Please check /bin /usr/sbin /sbin "
  echo "for packages that need to be updated"
  exit 2
else
  echo ""
  echo ""
  echo "New filesystem in place"
  echo "LinHES 8.2 upgrade was successful!"
  echo "Please reboot"
fi