#!/bin/sh

if [ x$1 != x ]
then
  version="/$1"
fi

if [ -f /etc/sysconfig/appliance-group ]
then
   group=`cat /etc/sysconfig/appliance-group`
else
   echo "Appliance group not found. Aborting..."
   exit 1
fi
echo "Unpinning kernel an kernel modules..."
conary unpin `conary q | grep kernel | grep -v group | awk -F= '{print $1}' | sort -u`

echo "Fixing some package issues..."
if [ -f /var/empty/sshd/etc ]
then 
  rm -rf /var/empty/sshd/etc 
  mkdir -p /var/empty/sshd/etc
fi
if [ -f /etc/resolv.conf.predhclient  ]
then
  rm -rf /etc/resolv.conf.predhclient 
fi
sed -ie 's/noauto,ro,owner,exec/noauto,ro,users,exec/' /etc/fstab

echo "Migrating system to new version..."
conary migrate $group=cernvm.cern.ch@cern:cernvm-2$version

echo "Fixing conary label..."
cp /etc/conaryrc /etc/conaryrc.orig
echo "installLabelPath    cernvm.cern.ch@cern:cernvm-2 cernvm.cern.ch@cern:sl-5" > /etc/conaryrc
grep -v installLabelPath /etc/conaryrc.orig >> /etc/conaryrc

echo "Removing old desktop config files..."
find /home /root -maxdepth 2 -type d -name '.config' -o -name '.cache'  -o -name '.fontconfig'  -o -name '.local' | xargs -i rm -rf {}\;

echo "Running CVMFS setup..."
cvmfs_config setup > /dev/null 2>&1

echo "Forcing  extra modules to be built on reboot if needed..."
rm -f /etc/cernvm/tools.conf

case $group in
    *-desktop)
       /etc/cernvm/config -x
       ;;
    *)
       ;;
esac

reboot


