#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL

# To avoid there is no input output in rc running, we add this one:
exec </dev/console >/dev/console 2>&1

# Decide the path for live media.
# casper 1.77+debian-7 uses /live_media, 1.110 uses /cdrom
# live-initramfs uses /live/image.
possible_path="/live_media /cdrom /live/image"
# Possible kernel/initrd paths are /casper (created by casper) or /live (created by live-initramfs)
live_sys_files_dir_list="casper live isolinux"

LIVE_MEDIA=""
# It's too slow to use find to search the whole system... Therefore we use this method.
for i in $possible_path; do
  for j in $live_sys_files_dir_list; do
    if [ -f "$i/$j/filesystem.squashfs" ]; then
      LIVE_MEDIA="$i"
      break
    fi
  done
  [ -n "$LIVE_MEDIA" ] && break
done
if [ -z "$LIVE_MEDIA" ]; then
  echo "///WARNING/// filesystem.squashfs not found! No idea where is LIVE_MEDIA!!!"
fi
echo "Live media is in $LIVE_MEDIA"

# This script will run those scripts in dir /$LIVE_MEDIA/ocs-live.d
[ ! -d "/$LIVE_MEDIA/ocs-live.d/" ] && exit 1
for script in /$LIVE_MEDIA/ocs-live.d/S[0-9][0-9]*; do
  . $script
done
