#!/bin/bash
# Installation de linuxconsole depuis une autre distro
# V1.0 : testé su ubuntu 7.10
# V2.0 : testé su fedora core 8
# V2.1 : Corrections pour Aspire One
# V3.0 : Corrections pour Ubuntu 10.4
# TODO : tests MD5SM
# TODO : faire fonctionner aussi avec LILO

which yum 1>/dev/null && BASE=fedora
which apt-get 1>/dev/null && BASE=ubuntu
which wget 1>/dev/null || WGET="no"

if [ "$1" = "fix_initrd" ]
then
  [ ! -e /etc/grub.d/10_linuxconsole ] && echo "/etc/grub.d/10_linuxconsole not found" && exit 1
#  INITRD="/boot/263212"
  INITRD=`grep initrd /etc/grub.d/10_linuxconsole | cut -d' ' -f2 | tr -d '\t'`
  echo "--$INITRD--"
  cd $HOME/linuxconsole-initrd || exit 1
  gzip -dc < "$INITRD" | cpio -id
  cp -fR $HOME/fix-initrd/* .
  # cd $HOME/linuxconsole-initrd || exit 1
  find . -print | cpio -o -H newc | gzip -9 > $HOME/`basename $INITRD`
fi
if [ "$1" = "uninstall" ]
then
  [ -e /etc/grub.d/10_linuxconsole ] && rm /etc/grub.d/10_linuxconsole && /usr/sbin/update-grub
  for file in `ls /linuxconsole/*.lcm`
  do
    echo delete $file 
    rm $file
  done
  exit 0
fi 
[ ! -e /cdrom ] && install -d /cdrom

RUN_DIR=`dirname "$0"`
echo $0
if [ -e "$RUN_DIR/lcinstall.nsi" ]
then
  echo "Running from CD"
  [ "$RUN_DIR" = "/cdrom" ] || mount "$RUN_DIR" /cdrom -o bind
else
  [ "$WGET" = "no" ] && [ "$BASE" = "fedora" ] && yum install wget
  which wget 1>/dev/null || exit 1
  cd $HOME
  URL=http://linuxconsole.org/1.0/2007/
  URL=http://ftp.cc.uoc.gr/mirrors/linux/linuxconsole/
  URL=http://jukebox.linuxconsole.org/testing/
  ISO=linuxconsole1.0.2007-multimedia.iso
  ISO=linuxconsole1.0.2009-cd.iso
  ISO=linuxconsole1.0.2010-multimedia.iso
  # MD5SUM=$ISO".md5sum"
  echo "Installing from $URL/$ISO"
  [ ! -e $ISO ] && wget $URL/$ISO
  # [ ! -e $MD5SUM ] && wget $URL/$MD5SUM
  # [ "`md5sum $ISO | cut -d' ' -f 1`" = "`cat $MD5SUM | cut -d' ' -f1`" ] || echo "Erreur MD5SUM"
  mount $ISO /cdrom -o loop
fi
install -d /linuxconsole/virtualfs1.0

GRUBCONF=""
LILOCONF=""
LINUX=`grep kernel /cdrom/winxp/menu.lst | head -1 | cut -d ' ' -f2`
INITRD=`grep 'initrd /boot/' /cdrom/winxp/menu.lst  | head -1 | cut -d' ' -f2`
[ -e /boot/grub/menu.lst ] && GRUBCONF=/boot/grub/menu.lst
[ -e /boot/grub/grub.conf ] && GRUBCONF=/boot/grub/grub.conf
[ -e /boot/lilo.conf ] && LILOCONF=/boot/grub/menu.lst

if [ -n "$GRUBCONF" ]
then
  echo "Update GRUB at $GRUBCONF"
  cat /cdrom/winxp/menu.lst | while read line
  do
    # grep LinuxConsole /boot/grub/menu.lst && break
    echo $line | grep timeout && continue
    if [ "$BASE" = "fedora" ] 
    then
      echo $line | sed "s'boot/''" >> $GRUBCONF
    else
      echo $line >> $GRUBCONF
    fi
  done
  cp /cdrom/isolinux/`basename $LINUX` /boot
  cp /cdrom/isolinux/`basename $INITRD` /boot
  # chmod +w /boot/*
fi

if [ -e /etc/grub.d ] && [ -e /usr/sbin/update-grub ]
then
    cp /cdrom/isolinux/`basename $LINUX` /boot
    cp /cdrom/isolinux/`basename $INITRD` /boot
  cat > /etc/grub.d/10_linuxconsole << EOF     
#! /bin/sh -e
echo "LinuxConsole 2010: $linux" >&2
echo "
menuentry 'LinuxConsole2010' --class gnu-linux --class gnu --class os{
	recordfail
	insmod ext2
	set root='(hd0,1)'
	linux $LINUX
	initrd $INITRD	
}
"
EOF
  chmod +x /etc/grub.d/10_linux
  echo "running /usr/sbin/upgrad-grub ..."
  /usr/sbin/update-grub || exit 4
fi
which vgscan && vgscan | grep "Found volume"
if [ $? = 0 ]
then
  INITRD=`ls /cdrom/isolinux/26*  | cut -d '/' -f4 | cut -d'.' -f1`
  [ ! -n "$INITRD" ] && echo "initrd not found" && exit 0
  cp /cdrom/isolinux/"$INITRD" /tmp
  mv  /tmp/"$INITRD"  /tmp/"$INITRD".gz
  gunzip /tmp/"$INITRD".gz
  install -d /mnt/initrd
  # FIXME : now initrdamfs is cpio
  mount /tmp/$INITRD /mnt/initrd -o loop || exit 3
  for MOD in `find /mnt/initrd/lib/modules/ | grep ata`
  do
    [ -d $MOD ] && continue
    MODULE=`echo $MOD | cut -d'/' -f10 | cut -d'.' -f1`
    lsmod | grep $MOD || rm $MOD
  done
  # [ ! -e lvm.static ] && wget http://linuxconsole.org/1.0/lcsh/lvm.static
  # gzip lvm.static
  # mv lvm.static.gz /mnt/initrd
  cp /cdrom/lvm.static.gz /mnt/initrd
  umount /mnt/initrd
  gzip -9 /tmp/$INITRD 
  mv /tmp/$INITRD.gz /boot/$INITRD
fi

for file in `ls /cdrom/*.lcm`
do
  echo install $file 
  cp $file /linuxconsole
done

