#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/system-filesystems-ext2-to-ext3 - mark filesystems for
#                                                    ext3 conversion
#
# Copyright (c) 2005 Frank Meyer <frank@eisfair(dot)org>
#
# Creation   : 24.07.2004 fm
# Last Update: $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------
. /var/install/include/eislib
gotoyx () { echo -e "\033[$1;$2H\c"; }
kernel_good=false

status=`/var/install/bin/check-version eiskernel 1.0.9`
if [ "$status" = "installed" -o "$status" = "old" ]
then
    kernel_good=true
fi

if [ $kernel_good = false ]
then
    mecho -error "You need eiskernel version 1.0.9 (or higher) to convert"
    mecho -error "filesystems to ext3."
    mecho -error "Please install a newer version of eiskernel"
    exit 1
fi

do_exit=false

while [ 1 ]
do
    marked=false
    title_printed=false
    clrhome
    mecho -info "Convert ext2-filesystems to ext3"
    echo
    echo

    i=1
    while read line
    do
        set -- $line
        fs_spec=$1
        fs_file=$2
        fs_vfstype=$3
        fs_mntopts=$4
        fs_freq=$5
        fs_passno=$6

        if [ "$fs_vfstype" = "ext2" ]
        then
            if [ "$title_printed" = false ]
            then
                echo "  ext2 filesystems found:"
                echo
                title_printed=true
            fi

            if grep "^$fs_spec\$" /tmp/convert-ext2-to-ext3.list  >/dev/null 2>&1
            then
                marked=true
                eval fs_vfstype_$i=ext3
                echo "     $i: $fs_spec $fs_file (marked for ext3-conversion)"
            else
                eval fs_vfstype_$i=ext2
                echo "     $i: $fs_spec $fs_file"
            fi
        
            eval fs_spec_$i=$fs_spec
            i=`expr $i + 1`
        fi

    done </etc/fstab

    echo
    i=`expr $i - 1`

    if [ $i -eq 0 ]
    then
        echo "No ext2 filesystem found. Exit."
        break
    else
        gotoyx 20 1
        echo -e "Number of filesystem to mark/unmark (1-$i, ENTER=Return 0=Exit): \c"
        read answer

        if [ "$answer" = "" ]
        then
            break
        else
            if [ "$answer" = 0 ]
            then
                do_exit=true
                break
            fi
            eval fs_vfstype='$fs_vfstype_'$answer
            eval fs_spec='$fs_spec_'$answer
            if [ "$fs_vfstype" = "ext2" ]
            then
                echo $fs_spec >>/tmp/convert-ext2-to-ext3.list
            else
                grep -v "^$fs_spec\$" /tmp/convert-ext2-to-ext3.list >/tmp/convert-ext2-to-ext3.list.$$
                mv /tmp/convert-ext2-to-ext3.list.$$ /tmp/convert-ext2-to-ext3.list
            fi
        fi
    fi
done

if [ $i -ne 0 -a $marked = false ]
then
    rm -f /tmp/convert-ext2-to-ext3.list
fi

echo
if [ -f /tmp/convert-ext2-to-ext3.list ]
then
    mkdir -p /var/boot-ext
    echo "/bin/sh /var/install/bin/convert-ext2-to-ext3" >/var/boot-ext/convert-ext2-to-ext3
    mecho -info "INFO: ext3-filesystem conversion will be started on next reboot."
else
    rm -f /var/install/boot-ext/convert-ext2-to-ext3
    mecho -info "INFO: nothing marked for ext3 filesystem conversion."
fi

if [ $do_exit = true ]
then
    exit 127
fi

echo
anykey