#!/bin/bash
#$Id: upgrade-32.sh,v 1.26 2006/06/06 15:14:04 bzizou Exp $
# SLIS 3.1 to SLIS 3.2 upgrade script
######################################

. /etc/slis/slis.conf

# Update level check
echo "Update level check..."
logger -t "update-3.2" "Update level check..."
declare -i REL
REL=`rpm -q --qf "%{RELEASE}" slis-up2date`
if [ "$REL" -lt "31" ]
then
  echo "ERROR: This slis 3.1 is not up-to-date. I can't upgrade. Aborting."
  logger -t "update-3.2" "ERROR: This slis 3.1 is not up-to-date. I can't upgrade. Aborting."
  exit 3
fi


# Clean temporary directories
echo "Cleaning..."
logger -t "update-3.2" "Cleaning..."
/usr/sbin/tmpwatch 24 /tmp /var/tmp
/sbin/service runcache stop
killall RunCache 2>/dev/null
killall squid 2>/dev/null
sleep 5
/bin/rm -rf /www-cache/*

# Removing docs
echo "Removing docs to make some space..."
logger -t "update-3.2" "Removing docs..."
rm -rf /usr/share/docs/*

# Space disk checks
echo "Disk space check..."
logger -t "update-3.2" "Disk space check..."
declare -i SPACE
SPACE=`df -P | grep /tmp$ | awk '{print $4}'`
if [ "$SPACE" -lt "10000" ]
then
  logger -t "update-3.2" "ERR: not enough free space into /tmp. ABORTING."
  echo "ERR: not enough free space into /tmp. ABORTING."
  /sbin/service squid start
  exit 1
fi
SPACE=`df -P | grep /var$ | awk '{print $4}'`
if [ "$SPACE" -lt "100000" ]
then
  logger -t "update-3.2" "ERR: not enough free space into /var. ABORTING."
  echo "ERR: not enough free space into /var. ABORTING."
  /sbin/service squid start
  exit 1
fi
SPACE=`df -P | grep /$ | awk '{print $4}'`
if [ "$SPACE" -lt "100000" ]
then
  logger -t "update-3.2" "ERR: not enough free space into /. ABORTING."
  echo "ERR: not enough free space into /. ABORTING."
  /sbin/service squid start
  exit 1
fi
SPACE=`df -P | grep /home$ | awk '{print $4}'`
if [ "$SPACE" -lt "10000" ]
then
  logger -t "update-3.2" "ERR: not enough free space into /home. ABORTING."
  echo "ERR: not enough free space into /home. ABORTING."
  /sbin/service squid start
  exit 1
fi
SPACE=`df -P | grep /www-cache$ | awk '{print $4}'`
if [ "$SPACE" -lt "400000" ]
then
  logger -t "update-3.2" "ERR: not enough free space into /www-cache. ABORTING."
  echo "ERR: not enough free space into /www-cache. ABORTING."
  /sbin/service squid start
  exit 1
fi

# We will use the squid-cache partiton as a temporary space
echo "Moving the cache dir..."
logger -t "update-3.2" "Moving the cache dir..."
/bin/rm -rf /www-cache/*
/bin/mv /var/cache /www-cache/
/bin/ln -s /www-cache/cache /var/cache

# Dumping postgres
echo "PG dump..."
logger -t "update-3.2" "PG dump..."
export PGUSER=`grep "POSTGRESUSER" /home/hadmin/html/config_pgsql.inc.php | cut -d\" -f2`
export PGPASSWORD=`grep "POSTGRESPASS" /home/hadmin/html/config_pgsql.inc.php | cut -d\" -f2`
psql -tqc "delete from access_squid" hadmin
psql -tqc "delete from access_apache" hadmin
pg_dumpall -D > /var/cache/dump.sql
if [ $? != 0 ]
then
  logger -t "update-3.2" "ERROR: PG dump failed! Reverting to the initial state."
  echo "ERROR: PG dump failed! Reverting to the initial state."
  /bin/rm -f /var/cache/dump.sql
  /bin/rm -f /var/cache
  /bin/mv -f /www-cache/cache /var/
  /sbin/service squid start
  exit 2
fi

# Removing slis-up2date
echo "Removing slis-up2date..."
apt-get remove -q2 slis-up2date

# Changing the APT repository
echo "APT update..."
logger -t "update-3.2" "APT update..."
> /etc/apt/sources.list
#NE PAS UTILISER LE CDROM : le kernel n'est pas le bon !

#mount /mnt/cdrom
#if [ "`grep "Fedora Core 3" /mnt/cdrom/.discinfo`" != "" ]
#then
#  echo "  Cool, I found a Fedora Core 3 CDROM into the drive. I'll use it."
#  logger -t "update-3.2" "Cool, I found a Fedora Core 3 CDROM into the drive! I'll use it."
 # mkdir /var/tmp/apt
 # cd /var/tmp/apt
  #ln -s /mnt/cdrom/Fedora/RPMS RPMS.slis32
  #mkdir SRPMS.slis32
  #genbasedir --flat --bloat --progress /var/tmp/apt  slis32
  #echo "rpm file:/var/tmp/ apt slis32" > /etc/apt/sources.list
#fi
# rpm ftp://ftp.ac-creteil.fr/SLIS slis/3.2/i386 updates fedora-updates os slis fedora.us 
echo "rpm ftp://ftp.ac-grenoble.fr/pub/slis slis/3.2/i386 updates fedora-updates os slis" >> /etc/apt/sources.list
apt-get -q2 update

# Kernel update

echo "Kernel-update..."
logger -t "update-3.2" "Kernel-update..."
apt-get -q2 install kernel#2.6.12-2.3.legacy_FC3 2>/dev/null

echo "kernel-update ok!"

/sbin/new-kernel-pkg --mkinitrd --depmod --install 2.6.12-2.3.legacy_FC3

apt-get -q2 remove kernel#2.4.22-1.2149.nptl 2>/dev/null
apt-get -q2 remove kernel-smp#2.4.22-1.2149.nptl 2>/dev/null
apt-get -q2 remove kernel#2.4.22-1.2166.nptl 2>/dev/null
apt-get -q2 remove kernel-smp#2.4.22-1.2166.nptl 2>/dev/null
apt-get -q2 remove kernel#2.4.22-1.2199.8.legacy.nptl 2>/dev/null
apt-get -q2 remove kernel-smp#2.4.22-1.2199.8.legacy.nptl 2>/dev/null

rm -f /boot/System.map
rm -f /boot/vmlinuz


# Pre-download
echo "Getting files..."
logger -t "update-3.2" "Getting files..."
apt-get -q2 --download-only dist-upgrade
if [ $? != 0 ]
then
  logger -t "update-3.2" "ERROR: apt-get --download-only failed! Reverting to the initial state."
  echo "ERROR: apt-get --download-only failed! Reverting to the initial state."
  echo "rpm ftp://ftp.ac-grenoble.fr/pub/slis slis/3.1/i386 updates fedora-updates" > /etc/apt/sources.list
  /bin/rm -f /var/cache/dump.sql
  /bin/rm -f /var/cache
  /bin/mv -f /www-cache/cache /var/
  /sbin/service squid start
  exit 2
fi

# Now, no more tests... it goes or it brakes!
# Upgrade
echo "The big upgrade (may be long)..."
logger -t "update-3.2" "The big upgrade (may be long)..."
apt-get -q2 dist-upgrade

# Fixes a missing link to sendmail
echo "Fixing missing sendmail link..."
logger -t "update-3.2" "Fixing missing sendmail link..."
/bin/ln -s /etc/alternatives/mta-sendmail /usr/lib/sendmail

# Kernel config
echo "Kernel-config..."
logger -t "update-3.2" "Kernel-config..."
echo "udev_initrd=\"yes\"" >> /etc/udev/udev.conf
/sbin/mkinitrd -f /boot/initrd-2.6.12-2.3.legacy_FC3.img 2.6.12-2.3.legacy_FC3

# Permissions
echo "Setting permissions..."
logger -t "update-3.2" "Setting permissions..."
/usr/local/sbin/permslis


# APT config
echo "APT configuration..."
logger -t "update-3.2" "APT configuration..."
/bin/rm -rf /etc/apt/pgp/*
/bin/rm -rf /etc/apt/apt.conf.d/*
/bin/rm -rf /etc/apt/sources.list.d/*
echo "Apt::State::FirstRun \"false\";" >> /etc/apt/apt.conf
echo "RPM::Upgrade-Virtual \"false\";" > /etc/apt/apt.conf.d/kernel.conf
echo "RPM::Allow-Duplicated { \"^gpg-pubkey\$\"; };" > /etc/apt/apt.conf.d/gpg-pubkeys.conf

# Don't know why, but bind seems to need a restart
/sbin/service named restart
sleep 10

# Don't know why, but I need to do another upgrade
apt-get -q2 update
apt-get -q2 dist-upgrade

# PG restore
echo "PG restore..."
logger -t "update-3.2" "PG restore..."
/sbin/service postgresql stop
cd /var/lib
/bin/mv pgsql/ pgsql.32upd-backup
mkdir pgsql
chmod 700 pgsql
chown postgres.postgres pgsql
/sbin/service postgresql start
sleep 10
echo "alter user postgres with password '$PGPASSWORD'" > /tmp/pg.$$.sql
su - postgres -c "psql -qf /tmp/pg.$$.sql template1 >/dev/null"
/bin/rm -f /tmp/pg.$$.sql
sleep 5
su - postgres -c "psql -qf /var/cache/dump.sql template1 > /dev/null"
/bin/cp -f /var/lib/pgsql.32upd-backup/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf
perl -pi -e "s/.*tcpip_socket.*=.*/tcpip_socket = true/" /var/lib/pgsql/data/postgresql.conf
/sbin/service postgresql stop
sleep 10
/sbin/service postgresql start

