#!/bin/sh

echon () {
    if [ X"$ECHON" = X ]
    then
	# Determine how to "echo" without newline: "echo -n"
	# or "echo ...\c"
	if [ X`echo -n` = X-n ]
	then ECHON=echo; NNL="\c"
	else ECHON="echo -n"; NNL=""
	fi

    fi
    $ECHON "$*$NNL"
}

ask() {
  #
  # ask a question with a yes/no answer;
  # Usage: ask [-y|-n] "question"
  # Set default: -y = yes; -n = no; otherwise no default;
  # Returns: 0 (true) = yes; 1 (false) = no;
  # Note: changing the default does not effect the return value;
  #
  ASK_DFLT=
  # process options/args
  for ASK_OPT do
	case "$ASK_OPT" in
	  -[yY]*) ASK_DFLT='y' ;;
	  -[nN]*) ASK_DFLT='n' ;;
	  --) shift; break ;;
	  -*) ;;
	  *) break
	  esac
	shift
    done
  ASK_PROMPT="$*"

  # get the response
  while : ; do
	echon "$ASK_PROMPT (y/n)?${ASK_DFLT:+ [$ASK_DFLT]} " >&2
	read ASK_ANSWER ASK_JUNK

	: ${ASK_ANSWER:=$ASK_DFLT}

	case "$ASK_ANSWER" in
	  [yY]*)
		return 0
		;;
	  [nN]*)
		return 1
		;;
	  *)
		echo "" >&2
	  esac
    done

  return
}

echo ''
echo '========================='
echo ' Zubuntu Flash Installer '
echo '========================='
echo ''

echo '* Checking for required packages'
if [ ! -e /usr/sbin/flash_eraseall ]; then
  echo '  *** Installation aborted *** Please install mtd-utils package'
  exit 12
fi

echo '* Creating temporary work space'
mkdir -p /omegamoon

DESTINATION=``

echo '* You have to choose one of the two available flash partitions now.'
echo '  These are the original Sharp /root (mtd2) and /home (mtd3) partitions.'
echo '  The /home (mtd3) partition is the largest one, approximately 90Mb in size.'
echo ''
if ask -y "  > Do you want to use the /home (mtd3) partition for Zubuntu?"
then
  DESTINATION="/dev/mtd3"
else
  if ask -y "  > Do you want to use the /root (mtd2) partition for Zubuntu?"
  then
    DESTINATION="/dev/mtd2"
  else
    echo '*** Installation aborted *** Invalid option!'
    exit 10
  fi
fi

echo ''
echo '** WARNING ** ALL DATA ON '$DESTINATION' WILL BE ERASED!!'
if ask -n "> Are you sure you want to erase the internal flash partition?"
then
  flash_eraseall $DESTINATION
else
  echo '*** Installation aborted ***'
  exit 11
fi

echo ''
echo '* Mounting flash partition'
if [ $DESTINATION="/dev/mtd3" ]; then
  mount -t jffs2 /dev/mtdblock3 /omegamoon
else
  mount -t jffs2 /dev/mtdblock2 /omegamoon
fi

echo '* Start copying files. Be patient!'

echo '  ... /bin'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz bin
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /boot'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz boot
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /dev'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz dev
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /etc'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz etc
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /home'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz home
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /initrd'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz initrd
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /lib'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz lib
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /lost+found'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz lost+found
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /mnt'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz mnt
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /opt'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz opt
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /root'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz root
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /sbin'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz sbin
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '  ... /srv'
cd /
tar -czpf /omegamoon/zubuntu-tmp.tar.gz srv
cd /omegamoon
tar -zxpf zubuntu-tmp.tar.gz
rm zubuntu-tmp.tar.gz

echo '* Creating directories'
mkdir -p /omegamoon/media
mkdir -p /omegamoon/media/card
mkdir -p /omegamoon/media/cf
mkdir -p /omegamoon/media/hdd
mkdir -p /omegamoon/media/mmc1
mkdir -p /omegamoon/media/net
mkdir -p /omegamoon/media/ram
mkdir -p /omegamoon/media/realroot
mkdir -p /omegamoon/media/union
mkdir -p /omegamoon/media/proc
mkdir -p /omegamoon/media/sys
mkdir -p /omegamoon/media/tmp

echo '* Creating default symbolic links (check section about postinstall on www.omegamoon.com)'
cd /omegamoon
ln -s /media/card/usr usr
ln -s /media/card/var var

echo '* Creating default mointpoint (check section about postinstall on www.omegamoon.com)'
echo '#! /bin/sh \
mount /dev/mmcblk0p1 /media/card' > /omegamoon/etc/rcS.d/S00mount-zubuntu-sd.sh

echo '* Unmounting flash partition'
umount /omegamoon

echo '* Cleaning up'
rmdir /omegamoon

echo ''
echo '======================='
echo ' Installation finished'
echo '======================='
echo ''

exit 0

