#!/system/bin/sh
# Author: feonwan@gmail.com
# Modified: 2014-10-29

# check busybox installation
echo "Checking busybox installation"
if [ ! -e /system/xbin/busybox ] && [ ! -e /system/bin/busybox ] ; then
  echo "Installing busybox..."
  mount -o rw,remount /system
  cp busybox /system/xbin/
  chmod 0755 /system/xbin/busybox
  mount -o ro,remount /system
  export PATH=$PATH:/system/xbin
  echo "Busybox have been installed to /system/xbin."
fi

# variables
PRJN=qemu-lp # Project Name
IMG=./${PRJN}.img
TMP=/data/${PRJN}_tmp
mkdir $TMP
busybox mount qemu.img $TMP
install_size=`busybox du -s $TMP`
data_part_size=`busybox df | busybox grep '\/data' | busybox awk '{print $4}'`

# copy files
if [ $install_size -ge $data_part_size ]; then
  echo "No Space Left in /data part. No deployment of qemu.img!"
  exit 1;
else
  echo "Whehter install ${PRJN} to /data directory? (Y/n)"
  read answer
  if [ "$answer" == "" || "ansewer" == "Y" ]; then
    echo "${PRJN} installation ..."
    INS_DIR=/data/${PRJN}
    mkdir $INS_DIR
    cp -R $TMP/* $INS_DIR
    chmod 0755 $INS_DIR/bin/*
    chmod 0755 $INS_DIR/lib/ld-linux-armhf.so.3
    chmod 0755 $INS_DIR/libexec/*
    echo "File copy Completed!"
  else
    echo "Installation is canneled!"
  fi
fi

# unmount and delete tmporary files
echo "Finishing installation..."
busybox umount $TMP
if [ $? -eq 0 ]; then
  rm -r $TMP
fi
echo "Installation Completed!"