#!/bin/sh
# rc.swap 
# create swap in DOS part. for lowmem machine 
# by M. Andreoli

try_dos_swap()
{
[ "$DOS_SWAP" ] || return 1
setlevel 1
mount $DOS_SWAP /swap 2>/dev/null || return 1
echo "Swapping in a file in the $DOS_SWAP $(speak_dos $DOS_SWAP) disk."
setlevel 5
dd if=/dev/zero of=/swap/linux.swp bs=1k count=16000
ln -f -s /swap/linux.swp /dev/swap
mkswap /dev/swap
sync
swapon /dev/swap
}

try_linux_swap()
{
[ "$LINUX_SWAP" ] || return 1
ln -f -s $LINUX_SWAP /dev/swap
echo
echo "Warning: this destroy any data in the $LINUX_SWAP disk."
read -p "Continue (y/n) [y] " cont
[ -z "$cont" ] && cont=y
[ "$cont" != y ] && return 1
mkswap /dev/swap
sync
swapon /dev/swap
}

# MAIN

clear
echo -e "${BLINK}Low memory${NORMAL}"
echo -e "$BRIGHT$GREEN"
cat << END

  Setup has detect a low-cost machine (a $CPU, with $MEM  bytes free),
so you'll need a swap file (/linux.swp) to continue. This should be created
on your DOS partition. You can remove it, later, If You want.

  As alternative, muLinux can use a true Linux Swap device, if
available in the system.
END
echo -e "$NORMAL"

read -p "press -ENTER- " enter

# fdisk scan 

setlevel 1
fdisk -l 2>/dev/null | rgrep /dev | rgrep -v Disk| sed 's/\*/a/g' >/tmp/PAR
setlevel 5
clear
echo "This is the partition table of your system:"
echo -e "$BRIGHT"
echo "   Device Boot   Begin    Start      End   Blocks   Id  System"
cat /tmp/PAR
echo -e "$NORMAL"
echo
echo


# select swap destination

set -- `cat /tmp/PAR | rgrep DOS`
if [ -z "$1" ] ; then
set -- `cat /tmp/PAR | rgrep "b  Unknown"`
fi
DOS_SWAP=$1

set -- `cat /tmp/PAR | rgrep Linux swap`
LINUX_SWAP=$1

rm -f /tmp/PAR

# report to user

cat <<END

Our possibilities about swapping method:

0) do by yourself -- I will open the shell interpreter
END


if [ "$LINUX_SWAP" ] ; then
echo "1) $LINUX_SWAP -- a true Linux swap partition"
fi
if [ "$DOS_SWAP" ] ; then
echo "2) $DOS_SWAP -- I will swap in a file in this disk"
fi
echo
while [ 1 ] ; do
read -p "Your choice? (0/1/2) " method
[ "$method" ] && break
done

case "$method" in
0)
	/bin/-ash
	;;
1)
	try_linux_swap || (echo fail; reboot -f)
	;;
2)
	try_dos_swap || (echo fail; reboot -f)
	;;
esac

# End
