#!/bin/sh
#
# So... what is the license? GPL? Yep...
#
# Authors are: averne and yoshi (appearance in alphabetical order)
# Reach them at @pld-linux.org or somewhere else.
#

ARCH=`uname -m`

if [ "`uname -r |cut -f -2 -d '.'`" == "2.2" ]; then
	echo first install 2.4.6 or newer kernel
	exit 1
fi

# PLD Ac source (change if you upgrade from other location (LAN for example)
SOURCE_HREF="http://ftp.pld-linux.org/dists/ac/PLD/${ARCH}/PLD/RPMS/"
SOURCE_NAME="ac-${ARCH}"

if [ "$SOURCE_HREF" == "" ] || [ "$SOURCE_NAME" == "" ]; then
	echo No source
	exit 1
fi

PACKAGES_1="glibc coreutils rpm poldek openssl openssh pcre popt acl attr"
PACKAGES_2="kernel grub rc-boot lilo dev FHS localedb-src"
mv /etc/poldek.conf /etc/poldek.conf.ra

cp /etc/lilo.conf /etc/lilo.conf.ra
cp /boot/grub/menu.lst /boot/grub/menu.lst.ra

grep '^\(ignore\|hold\)' /etc/poldek.conf.ra | grep -v "#" 2>&1 > /dev/null
STATUS=$?
if [ $STATUS -eq 0 ]; then
    echo "You have some hold/ignore lines in poldek.conf. SOME THING WILL GO WRONG"
	grep '^\(ignore\|hold\)' /etc/poldek.conf.ra
fi


cat>>/etc/poldek.conf<<EOF
source = $SOURCE_NAME $SOURCE_HREF
particle_install=no
unique_package_names=yes
follow=yes
greedy=yes
mercy=yes
EOF
grep ^ftp_get /etc/poldek.conf.ra >> /etc/poldek.conf
grep ^http_get /etc/poldek.conf.ra >> /etc/poldek.conf
grep ^https_get /etc/poldek.conf.ra >> /etc/poldek.conf
grep ^rsync_get /etc/poldek.conf.ra >> /etc/poldek.conf
grep ^cdrom_get /etc/poldek.conf.ra >> /etc/poldek.conf
grep ^rpmdef /etc/poldek.conf.ra >> /etc/poldek.conf

/usr/bin/poldek --sn=$SOURCE_NAME --up
/usr/bin/poldek --sn=$SOURCE_NAME --nodeps --force --upgrade $PACKAGES_1 2>/dev/null
ldconfig
/usr/bin/poldek --sn=$SOURCE_NAME --nodeps --force --upgrade $PACKAGES_2

if [ -f /etc/sysconfig/pcmcia ]; then
	cp /etc/sysconfig/pcmcia /etc/sysconfig/pcmcia.ra2acsave
	cat /etc/sysconfig/pcmcia|sed -e s,^PCIC=i82365,PCIC=yenta_socket,g > /etc/sysconfig.pcmcia.ra2acnew
	cp /etc/sysconfig.pcmcia.ra2acnew /etc/sysconfig/pcmcia
fi

rm /etc/poldek.conf

cat>>/etc/poldek.conf<<EOF
source = $SOURCE_NAME $SOURCE_HREF
particle_install=yes
unique_package_names=yes
follow=yes
greedy=yes
mercy=yes
EOF


mv /etc/rc.d/rc.local /etc/rc.d/rc.local.ra2acsave
cat>>/etc/rc.d/rc.local<<EOF
if [ -d /home/httpd ]; then
	mv /home/httpd /home/services/httpd
fi
/usr/bin/poldek --sn=$SOURCE_NAME --up --nodeps --force --upgrade-dist
. /etc/sysconfig/i18n

if [ "`rpm -qa|grep localedb-src`" == "" ]; then
	/usr/bin/poldek --sn=$SOURCE_NAME --up --nodeps --force -i localedb-src
fi

if [ "$LANG" != "" ]; then
	/usr/bin/localedb-gen -l $LANG
else
	echo "Wykonaj 'localedb-src -l LOCALE' aby wygenerowa locale dla jzyka LOCALE"
fi
mv /etc/rc.d/rc.local.ra2acsave /etc/rc.d/rc.local
/sbin/reboot
EOF
chmod +rx /etc/rc.d/rc.local

echo You should now execute bootmanager configuration.
echo If you use lilo - lilo.
echo If you use grub - configuration will be not necessary.
echo Be aware that .ra files in /etc and /boot/grub should contain old 
echo configs of poldek, lilo and grub.

