TEMP_WORK_DIR=tmp_sqsh_work_dir
TEMP_WORK_FILE1=tmp_sqsh_work_file1

## some tool checks
if [ ! -x "uncramfs" ]; then
  echo "Need the uncramfs tool in local dir"
  echo "See original package"
  exit 0
fi

##check command line
if [ "$1" = "" -o "$2" = "" ]; then
  echo "Simple undreamboxfs script/tool for PPC/DB"
  echo "http://t-hydron.verkoyen.be"
	echo ""
  echo "Usage:"
  echo "undreamboxfs dreambox.img outputdir"
  echo "Example:"
  echo "undreamboxfs rel106.img output"
  exit 0
fi

if [ -e "$2" ]; then
  echo "Output dir already exists!"
  exit 0
fi

#offset is always 0x00120000 (see MTD partition 5)
OFFSET1=1179648

## get the squashed fs 
BLOCKSIZE=512
BOFFSET=$(($OFFSET1 / $BLOCKSIZE))
BLOCKS=$((8000000 / $BLOCKSIZE))
dd if=$1 of=$TEMP_WORK_FILE1 bs=$BLOCKSIZE count=$BLOCKS skip=$BOFFSET

#mount the squashfs file on loop device
mkdir $TEMP_WORK_DIR
mount -t squashfs -o loop $TEMP_WORK_FILE1 $TEMP_WORK_DIR
CHECK=`mount | grep $TEMP_WORK_DIR`

if [ "$CHECK" = "" ]; then
  rm -rf $TEMP_WORK_FILE1
  rm -rf $TEMP_WORK_DIR
  echo "Could not mount Squashfs filesystem! Are you sure you're running a dream 1.06+ kernel?"
  exit 0
fi

rm -rf $2
cp -a $TEMP_WORK_DIR $2
umount $TEMP_WORK_DIR

rm -rf $TEMP_WORK_DIR 
rm -rf $TEMP_WORK_FILE1
./uncramfs $TEMP_WORK_DIR $1 > /dev/null
cp -a $TEMP_WORK_DIR/* $2

##cleanup
rm -rf $TEMP_WORK_DIR 
rm -rf $TEMP_WORK_FILE1

echo "Done!"