# QEMU Linux Portable (QLP) Installation Script
# Author: felonwan@gmail.com
# Modified: 2014-10-31

# variable
PRJN=qemu\-lp # Project Name
ZIP=./${PRJN}.zip
INS=/data/${PRJN}
BIN=$INS/bin
TMP=/data

# choose whther install
echo "Install ${PRJN} to /data directory？(Y--Yes or other inputs--No)"
read ans
if [ "$ans" == "Y" ]; then
    echo "Thanks for choosing ${PRJN}！"
else
    echo "You've cancelled the installation. Now exit..."
    exit 0
fi

# Check and install busybox
echo "Checking installation of Busybox..."
busybox >/dev/null
if [ $? -ne 0 ]; then
  echo "Installing Busybox……"
  cp busybox $TMP/
  chmod 0755 $TMP/busybox
  if [ $? -eq 0 ]; then
	  echo "Busybox is installed to $TMP"
  else
	  echo "Error 1: Installing of Busybox Failed!"
	  exit 1;
  fi
  export PATH=$PATH:$TMP
else
  echo "Busybox is installed!"
fi

# decompress files
echo "Inflating files..."
mkdir -p $BIN
busybox unzip $ZIP -d $INS
if  [ $? -eq 0 ]; then
    echo "Decompression completed!"
else
   echo "Error 2: Decompressing files Failed"
   exit 2
fi

#setting permissions
echo "Setting file permissions ..."
chmod 0755 $INS/bin/*
chmod 0755 $INS/lib/ld-linux-armhf.so.3
chmod 0755 $INS/libexec/*
echo "Permission setting completed!"

#finishing installation
rm $ZIP
rm INSTALL*
rm busybox
rm $TMP/busybox
echo "Installation complete"
echo "Exit..."
