#!/bin/bash

# ${after_hook_sh} ${update_zip} ${system_rw_dir} ${tmp_dir} ${arc_mode}


update_zip=$1
system_rw_dir=$2
tmp_dir=$3
arc_mode=$4
is_arcvm() {
  [ "$arc_mode" == "arcvm" ]
}

root_uid=655360
root_gid=655360
if is_arcvm; then
  root_uid=0
  root_gid=0
fi

sudo install -o $root_uid -g $root_gid -d ${system_rw_dir}/system/usr/share/opengapps
sudo setfilecon u:object_r:system_file:s0 ${system_rw_dir}/system/usr/share/opengapps

sudo echo '#!/system/bin/sh

pm grant com.android.vending android.permission.WRITE_EXTERNAL_STORAGE
pm grant com.android.vending android.permission.READ_SMS
pm grant com.android.vending android.permission.RECEIVE_SMS
pm grant com.android.vending android.permission.READ_EXTERNAL_STORAGE
pm grant com.android.vending android.permission.ACCESS_COARSE_LOCATION
pm grant com.android.vending android.permission.READ_PHONE_STATE
pm grant com.android.vending android.permission.SEND_SMS
pm grant com.android.vending android.permission.WRITE_EXTERNAL_STORAGE
pm grant com.android.vending android.permission.READ_CONTACTS

' > ${tmp_dir}/patch_opengapps.sh

if ! is_arcvm; then
  echo '
counter=200
while [[ ! $counter -eq 0 ]];
do
  sleep 0.2
  pm disable "org.chromium.arc.applauncher/org.chromium.arc.applauncher.ChromeOsAccountManagerActivity"
  pm disable "com.google.android.setupwizard"
  ((counter=$counter-1))
done
' >> ${tmp_dir}/patch_opengapps.sh
else
  echo '
pm disable com.google.android.setupwizard
' >> ${tmp_dir}/patch_opengapps.sh
fi

sudo install -o $root_uid -g $root_gid ${tmp_dir}/patch_opengapps.sh ${system_rw_dir}/system/usr/share/opengapps/
sudo setfilecon u:object_r:system_file:s0 ${system_rw_dir}/system/usr/share/opengapps/patch_opengapps.sh
sudo rm -f ${tmp_dir}/patch_opengapps.sh

sudo grep "service patch_opengapps" ${system_rw_dir}/init.fydeos.rc
if [ ! $? -eq 0 ]; then

	sudo echo '
# >>> Configure Open GApps

service patch_opengapps /system/usr/share/opengapps/patch_opengapps.sh
	disabled
	user root
	seclabel u:r:shell:s0
	oneshot

on property:sys.boot_completed=1
	start patch_opengapps

# <<< Configure Open GApps

	' | sudo tee -a ${system_rw_dir}/init.fydeos.rc

fi


