#!/sbin/sh
#
##############################################################
# File name       : update-binary
#
# Description     : Setup installation, environmental variables
#                   and helper functions
#
# Build Date      : Friday March 15 11:36:43 IST 2019
#
# BiTGApps Author : TheHitMan @ xda-developers
#
# Copyright       : Copyright (C) 2020 TheHitMan7 (Kartik Verma)
#
# License         : SPDX-License-Identifier: GPL-3.0-or-later
##############################################################
# The BiTGApps scripts are free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# These scripts are distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
##############################################################

# We will have to manually find out OUTFD
OUTFD=$2

readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
if [ "$?" -eq "0" ]; then
  # rerouted to log file, we don't want our ui_print commands going there
  OUTFD=0

  # we are probably running in embedded mode, see if we can find the right fd
  # we know the fd is a pipe and that the parent updater may have been started as
  # 'update-binary 3 fd zipfile'
  for FD in `ls /proc/$$/fd`; do
    readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null
    if [ "$?" -eq "0" ]; then
      ps | grep " 3 $FD " | grep -v grep >/dev/null
      if [ "$?" -eq "0" ]; then
        OUTFD=$FD
        break
      fi;
    fi;
  done
fi;

# Set environmental variables in the global environment
export ZIPFILE="$3"
export ZIPTYPE="basic"
export ADDON=""
export OUTFD="$OUTFD"
export TMP="/tmp"
export INTERNAL="/sdcard"
export EXTERNAL="/sdcard1"
# Supported Android SDK Versions 30, 29, 28, 27, 25
export TARGET_ANDROID_SDK="28"
# Supported Android platforms ARM & ARM64
export TARGET_ANDROID_ARCH="ARM64"
# Set addon for installation, according to variable $ADDON
if [ "$ZIPTYPE" == "addon" ]; then
  if [ "$ADDON" == "sep" ]; then
    export TARGET_ASSISTANT_GOOGLE=""
    export TARGET_CALCULATOR_GOOGLE=""
    export TARGET_CALENDAR_GOOGLE=""
    export TARGET_CONTACTS_GOOGLE=""
    export TARGET_DESKCLOCK_GOOGLE=""
    export TARGET_DIALER_GOOGLE=""
    export TARGET_MARKUP_GOOGLE=""
    export TARGET_MESSAGES_GOOGLE=""
    export TARGET_PHOTOS_GOOGLE=""
    export TARGET_SOUNDPICKER_GOOGLE=""
    export TARGET_WELLBEING_GOOGLE=""
  fi;
fi;

ui_print() {
  echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
  echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
}

# Setup busybox
bb="$TMP/busybox-arm"
l="$TMP/bin"

# Extract zip files, according to variable $ZIPTYPE
if [ "$ZIPTYPE" == "addon" ]; then
  for f in busybox-arm installer.sh; do
    unzip -o "$ZIPFILE" "$f" -d "$TMP";
  done
  for f in busybox-arm installer.sh; do
    chmod +x "$TMP/$f";
  done
else
  for f in addon.sh busybox-arm curl data.prop g.prop init.bootlog.rc installer.sh pm.sh sqlite3 zipalign; do
    unzip -o "$ZIPFILE" "$f" -d "$TMP";
  done
  for f in busybox-arm installer.sh zipalign; do
    chmod +x "$TMP/$f";
  done
fi;

ui_print "***********************************************";
ui_print "Custom GApps Package : BiTGApps                ";
ui_print "Developed By         : TheHitMan               ";
ui_print "***********************************************";

ui_print " ";

ui_print "Installing toolbox";
if [ -e "$bb" ]; then
  install -d "$l"
  for i in $($bb --list); do
    if ! ln -sf "$bb" "$l/$i" && ! $bb ln -sf "$bb" "$l/$i" && ! $bb ln -f "$bb" "$l/$i" ; then
      # create script wrapper if symlinking and hardlinking failed because of restrictive selinux policy
      if ! echo "#!$bb" > "$l/$i" || ! chmod +x "$l/$i" ; then
        ui_print "ERROR 10: Failed to set-up pre-bundled busybox";
        exit 1
      fi;
    fi;
  done
else
  ui_print "No match found !";
  exit 1
fi;

ui_print " ";

PATH="$l:$PATH" $bb ash "$TMP/installer.sh" "$@" || exit "$?"
