#!/bin/sh

VERSION=`/bin/getydata -v`

#try to deinstall as many RPMs as possible to get close to a JeOS image in RPM set. For JeOS images this should lead to no changes.
#reprio this script with reduced prio
/opt/webservice/scripts/re_prio.sh $$

. /etc/profile

#just uninstall X server
rpm -q xorg-x11-server && {
  echo "`/bin/date` ASYAPSPREPOST: Uninstalling X Server ..."
  #make backup first of X server
  /opt/webservice/scripts/generic_sysbackup -p xorg-x11-server
  mkdir -p /var/opt/bkp/xserver
  mv /var/opt/sysbackup/xorg-x11-server*rpm /var/opt/bkp/xserver/.
  #now really uninstall X server
  #OSFOURK-19985 V10R1.21.0 - Assistant restart during HF activation
  #skip going to runlevel 3 as with sles15 it does not make sense anymore
  #/sbin/init 3
  sleep 10
  rpm -e --nodeps xorg-x11-server
  ret=$?
  echo "`/bin/date` ASYAPSPREPOST: Uninstalling X Server returned $ret."
  rpm -q xorg-x11-server && echo "`/bin/date` ASYAPSPREPOST: ERROR: X Server is still installed."
}

###  #more correct solution would be to move this to the ASyaps postinstall scriptlet
###  #workaround: use WD at least once before the code from below as it would lead to problems otherwise
###  /usr/sbin/wd_keepalive -1
###  sleep 1
###  wd_keepalive -1
###  sleep 1
###  
###  echo "`/bin/date` ASYAPSPREPOST: Fixing WD probelms. Initrd config before fix:"
###  grep INITRD /etc/sysconfig/kernel
###  
###  WDT_RPM=softdog_wdt-base
###  rpm -q --scripts $WDT_RPM | head -n $((`rpm -q --scripts $WDT_RPM | grep -n "postuninstall scriptlet" | cut -f1 -d:` - 1)) | grep -v "^postinstall scriptlet" > /tmp/scriptlet.sh
###  source /tmp/scriptlet.sh
###  WDT_RPM=sch311x_wdt-base
###  rpm -q --scripts $WDT_RPM | head -n $((`rpm -q --scripts $WDT_RPM | grep -n "postuninstall scriptlet" | cut -f1 -d:` - 1)) | grep -v "^postinstall scriptlet" > /tmp/scriptlet.sh
###  source /tmp/scriptlet.sh
###  WDT_RPM=dscxl2_wdt-base
###  rpm -q --scripts $WDT_RPM | head -n $((`rpm -q --scripts $WDT_RPM | grep -n "postuninstall scriptlet" | cut -f1 -d:` - 1)) | grep -v "^postinstall scriptlet" > /tmp/scriptlet.sh
###  source /tmp/scriptlet.sh
###  
###  echo "`/bin/date` ASYAPSPREPOST: Initrd config after fix:"

echo "`/bin/date` ASYAPSPREPOST: Initrd config:"
grep INITRD /etc/sysconfig/kernel

## We do not need it and it disturbs the workarounds done for new openssl 1.0.2 to work in combination with CLA
#echo "`/bin/date` ASYAPSPREPOST: Calling set_cla_callback.sh ..."
#/opt/yaps/script/set_cla_callback.sh
#ret=$?
#echo "`/bin/date` ASYAPSPREPOST: set_cla_callback.sh returned $ret"

#refix uname command if in RAR-NUC case with PLT HF for standalone SG
[ -e /bin/preplthf-uname ] && {
	rm /bin/uname
	mv /bin/preplthf-uname /bin/uname
}

echo "`/bin/date` ASYAPSPREPOST: Pre-Postinstall actions finished"

exit 0
