#!/bin/sh

dir=`pwd`/../../..

/bin/rm -rf /tmp/rhimage
mkdir -m 0700 /tmp/rhimage || exit 1
/bin/rm -rf /tmp/drakx
mkdir -m 0700 /tmp/drakx || exit 2

for i in Mandrake RPMS misc boot images
do
    ln -sf "$dir/../../$i" "/tmp/rhimage/$i" 
done

/bin/cp -a "$dir/../../Mandrake/mdkinst" "/tmp/drakx"

if [ -x "/tmp/drakx/mdkinst/usr/bin/perl-install/live_install2" ]; then
    perlcoredir=/usr/lib/perl5/5.6.0/i386-linux/CORE
    cd /tmp/drakx/mdkinst/usr/bin/perl-install
    if [ -x $perlcoredir/libperl.so ]; then
	mv $perlcoredir/libperl.so /tmp/drakx/mdkinst
    fi
    mkdir -p $perlcoredir
    ln -sf /tmp/drakx/mdkinst/$perlcoredir/libperl.so $perlcoredir/libperl.so

    if [ -x /usr/bin/packdrake ]; then
	mv /usr/bin/packdrake /tmp/drakx/mdkinst
    fi
    ln -sf /tmp/drakx/mdkinst/usr/bin/packdrake /usr/bin/packdrake

    for i in MonitorsDB CardsNames Cards+
    do
	if [ -e /usr/X11R6/lib/X11/$i ]; then
	    mv /usr/X11R6/lib/X11/$i /tmp/drakx/mdkinst
	fi
	ln -sf /tmp/drakx/mdkinst/usr/X11R6/lib/X11/$i /usr/X11R6/lib/X11/$i
    done

    # start DrakX
    ../../../lib/ld-linux.so.2 ../perl ./live_install2

    for i in MonitorsDB CardsNames Cards+
    do
	rm -f /usr/X11R6/lib/X11/$i
	if [ -e /tmp/drakx/mdkinst/$i ]; then
	    mv /tmp/drakx/mdkinst/$i /usr/X11R6/lin/X11/$i
	fi
    done

    rm -f /usr/bin/packdrake
    if [ -x /tmp/drakx/mdkinst/packdrake ]; then
	mv /tmp/drakx/mdkinst/packdrake /usr/bin/packdrake
    fi

    rm -f $perlcoredir/libperl.so
    if [ -x /tmp/drakx/mdkinst/libperl.so ]; then
	mv /tmp/drakx/mdkinst/libperl.so $perlcoredir/libperl.so
    fi
else
    echo >&2 "unable to get a working live system to start, check your working directory"
fi

#/bin/rm -rf /tmp/drakx
#/bin/rm -rf /tmp/rhimage