#!/bin/sh

set -Eeuo pipefail

SUPERSU_ZIP_URL=https://supersuroot.org/downloads/SuperSU-v2.82-201705271822.zip
BLUESTACKS=/Applications/BlueStacks.app
ADB=${BLUESTACKS}/Contents/MacOS/adb

SUPERSU_ZIP_NAME=SuperSU.zip
ROOT_SCRIPT_NAME=root.sh
BLUESTACKS_BIN=BlueStacks.app/Contents/MacOS/BlueStacks
ANDROID_VBOX=${HOME}/Library/BlueStacks/Android/Android.vbox
ANDROID_SERIAL=localhost:5555

# check if we're on darwin
if [[ "$(uname -s)" != "Darwin" ]]; then
  echo "only Darwin is supported"
  exit 1
fi

# check if there's disks set as Readonly
if [[ $(grep 'type="Readonly"' "${ANDROID_VBOX}" | wc -l) -ne 0 ]]; then
  # check if BlueStacks is running
  if [[ $(ps aux | grep "${BLUESTACKS_BIN}" | grep -v grep | wc -l) -ne 0 ]]; then
    echo "please close bluestacks to prevent configuration corruption"
    exit 1
  fi

  echo "changing disks to RW mode..."
  sed -i.bak 's/type="Readonly"/type="Normal"/g' "${ANDROID_VBOX}"
fi


read -n 1 -s -r -p "please enable ADB on BlueStacks preferences, then press any key to continue"
echo

TMP=$(mktemp -d)
pushd "${TMP}" > /dev/null

echo "downloading SuperSU..."
curl -fsSLo ${SUPERSU_ZIP_NAME} ${SUPERSU_ZIP_URL}

# connect to the default port exposed by BlueStacks
echo "connecting to BlueStacks..."
"${ADB}" connect ${ANDROID_SERIAL}

# push SuperSu zip to device emulated sdcard
echo "pushing SuperSU zip file..."
"${ADB}" -s ${ANDROID_SERIAL} push ${SUPERSU_ZIP_NAME} /mnt/sdcard

echo "creating root script..."
cat <<EOF > ${ROOT_SCRIPT_NAME}
#!/system/bin/sh

# Disable SELinux
echo "disabling SELinux..."
setenforce 0

# Remount all filesystems
echo "Remounting / as RW..."
mount -o rw,remount,rw /
echo "Remounting /system as RW..."
mount -o rw,remount,rw /system
echo "Remounting /storage/emulated as RW..."
mount -o rw,remount,exec,rw /storage/emulated

# Unzip SuperSU zip
echo "unzipping SuperSU..."
cd /mnt/sdcard
mkdir supersu
cd supersu
unzip -q ../${SUPERSU_ZIP_NAME}

# Extract su command and run its post-install
echo "extracting su binary and configuring it..."
cp x64/su /system/xbin/su
chmod a+rwx /system/xbin/su
echo "post-install su..."
/system/xbin/su --install

# Start su daemon mode
echo "starting su daemon..."
/system/xbin/su --daemon

# cleanup
echo "cleaning up..."
cd ..
rm -rf supersu
rm ${SUPERSU_ZIP_NAME}
rm ${ROOT_SCRIPT_NAME}
EOF

echo "pushing root script..."
"${ADB}" -s ${ANDROID_SERIAL} push ${ROOT_SCRIPT_NAME} /mnt/sdcard

# Use the provided su binary to run the script as root
echo "running root script..."
"${ADB}" -s ${ANDROID_SERIAL} shell "/system/xbin/bstk/su -c sh /mnt/sdcard/${ROOT_SCRIPT_NAME}"

# install apk
echo "installing apk..."
unzip -q ${SUPERSU_ZIP_NAME}
"${ADB}" -s ${ANDROID_SERIAL} install -r common/Superuser.apk

echo "disconnecting adb..."
"${ADB}" disconnect ${ANDROID_SERIAL}

popd "${TMP}" > /dev/null

echo "cleaning up..."
rm -rf "${TMP}"

echo "Done!"
echo "Open up SuperSU on BlueStacks to update the binaries (force quit BlueStacks afterwards, as it'll hang)"
echo "Then finally reboot it and enjoy!"