# Don't know why TLS doesn't work anymore
authconfig --kickstart --disableldaptls
/sbin/service ldap restart

# Config de PAM (messages cron)
echo "Pam session configuration for crond..."
logger -t "update-3.2" "Pam session configuration for crond..."
grep -v "^session" /etc/pam.d/crond > /tmp/crond
echo "#
# system-auth inclusion disabled for preventing floody-logging messages (pam_unix session module)
#session    required  /lib/security/\$ISA/pam_stack.so service=system-auth
session     required      /lib/security/\$ISA/pam_limits.so
#session     required      /lib/security/\$ISA/pam_unix.so
session     optional      /lib/security/\$ISA/pam_ldap.so
#" >> /tmp/crond
cp -f /tmp/crond /etc/pam.d/crond

# SLIS updates
echo "Installing SLIS 3.2 updates..."
#apt-get install -q2 slis-up2date
apt-get -q2 --force-yes install slis-up2date openldap=2.2.13 openldap-clients=2.2.13 compat-openldap=2.1.30 openldap-servers=2.2.13

# Reconfiguration
echo "SLIS reconfiguration..."
logger -t "update-3.2" "SLIS reconfiguration..."
/usr/local/sbin/mkhttpdconf -n -d -g -c -s -p /etc/php.ini.rpmnew
/usr/local/sbin/mkslapdconf  > /etc/openldap/slapd.conf
/usr/local/sbin/mksendmailcf -a
/usr/local/sbin/mknamedconf > /etc/named.conf
/usr/local/sbin/mknamedzones
/usr/local/sbin/mknamedacls
/usr/local/sbin/mksquidconf
/usr/local/sbin/mksquidguardconf
perl -pi -e "s/.*tls_checkpeer.*/tls_checkpeer no/" /etc/ldap.conf
/usr/local/sbin/squid_rotate

