#!/bin/ash

export PATH=.:/:/usr/sbin:/usr/bin:/sbin:/bin
. liblinuxlive

header "starting Linux Live scripts <http://www.linux-live.org/>"

echolog "mounting /proc and /sys filesystems"
mount -t proc proc /proc
mount -t sysfs sysfs /sys

# setup DEBUGCMD variable. If debug boot option is present, call debug()
# function several times during script's execution
if [ "`cmdline_parameter debug`" ]; then DEBUGCMD="debug"; else DEBUGCMD=""; fi

$DEBUGCMD

# we have to set these variables very carefully
UNION=/union
MEMORY=/memory
MOUNTDIR=mnt
CHANGES=$MEMORY/changes
COPY2RAM=$MEMORY/copy2ram
IMAGES=$MEMORY/images
INITRAMDISK=$MOUNTDIR/live

# amount of RAM to store changes
RAMSIZE="`cmdline_value ramsize`"
if [ "$RAMSIZE" = "" ]; then RAMSIZE="60%"; fi

# cdrom support, isofs support, unionfs support, etc
modprobe_essential_modules
setup_dma
echo -e "ext3\next2\nvfat\n*" >/etc/filesystems

# in some cases, we need USB modules soon
if [ "`cmdline_parameter probeusb`" != "" ]; then
   modprobe_usb_modules
fi

$DEBUGCMD

# $UNION will be used as a root directory, livecd modules will be added soon.
echolog "setup union on $UNION"
mkdir -p $UNION
mkdir -p $MEMORY

CHANGESDEV="`cmdline_value changes`"
if [ "$CHANGESDEV" != "" ]; then
   # if starting with /dev/, mount the device
   if [ "`echo $CHANGESDEV | egrep \"^/dev/\"`" != "" ]; then
      CHANGESPATH="`echo $CHANGESDEV | cut -d \"/\" -f 4`"
      CHANGESDEV="`echo $CHANGESDEV | cut -d \"/\" -f 1-3`"
      echo "mounting $CHANGESDEV to $MEMORY"
      mount_device $CHANGESDEV $MEMORY
   else false; fi
else false; fi

# mount tmpfs only in the case when changes= boot parameter was empty
# or we were not able to mount the storage device
if [ $? -ne 0 ]; then mount -t tmpfs -o "size=$RAMSIZE" tmpfs $MEMORY; fi

mkdir -p $CHANGES
mkdir -p $IMAGES
mount -t unionfs -o debug=0,dirs=$CHANGES=rw unionfs $UNION
if [ $? -ne 0 ]; then fatal "can't setup union in /union directory"; fi

$DEBUGCMD

# try to find livecd data directory. If not found, try modprobing
# USB kernel modules and repeat the find procedure again
echolog "looking for data modules"
DATA="`find_live_data_dir $MOUNTDIR`";
if [ "$DATA" = "" ]; then
   modprobe_usb_modules;
   echolog -n "searching for usb devices .."
   for i in 1 2 3 4 5 6 7 8; do # max 36 seconds, prolong the delay each time
      echolog -n "."
      create_block_devices >/dev/null
      DATA="`find_live_data_dir $MOUNTDIR`";
      if [ "$DATA" != "" ]; then break; fi
      sleep $i;
   done
fi

if [ "$DATA" = "" ]; then fatal "
Data for LiveCD not found. Searching for livecd.sgn file, not found.
You are maybe using an unsupported boot device
(for example a CD-ROM connected to SCSI or PCMCIA interface)
Try to copy all data from your boot media to your IDE harddisk,
eg. to /mnt/sda1/alldata/ or C:\\alldata\\. Then boot again."; fi
echolog "found in $DATA"

$DEBUGCMD

# If toram or copy2ram boot parameter is present, copy all .mo modules to RAM.
# (skip modules from /optional/ which are not listed in load= boot option)
# Finaly modify DATA variable so it will point to correct directory
if [ "`cmdline_parameter toram`" != "" -o "`cmdline_parameter copy2ram`" != "" ]; then
   echolog "copying data to RAM, this may take some time..."
   mkdir -p $COPY2RAM

   # make sure it's in RAM even with changes= parameter
   if [ "$CHANGESDEV" != "" ]; then mount -t tmpfs -o "size=$RAMSIZE" tmpfs $COPY2RAM; fi
   copy_to_ram $DATA $COPY2RAM

   cd_autoeject 1
   umount $DATA 2>/dev/null
   if [ $? -ne 0 ]; then umount `dirname $DATA` 2>/dev/null; fi
   rmdir $MOUNTDIR/* 2>/dev/null # mounted device names are empty, remove them
   DATA=$COPY2RAM
   cd_autoeject 0
fi

mkdir -p $UNION/boot
mount -o bind $DATA $UNION/boot

$DEBUGCMD

# DATA contains path to the base directory of all .mo images which need
# to be mounted and inserted into live filesystem. Do it now.
echolog "inserting all modules and creating live filesystem"
union_insert_modules $UNION $DATA $IMAGES

# the $MEMORY directory can contain $MEMORY/modules too
# in the case where changes= boot argument is used. If not, it doesn't hurt
union_insert_modules $UNION $MEMORY $IMAGES

$DEBUGCMD

echo "copying rootchanges"
cp -a $DATA/rootcopy/* $UNION 2>/dev/null # may be empty

$DEBUGCMD

echo "creating /etc/fstab"
fstab_update $UNION

# More likely these directories aren't there.
# Even if they are, this won't hurt.
mkdir -p $UNION/proc
mkdir -p $UNION/sys
mkdir -p $UNION/dev
mkdir -p $UNION/tmp
chmod 0777 $UNION/tmp

$DEBUGCMD

# Union contains all the files and directories unioned from all modules.
# Change root directory to it, and move initrd's root to /mnt/live/initramdisk
# Finaly execute /sbin/init to start the distribution.
echolog "changing root directory..."
cd $UNION
mkdir -p $INITRAMDISK

umount /sys # we won't need it anymore
if [ ! -e $UNION/dev/console ]; then mknod $UNION/dev/console c 5 1; fi

if [ -x $UNION/usr/sbin/chroot ];
  then CHROOT=/usr/sbin/chroot
  else CHROOT=/usr/bin/chroot
fi

header "linux live end"
pivot_root . $INITRAMDISK
exec $CHROOT . sbin/init <dev/console >dev/console 2>&1

header "ERROR!"
echolog "You are not supposed to be here, something went wrong!"
echolog "Even Ctrl+Alt+Del won't help you in kernel panic."