# Don't know why TLS doesn't work anymore
authconfig --kickstart --disableldaptls
/sbin/service ldap restart

# Don't know why, but bind seems to need a restart
/sbin/service named restart
sleep 10

# Courier Imap
echo "Courrier imap installation..."
logger -t "update-3.2" "Courrier imap installation..."
rpm -e imap-2002d-3* 2>/dev/null
apt-get -q2 install courier-imap courier-authlib-ldap

/sbin/chkconfig courier-authlib on
/sbin/chkconfig courier-imap on


# Restore cache dir
echo "Cache dir restore..."
logger -t "update-3.2" "Cache dir restore..."
apt-get -q2 clean

/bin/rm -f /var/cache
/bin/mv -f /www-cache/cache /var

# Version number
echo "Version number..."
logger -t "update-3.2" "Version number..."
echo "3.2 - Fedora Core 3 (upgraded from 3.1 - Fedora Core 1)" > /etc/version_slis
echo "3.2" > /etc/version_slis_num

# Remove the local repos
perl -pi -e "s/rpm file:.*//" /etc/apt/sources.list
/bin/rm -rf /var/tmp/apt

# Option pci=nosort
echo "Setting pci=nosort at boot to prevent from eth swap..."
perl -pi -e "s/kernel(.*) ro /kernel\$1 ro pci=nosort /" /boot/grub/menu.lst

echo "Done. Please, reboot..."
logger -t "update-3.2" "Done. Please, reboot..."

