のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

pa_gapps-modular-pico-5.1-20150315-signedのupdater-scriptのbin

pa_gapps-modular-pico-5.1-20150315-signedのupdater-scriptのbin

#!/sbin/sh
#
# PA GApps 2.0 Shell Script Installer
# Created by @TKruzze with MAJOR script contributions by @osm0sis
#
# Last Updated: 2015-03-14
unzip -o "$3" installer.data sizes.prop g.prop gapps-remove.txt bkup_tail.sh -d /tmp;
. /tmp/installer.data;
. /tmp/sizes.prop;
# _____________________________________________________________________________________________________________________
#                                                  Declare Variables
ZIP="$3";
zip_folder="$(dirname "$ZIP")";
OUTFD=/proc/self/fd/$2;

g_prop=/system/etc/g.prop;
b_prop=/system/build.prop;
bkup_tail=/tmp/bkup_tail.sh;
gapps_removal_list=/tmp/gapps-remove.txt
g_log=/tmp/g.log;
calc_log=/tmp/calc.log;
conflicts_log=/tmp/conflicts.log;
rec_cache_log=/cache/recovery/log;
rec_tmp_log=/tmp/recovery.log;

log_close="# End PA GApps Install Log\n";

reclaimed_gapps_space_kb=0;
reclaimed_removal_space_kb=0;
reclaimed_aosp_space_kb=0;
total_install_size_kb=0;
# _____________________________________________________________________________________________________________________
#                                                  Define Functions
abort() {
    quit;
    ui_print "- NO changes were made to your device";
    ui_print " ";
    ui_print "Installer will now exit...";
    ui_print " ";
    ui_print "Error Code: $1";
    sleep 5;
    exxit $1;
}

ch_con() {
  LD_LIBRARY_PATH=/system/lib /system/toolbox chcon u:object_r:system_file:s0 $1;
  LD_LIBRARY_PATH=/system/lib /system/bin/toolbox chcon u:object_r:system_file:s0 $1;
  chcon u:object_r:system_file:s0 $1;
}

ch_con_recursive() {
    dirs=$(echo $* | awk '{ print substr($0, index($0,$1)) }');
    for i in $dirs; do
        find "$i" -exec LD_LIBRARY_PATH=/system/lib /system/toolbox chcon u:object_r:system_file:s0 {} +;
        find "$i" -exec LD_LIBRARY_PATH=/system/lib /system/bin/toolbox chcon u:object_r:system_file:s0 {} +;
        find "$i" -exec chcon u:object_r:system_file:s0 {} +;
    done;
}

complete_gapps_list() {
cat <<EOF
$full_removal_list
EOF
}

contains() {
    case "$1" in
        *"$2"* ) return 0;;
        * ) return 1;;
    esac;
}

clean_inst() {
    if [ -f /data/system/packages.xml ]; then
        return 1;
    fi;
    return 0;
}

exxit() {
    set_progress 0.98;
    if ( ! grep -qi "nodebug" "$g_conf" ); then
        if [ "$g_conf" ]; then # copy gapps-config files to debug logs folder
            cp -f "$g_conf_orig" /tmp/logs/gapps-config_original.txt;
            cp -f "$g_conf" /tmp/logs/gapps-config_processed.txt;
        fi;
        ls -alZR /system > /tmp/logs/System_Files_After.txt;
        df -k > /tmp/logs/Device_Space_After.txt;
        cp -f "$log_folder/pa_gapps_log.txt" /tmp/logs;
        cp -f $b_prop /tmp/logs;
        cp -f /system/addon.d/70-gapps.sh /tmp/logs;
        cp -f $gapps_removal_list "/tmp/logs/gapps-remove(revised).txt";
        cp -f $rec_cache_log /tmp/logs/Recovery_cache.log;
        cp -f $rec_tmp_log /tmp/logs/Recovery_tmp.log;
        cd /tmp/logs;
        tar -cz -f "$log_folder/pa_gapps_debug_logs.tar.gz" *;
        cd /;
    fi;
    rm -rf /tmp/*;
    set_progress 1.0;
    ui_print "- Unmounting /system, /data, /cache";
    ui_print " ";
    umount /system;
    umount /data;
    umount /cache;
    exit $1;
}

file_getprop() {
    grep "^$2" "$1" | cut -d= -f2;
}

folder_extract() {
    unzip -o "$ZIP" "$1/$2/*" -d /tmp;
    bkup_list=$'\n'"$(find /tmp/$1/$2 -type f | cut -d/ -f5-)${bkup_list}";
    cp -rf "/tmp/$1/$2/." /system/;
    rm -rf "/tmp/$1";
}

log() {
    printf "%30s | %s\n" "$1" "$2" >> $g_log;
}

log_add() {
    printf "%7s | %18s | + %7d | %7d\n" "$1" "$2" "$3" "$4">> $calc_log;
}

log_sub() {
    printf "%7s | %18s | - %7d | %7d\n" "$1" "$2" "$3" "$4">> $calc_log;
}

obsolete_gapps_list() {
cat <<EOF
$remove_list
EOF
}

quit() {
    set_progress 0.94;
    install_note=$(echo "${install_note}" | sort -r | sed '/^$/d'); # sort Installation Notes & remove empty lines
    echo -------------------------------------------------------------------------------- >> $g_log;
    echo -e "$log_close" >> $g_log;

    # Add Installation Notes to log to help user better understand conflicts/errors
    for note in $install_note; do
        eval "error_msg=\$${note}";
        echo -e "$error_msg" >> $g_log;
    done;

    # Add Duplicate Files Log if it exists
    if [ -r $conflicts_log ]; then
        echo -e "$del_conflict_msg" >> $g_log;
        echo "# Begin GApps <> ROM Duplicate File List" >> $g_log;
        cat $conflicts_log >> $g_log;
        rm -f $conflicts_log;
        echo -e "# End GApps <> ROM Duplicate File List\n" >> $g_log;
    fi;

    # Add Installation Calculations to the log if they were performed
    if [ -r $calc_log ]; then
        echo "# Begin GApps Size Calculations" >> $g_log;
        cat $calc_log >> $g_log;
        rm -f $calc_log;
        echo -e "\n# End GApps Size Calculations" >> $g_log;
    fi;

    set_progress 0.96;
    # Add gapps-config information to the log
    echo -e "\n# Begin User's gapps-config" >> $g_log;
    if [ "$g_conf" ]; then
        cat "$g_conf" >> $g_log;
    else
        echo -n "   *** NOT USED ***" >> $g_log;
    fi;
    echo -e "\n# End User's gapps-config" >> $g_log;

    # Copy logs to proper folder (Same as gapps-config or same as Zip)
    ui_print "- Copying Log to $log_folder";
    ui_print " ";
    cp -f $g_log "$log_folder/pa_gapps_log.txt";
    rm -f $g_log;
    set_progress 0.97;
}

set_perm() {
  chown $1.$2 $4;
  chown $1:$2 $4;
  chmod $3 $4;
}

set_perm_recursive() {
    dirs=$(echo $* | awk '{ print substr($0, index($0,$5)) }');
    for i in $dirs; do
        chown -R $1.$2 $i; chown -R $1:$2 $i;
        find "$i" -type d -exec chmod $3 {} +;
        find "$i" -type f -exec chmod $4 {} +;
    done;
}

set_progress() { echo "set_progress $1" > $OUTFD; }

sys_app() {
    if ( grep -q "codePath=\"/system/app/$1" /data/system/packages.xml ); then
        return 0;
    fi;
    return 1;
}

ui_print() {
    echo -ne "ui_print $1\n" > $OUTFD;
    echo -ne "ui_print\n" > $OUTFD;
}
# _____________________________________________________________________________________________________________________
#                                                  Gather Pre-Install Info
# Get GApps Version and GApps Type from g.prop extracted at top of script
gapps_version=$(file_getprop /tmp/g.prop ro.addon.pa_version);
gapps_type=$(file_getprop /tmp/g.prop ro.addon.pa_type);
# _____________________________________________________________________________________________________________________
#                                                  Begin GApps Installation
ui_print " ";
ui_print "#########################################";
ui_print " ___   _      ___    _                   ";
ui_print "| _ \ /_\    / __|  /_\   _ __  _ __  ___";
ui_print "|  _// _ \  | (_ | / _ \ | '_ \| '_ \(_-<";
ui_print "|_| /_/ \_\  \___|/_/ \_\| .__/| .__//__/";
ui_print "                         |_|   |_|       ";
ui_print "#########################################";
ui_print " ";
ui_print "$installer_name$gapps_version";
ui_print " ";
ui_print "- Mounting /system, /data, /cache";
ui_print " ";
set_progress 0.01;
busybox mount /system;
busybox mount /data;
busybox mount /cache;
busybox mount -o rw,remount /system;
# _____________________________________________________________________________________________________________________
#                                                  Gather Device & GApps Package Information
# Locate gapps-config (if used)
for i in "$zip_folder/.gapps-config" "$zip_folder/gapps-config.txt" /sdcard/PA-GApps/.gapps-config /sdcard/PA-GApps/gapps-config.txt "$zip_folder/.gapps-config.txt" /sdcard/PA-GApps/.gapps-config.txt; do
    if [ -r "$i" ]; then
        g_conf="$i";
        break;
    fi;
done;
if [ "$g_conf" ]; then
    config_file="$g_conf";
    log_folder="$(dirname "$g_conf")";
    sed -e 's|#.*||g' -e 's/\r//g' -e '/^$/d'  "$g_conf" > /tmp/proc_gconf;
    g_conf_orig=$g_conf;
    g_conf=/tmp/proc_gconf;
else
    config_file="Not Used";
    log_folder="$zip_folder";
fi;

# Unless this is a NoDebug install - create folder and take 'Before' snapshots
if ( ! grep -qi "nodebug" "$g_conf" ); then
    mkdir /tmp/logs;
    ls -alZR /system > /tmp/logs/System_Files_Before.txt;
    df -k > /tmp/logs/Device_Space_Before.txt;
fi;

# Get ROM android version from build.prop
ui_print "- Gathering device & ROM information";
ui_print " ";
rom_android_version=$(file_getprop $b_prop ro.build.version.release);

# Get device name any which way we can
for field in ro.product.device ro.build.product; do
    for file in $b_prop /default.prop; do
        device_name=$(file_getprop $file $field);
        if [ ${#device_name} -ge 2 ]; then
            break 2;
        fi;
    done;
    device_name="Bad ROM/Recovery";
done;

# Get Device Type (phone or tablet) from build.prop
if echo "$(file_getprop $b_prop ro.build.characteristics)" | grep -qi "tablet"; then
    device_type=tablet;
else
    device_type=phone;
fi;

# Get Rom Version from build.prop
for field in ro.modversion ro.build.version.incremental; do
    rom_version=$(file_getprop $b_prop $field);
    if [ ${#rom_version} -ge 2 ]; then
        break;
    fi;
    rom_version="non-standard build.prop";
done;

echo "# Begin PA GApps Install Log" > $g_log;
echo -------------------------------------------------------------------------------- >> $g_log;
log "ROM Android Version" $rom_android_version;

# Check to make certain user has proper version ROM Installed
if [ ! ${rom_android_version:0:3} == $req_android_version ]; then
    ui_print "*** Incompatible Android ROM detected ***";
    ui_print " ";
    ui_print "This GApps pkg is for Android $req_android_version.x ONLY";
    ui_print " ";
    ui_print "******* GApps Installation failed *******";
    ui_print " ";
    install_note="${install_note}rom_version_msg"$'\n'; # make note that ROM Version is not compatible with these GApps
    abort $E_ROMVER;
fi;

# Check to make certain that user is not using a 64-bit device
if echo "$(file_getprop $b_prop ro.product.cpu.abilist64)" | grep -qi "arm64"; then
    ui_print "***** Incompatible Device Detected *****";
    ui_print " ";
    ui_print "PA GApps can ONLY be installed on 32-bit";
    ui_print "devices. Your device has been detected";
    ui_print "as a 64-bit device. You will need to";
    ui_print "find a 64-bit compatible GApps package.";
    ui_print " ";
    ui_print "******* GApps Installation failed *******";
    ui_print " ";
    install_note="${install_note}64bit_compat_msg"$'\n'; # make note that PA GApps are not 64-bit compatible
    abort $E_64BIT;
fi;

# Determine Recovery Type and Version
for rec_log in $rec_tmp_log $rec_cache_log; do
    recovery=$(busybox grep -m 2 -E " Recovery v|Starting TWRP|Welcome to|PhilZ" $rec_log);
    case "$recovery" in
        *Welcome*) recovery="$(grep -m 1 "Welcome to" $rec_log | awk '{ print substr($0, index($0,$3)) }')$(grep -m 1 "^ext.version" $rec_log | cut -d\" -f2)"; break;;
        *Recovery*) recovery=$(grep -m 1 "Recovery v" $rec_log); recovery=${recovery/Recovery v/Recovery }; break;;
        *PhilZ*) recovery=$(grep -m 2 -E "PhilZ|ClockworkMod" $rec_log); recovery="${recovery/ClockworkMod v/(ClockworkMod })"; break;;
        Starting*) recovery=$(echo $recovery | awk -F"Starting " '{ print $2 }' | awk -F" on " '{ print $1 }'); break;;
    esac;
done;

# Get display density using getprop from Recovery
density=$(getprop ro.sf.lcd_density);

# If the density returned by getprop is empty or non-standard - read from default.prop instead
case $density in
    160|240|320|480) ;;
    *) density=$(file_getprop /default.prop ro.sf.lcd_density);;
esac;

# If the density from default.prop is still empty or non-standard - read from build.prop instead
case $density in
    160|240|320|480) ;;
    *) density=$(file_getprop $b_prop ro.sf.lcd_density);;
esac;

case $density in
    160) gms=0;; # should be 2  remove in pico(uni)
    240) gms=4;; #              remove in pico(uni)
    320) gms=6;; #              remove in pico(uni)
    480) gms=8;; #              remove in pico(uni)
      *) gms=0;;
esac;

# Set density to unknown if it's still empty
test -z $density && density=unknown;

# Remove any files from gapps-list.txt that should not be processed for automatic removal
for bypass_file in $removal_bypass_list; do
    sed -i "\:${bypass_file}:d" $gapps_removal_list;
done;

# Is this a 'Clean' or 'Dirty' install
if ( clean_inst ); then
    install_type="Clean[Data Wiped]";
    cameragoogle_inst=Clean;
    keyboardgoogle_inst=Clean;
else
    install_type="Dirty[Data NOT Wiped]";

    # Was Google Camera previously installed (in /system)
    if ( sys_app GoogleCamera ); then
        cameragoogle_inst=true;
    else
        cameragoogle_inst=false;
    fi;

    # Was Google Keyboard previously installed (in /system)
    if ( sys_app LatinImeGoogle ); then
        keyboardgoogle_inst=true;
    else
        keyboardgoogle_inst=false;
    fi;
fi;

# Is device FaceUnlock compatible
if ( ! grep -qE "Victory|herring|sun4i" /proc/cpuinfo ) && [ -e /system/etc/permissions/android.hardware.camera.front.xml ]; then
    faceunlock_compat=true;
else
    faceunlock_compat=false;
fi;

# Check device name for devices that are incompatible with Google Camera
# bacon or A0001=OnePlus One | find7=Oppo Find7 and Find7a
case $device_name in
    A0001|bacon|find7) cameragoogle_compat=false;;
    *) cameragoogle_compat=true;;
esac;

log "ROM ID" "$(file_getprop $b_prop ro.build.display.id)";
log "ROM Version" "$rom_version";
log "Device Recovery" "$recovery";
log "Device Name" "$device_name";
log "Device Model" "$(file_getprop $b_prop ro.product.model)";
log "Device Type" "$device_type";
log "Device CPU" "$(file_getprop $b_prop ro.product.cpu.abilist32)";
log "getprop Density" "$(getprop ro.sf.lcd_density)";
log "default.prop Density" "$(file_getprop /default.prop ro.sf.lcd_density)";
log "build.prop Density" "$(file_getprop $b_prop ro.sf.lcd_density)";
log "Display Density Used" "${density}ppi";
log "Install Type" "$install_type";
log "Google Camera Installedケ" "$cameragoogle_inst";
log "Google Keyboard Installedケ" "$keyboardgoogle_inst";
log "FaceUnlock Compatible" "$faceunlock_compat";
log "Google Camera Compatible" "$cameragoogle_compat";
log_close="                  ケ Previously installed with PA GApps\n$log_close";

# Determine if a GApps package is installed and
# the version, type, and whether it's a PA GApps package
if [ -e /system/priv-app/GoogleServicesFramework/GoogleServicesFramework.apk -a -e /system/priv-app/GoogleLoginService/GoogleLoginService.apk ]; then
    if [ -n "$(grep ro.addon.pa_version $g_prop)" ]; then
        log "Current GApps Version" "$(file_getprop $g_prop ro.addon.pa_version)";
        if [ -n "$(grep ro.addon.pa_type $g_prop)" ]; then
            log "Curent PA GApps Package" "$(file_getprop $g_prop ro.addon.pa_type)";
        else
            log "Curent PA GApps Package" "Unknown";
        fi;
    else
        log "Current GApps Version" "NON PA GApps Currently Installed (FAILURE)";
        ui_print "* Incompatible GApps Currently Installed *";
        ui_print " ";
        ui_print "PA GApps can ONLY be installed on top of";
        ui_print "an existing installation of PA GApps. You";
        ui_print "must wipe (format) your system partition";
        ui_print "BEFORE installing any PA GApps package.";
        ui_print " ";
        ui_print "See FAQ #6 in XDA thread for more details";
        ui_print " ";
        ui_print "******* GApps Installation failed *******";
        ui_print " ";
        install_note="${install_note}non_pa_gapps_msg"$'\n'; # make note that currently installed GApps are non-PA
        abort $E_NONPA;
    fi;
else
    # User does NOT have a GApps package installed on their device
    log "Current GApps Version" "NO GApps Installed";
    log "Curent PA GApps Package" "NO GApps Installed";

    # Use the opportunity of No GApps installed to check for potential ROM conflicts when deleting existing GApps files
    while read gapps_file; do
        if [ -e $gapps_file ]; then
            echo $gapps_file >> $conflicts_log;
        fi;
    done < $gapps_removal_list;
fi;
# _____________________________________________________________________________________________________________________
#                                                  Prepare the list of GApps being installed and AOSP/Stock apps being removed
# Build list of available GApps that can be installed (and check for a user package preset)
for pkg in $pkg_names; do
    eval "addto=\$${pkg}_gapps_list"; # Look for method to combine this with line below
    all_gapps_list=${all_gapps_list}${addto}; # Look for method to combine this with line above
    if ( grep -qi "${pkg}gapps" "$g_conf" ); then # user has selected a 'preset' install
        gapps_type=$pkg;
        sed -i "/ro.addon.pa_type/c\ro.addon.pa_type=$pkg" /tmp/g.prop; # modify g.prop to new package type
        break;
    fi;
done;

# Prepare list of User specified GApps that will be installed
if [ "$g_conf" ]; then
    if ( grep -qi "include" "$g_conf" ); then # User is indicating the apps they WANT installed
        config_type=include;
        for gapp_name in $all_gapps_list; do
            if ( grep -qi "$gapp_name" "$g_conf" ); then
                gapps_list="$gapps_list$gapp_name"$'\n';
            fi;
        done;
    else # User is indicating the apps they DO NOT WANT installed
        config_type=exclude;
        for gapp_name in $all_gapps_list; do
            if ( ! grep -qi "$gapp_name" "$g_conf" ); then
                gapps_list="$gapps_list$gapp_name"$'\n';
            fi;
        done;
    fi;
else # User is not using a gapps-config and we're doing the 'full monty'
    config_type=[Default];
    gapps_list=$all_gapps_list;
fi;

# Configure default removal of Stock/AOSP apps - if we're installing Stock GApps
if [ $gapps_type = "stock" ]; then
    remove_browser=true[default];
    remove_email=true[default];
    remove_gallery=true[default];
    remove_launcher=true[default];
    remove_mms=true[default];
    remove_picotts=true[default];
else
    # Do not perform any default removals - but make them optional
    optional_aosp_remove_list=${default_aosp_remove_list}${optional_aosp_remove_list};
    unset default_aosp_remove_list;
    remove_browser=false[default];
    remove_email=false[default];
    remove_gallery=false[default];
    remove_launcher=false[default];
    remove_mms=false[default];
    remove_picotts=false[default];
fi;

# Prepare list of AOSP/ROM files that will be deleted using gapps-config
# Since Stock GApps has default removal of Browser, Launcher, MMS, and PicoTTS
# we will look for +Browser, +Launcher, +MMS, and +PicoTTS to override
set_progress 0.03;
if [ "$g_conf" ]; then
    for default_name in $default_aosp_remove_list; do
        if ( grep -qi "+$default_name" "$g_conf" ); then
            eval "remove_${default_name}=false";
        else
            if ( grep -qi "$default_name" "$g_conf" ); then
                eval "remove_${default_name}=true";
            fi;
            aosp_remove_list="$aosp_remove_list$default_name"$'\n';
        fi;
    done;
    # Check gapps-config for other optional AOSP/ROM files that will be deleted
    for opt_name in $optional_aosp_remove_list; do
        if ( grep -qi "$opt_name" "$g_conf" ); then
            aosp_remove_list="$aosp_remove_list$opt_name"$'\n';
        fi;
    done;
else
    aosp_remove_list=$default_aosp_remove_list;
fi;

# Verify device is FaceUnlock compatible BEFORE we allow it in $gapps_list
if ( contains "$gapps_list" "faceunlock" ) && [ $faceunlock_compat = "false" ]; then
    gapps_list=${gapps_list/faceunlock};
    install_note="${install_note}faceunlock_msg"$'\n'; # make note that FaceUnlock will NOT be installed as user requested
fi;

# If we're NOT installing chrome make certain 'browser' is NOT in $aosp_remove_list UNLESS 'browser' is in $g_conf
if ( ! contains "$gapps_list" "chrome" ) && ( ! grep -qi "browser" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/browser};
    remove_browser=false[default];
fi;

# Add bksync to $gapps_list IF $config_type != "include" AND $g_conf does NOT contain bksync AND we're NOT removing 'browser'
#if ( ! contains "$aosp_remove_list" "browser" ) && [ $config_type != "include" ] && ( ! grep -qi "bksync" "$g_conf" ); then
#    gapps_list="${gapps_list}bksync"$'\n';
#fi;

# If we're NOT installing gmail make certain 'email' is NOT in $aosp_remove_list UNLESS 'email' is in $g_conf
if ( ! contains "$gapps_list" "gmail" ) && ( ! grep -qi "email" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/email};
    remove_email=false[default];
fi;

# If we're NOT installing googleplus make certain 'gallery' is NOT in $aosp_remove_list UNLESS 'gallery' is in $g_conf
if ( ! contains "$gapps_list" "googleplus" ) && ( ! grep -qi "gallery" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/gallery};
    remove_gallery=false[default];
fi;

# If $device_type is 'tablet' make certain we're not installing messenger
if ( contains "$gapps_list" "messenger" ) && [ $device_type = "tablet" ]; then
    gapps_list=${gapps_list/messenger}; # we'll prevent messenger from being installed since this isn't a phone
fi;

# If we're NOT installing hangouts or messenger make certain 'mms' is NOT in $aosp_remove_list UNLESS 'mms' is in $g_conf
if ( ! contains "$gapps_list" "hangouts" )  && ( ! contains "$gapps_list" "messenger" ) && ( ! grep -qi "mms" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/mms};
    remove_mms=false[default];
fi;

# If we're NOT installing hangouts or messenger and mms is in $aosp_remove_list then user must override removal protection
if ( ! contains "$gapps_list" "hangouts" ) && ( ! contains "$gapps_list" "messenger" ) && ( contains "$aosp_remove_list" "mms" ) && ( ! grep -qi "override" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/mms}; # we'll prevent mms from being removed so user isn't left with no way to receive text messages
    install_note="${install_note}nomms_msg"$'\n'; # make note that MMS can't be removed unless user Overrides
fi;

# If we're NOT installing googletts make certain 'picotts' is NOT in $aosp_remove_list UNLESS 'picotts' is in $g_conf
if ( ! contains "$gapps_list" "googletts" ) && ( ! grep -qi "picotts" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/picotts};
    remove_picotts=false[default];
fi;

# If we're NOT installing search then we MUST REMOVE googlenow from  $gapps_list (if it's currently there)
if ( ! contains "$gapps_list" "search" ) && ( contains "$gapps_list" "googlenow" ); then
    gapps_list=${gapps_list/googlenow};
    install_note="${install_note}googlenow_msg"$'\n'; # make note that Google Now Launcher will NOT be installed as user requested
fi;

# If we're NOT installing googlenow make certain 'launcher' is NOT in $aosp_remove_list UNLESS 'launcher' is in $g_conf
if ( ! contains "$gapps_list" "googlenow" ) && ( ! grep -qi "launcher" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/launcher};
    remove_launcher=false[default];
fi;

# If we're NOT installing googlenow and launcher is in $aosp_remove_list then user must override removal protection
if ( ! contains "$gapps_list" "googlenow" ) && ( contains "$aosp_remove_list" "launcher" ) && ( ! grep -qi "override" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/launcher}; # we'll prevent launcher from being removed so user isn't left with no Launcher
    install_note="${install_note}nolauncher_msg"$'\n'; # make note that Launcher can't be removed unless user Overrides
fi;

# If we're installing calendargoogle we must ADD calendarstock to $aosp_remove_list (if it's not already there)
if ( contains "$gapps_list" "calendargoogle" ) && ( ! contains "$aosp_remove_list" "calendarstock" ); then
    aosp_remove_list="${aosp_remove_list}calendarstock"$'\n';
fi;

# If we're NOT installing calendargoogle we will ADD calsync to $gapps_list IF $config_type != "include" AND $g_conf does NOT contain calsync
if ( ! contains "$gapps_list" "calendargoogle" ) && [ $config_type != "include" ] && ( ! grep -qi "calsync" "$g_conf" ); then
    gapps_list="${gapps_list}calsync"$'\n';
fi;

# Add bksync or calsync to $gapps_list IF $config_type = "include" AND $g_conf contain bksync or calsync
#for i in bksync calsync; do
#    if [ $config_type = "include" ] && ( grep -qi "$i" "$g_conf" ); then
#        gapps_list="${gapps_list}$i"$'\n';
#    fi;
#done;
# REMOVE 3 LINES BELOW ONCE bksync HAS BEEN ADDED BACK
if [ $config_type = "include" ] && ( grep -qi "calsync" "$g_conf" ); then
    gapps_list="${gapps_list}calsync"$'\n';
fi;

# If user wants to install keyboardgoogle then it MUST be a Clean Install OR keyboardgoogle was previously installed in system partition
if ( contains "$gapps_list" "keyboardgoogle" ) && ( ! clean_inst ) && [ $keyboardgoogle_inst = "false" ]; then
    gapps_list=${gapps_list/keyboardgoogle}; # we must DISALLOW keyboardgoogle from being installed
    aosp_remove_list=${aosp_remove_list/keyboardstock}; # and we'll prevent keyboardstock from being removed so user isn't left with no keyboard
    install_note="${install_note}keyboard_sys_msg"$'\n'; # make note that Google Keyboard will NOT be installed as user requested
fi;

# If we're installing keyboardgoogle we must ADD keyboardstock to $aosp_remove_list (if it's not already there)
if ( contains "$gapps_list" "keyboardgoogle" ) && ( ! contains "$aosp_remove_list" "keyboardstock" ); then
    aosp_remove_list="${aosp_remove_list}keyboardstock"$'\n';
fi;

# If we're NOT installing keyboardgoogle and keyboardstock is in $aosp_remove_list then user must override removal protection
if ( ! contains "$gapps_list" "keyboardgoogle" ) && ( contains "$aosp_remove_list" "keyboardstock" ) && ( ! grep -qi "override" "$g_conf" ); then
    aosp_remove_list=${aosp_remove_list/keyboardstock}; # we'll prevent keyboardstock from being removed so user isn't left with no keyboard
    install_note="${install_note}nokeyboard_msg"$'\n'; # make note that Stock Keyboard can't be removed unless user Overrides
fi;

# Verify device is Google Camera compatible BEFORE we allow it in $gapps_list
if ( contains "$gapps_list" "cameragoogle" ) && [ $cameragoogle_compat = "false" ]; then
    gapps_list=${gapps_list/cameragoogle}; # we must DISALLOW cameragoogle from being installed
    install_note="${install_note}camera_compat_msg"$'\n'; # make note that Google Camera will NOT be installed as user requested
fi;

# If user wants to install cameragoogle then it MUST be a Clean Install OR cameragoogle was previously installed in system partition
if ( contains "$gapps_list" "cameragoogle" ) && ( ! clean_inst ) && [ $cameragoogle_inst = "false" ]; then
    gapps_list=${gapps_list/cameragoogle}; # we must DISALLOW cameragoogle from being installed
    aosp_remove_list=${aosp_remove_list/camerastock}; # and we'll prevent camerastock from being removed so user isn't left with no camera
    install_note="${install_note}camera_sys_msg"$'\n'; # make note that Google Camera will NOT be installed as user requested
fi;

# If we're installing cameragoogle we MUST ADD camerastock to $aosp_remove_list (if it's not already there)
if ( contains "$gapps_list" "cameragoogle" ) && ( ! contains "$aosp_remove_list" "camerastock" ); then
    aosp_remove_list="${aosp_remove_list}camerastock"$'\n';
fi;

# If we're installing exchangegoogle we must ADD exchangestock to $aosp_remove_list (if it's not already there)
if ( contains "$gapps_list" "exchangegoogle" ) && ( ! contains "$aosp_remove_list" "exchangestock" ); then
    aosp_remove_list="${aosp_remove_list}exchangestock"$'\n';
fi;

# Read in gapps removal list from file
full_removal_list=$(cat $gapps_removal_list);

# Clean up and sort our lists for space calculations and installation
set_progress 0.04;
gapps_list=$(echo "${gapps_list}" | sort | sed '/^$/d'); # sort GApps list & remove empty lines
aosp_remove_list=$(echo "${aosp_remove_list}" | sort | sed '/^$/d'); # sort AOSP Remove list & remove empty lines
full_removal_list=$(echo "${full_removal_list}" | sed '/^$/d'); # Remove empty lines from  FINAL GApps Removal list
remove_list=$(echo "${remove_list}" | sed '/^$/d'); # Remove empty lines from remove_list

log "Installing GApps Version" $gapps_version;
log "Installing GApps Type" $gapps_type;
log "Config Type" $config_type;
log "Using gapps-config" "$config_file";
log "Remove Stock/AOSP Browser" $remove_browser;
log "Remove Stock/AOSP Email" $remove_email;
log "Remove Stock/AOSP Gallery" $remove_gallery;
log "Remove Stock/AOSP Launcher" $remove_launcher;
log "Remove Stock/AOSP MMS App" $remove_mms;
log "Remove Stock/AOSP Pico TTS" $remove_picotts;
log "Installing Play Services" "$gms_base$gms)";
# _____________________________________________________________________________________________________________________
#                                                  Perform space calculations
ui_print "- Performing system space calculations";
ui_print " ";

# Perform calculations of device specific applications
eval "gms_size=\$gms_${gms}_size"; # Determine size of GMSCore
eval "playgames_size=\$pg_${gms}_size"; # Determine size of PlayGames
eval "setupwiz_size=\$setupwiz_${device_type}_size"; # Determine size of SetupWizard

# Determine final size of Core Apps
if ( ! contains "$gapps_list" "keyboardgoogle" ); then
    core_size=$((core_size + setupwiz_size + keybd_lib_size)); # Add SetupWizard & Keyboard Lib size to core
else
    core_size=$((core_size + setupwiz_size)); # Add SetupWizard size to core
fi;

# Read and save system partition size details
df=$(busybox df -k /system | tail -n 1);
case $df in
    /dev/block/*) df=$(echo $df | awk '{ print substr($0, index($0,$2)) }');;
esac;
total_system_size_kb=$(echo $df | awk '{ print $1 }');
used_system_size_kb=$(echo $df | awk '{ print $2 }');
free_system_size_kb=$(echo $df | awk '{ print $3 }');
log "Total System Size (KB)" $total_system_size_kb;
log "Used System Space (KB)" $used_system_size_kb;
log "Current Free Space (KB)" $free_system_size_kb;

# Perform storage space calculations of existing GApps that will be deleted/replaced
reclaimed_gapps_space_kb=$(du -ck `complete_gapps_list` | tail -n1 | awk '{ print $1 }');

# Perform storage space calculations of other Removals that need to be deleted (Obsolete and Conflicting Apps)
set_progress 0.05;
reclaimed_removal_space_kb=$(du -ck `obsolete_gapps_list` | tail -n1 | awk '{ print $1 }');

# Add information to calc.log that will later be added to pa_gapps.log to assist user with app removals
post_install_size_kb=$((free_system_size_kb + reclaimed_gapps_space_kb)); # Add opening calculations
echo -------------------------------------------------- > $calc_log;
printf "%7s | %18s |   %7s | %7s\n" "TYPE " "DESCRIPTION   " "SIZE" "  TOTAL" >> $calc_log;
printf "%7s | %18s |   %7d | %7d\n" "" "Current Free Space" $free_system_size_kb $free_system_size_kb >> $calc_log;
printf "%7s | %18s | + %7d | %7d\n" "Remove" "Existing GApps" $reclaimed_gapps_space_kb $post_install_size_kb >> $calc_log;
post_install_size_kb=$((post_install_size_kb + reclaimed_removal_space_kb)); # Add reclaimed_removal_space_kb
printf "%7s | %18s | + %7d | %7d\n" "Remove" "Obsolete Files" $reclaimed_removal_space_kb $post_install_size_kb >> $calc_log;

# Perform calculations of AOSP/ROM files that will be deleted
set_progress 0.07;
for aosp_name in $aosp_remove_list; do
    eval "list_name=\$${aosp_name}_list";
    aosp_size_kb=0; # Reset counter
    for file_name in $list_name; do
        if [ -e /system/$file_name ]; then
            file_size_kb=$(du -ck /system/$file_name | tail -n1 | awk '{ print $1 }');
            aosp_size_kb=$((file_size_kb + aosp_size_kb));
            post_install_size_kb=$((post_install_size_kb + file_size_kb));
        fi;
    done;
    log_add "Remove" "$aosp_name" $aosp_size_kb $post_install_size_kb;
done;

# Perform calculations of GApps files that will be installed
set_progress 0.09;
post_install_size_kb=$((post_install_size_kb - core_size)); # Add Core GApps
log_sub "Install" "Coreイ" $core_size $post_install_size_kb;
post_install_size_kb=$((post_install_size_kb - gms_size)); # Add Google Play Services
log_sub "Install" "GMSCoreイ" $gms_size $post_install_size_kb;

for gapp_name in $gapps_list; do
    eval "gapp_size_kb=\$${gapp_name}_size"; # Determine size of GApp being installed
    post_install_size_kb=$((post_install_size_kb - gapp_size_kb));
    log_sub "Install" "$gapp_nameウ" $gapp_size_kb $post_install_size_kb;
done;

# Perform calculations of required Buffer Size
set_progress 0.11;
if ( grep -qi "smallbuffer" "$g_conf" ); then
    buffer_size_kb=$small_buffer_size;
fi;

post_install_size_kb=$((post_install_size_kb - buffer_size_kb));
log_sub "" "Buffer Spaceイ" $buffer_size_kb $post_install_size_kb;
echo -------------------------------------------------- >> $calc_log;

if [ "$post_install_size_kb" -ge 0 ]; then
    printf "%40s | %7d\n" "  Post Install Free Space" $post_install_size_kb >> $calc_log;
    log "Post Install Free Space (KB)" "$post_install_size_kb       << See Calculations Below";
else
    additional_size_kb=$((post_install_size_kb * -1));
    printf "%40s | %7d\n" "Additional Space Required" $additional_size_kb >> $calc_log;
    log "Additional Space Required (KB)" "$additional_size_kb       << See Calculations Below";
fi;

# Finish up Calculation Log
echo -------------------------------------------------- >> $calc_log;
echo "          イ Required (ALWAYS Installed)" >> $calc_log;
echo "          ウ Optional (may be removed)" >> $calc_log;

# Check whether there's enough free space to complete this installation
if [ "$post_install_size_kb" -lt 0 ]; then
    # We don't have enough system space to install everything user requested
    ui_print "Insufficient storage space available in";
    ui_print "System partition. You may want to use a";
    ui_print "smaller PA GApps package or consider";
    ui_print "removing some apps using gapps-config.";
    ui_print "See:'$log_folder/pa_gapps_log.txt'";
    ui_print "for complete details and information.";
    ui_print " ";
    install_note="${install_note}system_space_msg"$'\n'; # make note that there is insufficient space in system to install
    abort $E_NOSPACE;
fi;

# Check to see if this is the 'real thing' or only a test
if ( grep -qi "test" "$g_conf" ); then # user has selected a 'test' install ONLY
    ui_print "- Exiting Simulated Install";
    ui_print " ";
    install_note="${install_note}simulation_msg"$'\n'; # make note that this is only a test installation
    quit;
    exxit 0;
fi;
# _____________________________________________________________________________________________________________________
#                                                  Perform Removals
# Remove ALL Existing GApps files
set_progress 0.13;
ui_print "- Removing existing/obsolete Apps";
ui_print " ";
rm -rf $(complete_gapps_list);

# Remove Obsolete and Conflicting Apps
rm -rf $(obsolete_gapps_list);

# Remove Stock/AOSP Apps and add Removals to addon.d script
aosp_remove_list=$(echo "${aosp_remove_list}" | sort -r); # reverse sort list for more readable output
for aosp_name in $aosp_remove_list; do
    eval "list_name=\$${aosp_name}_list";
    list_name=$(echo "${list_name}" | sort -r); # reverse sort list for more readable output
    for file_name in $list_name; do
        rm -rf /system/$file_name;
        sed -i "\:# Remove Stock/AOSP apps (from GApps Installer):a \    rm -rf /system/$file_name" $bkup_tail;
    done;
done;

# Remove any empty folders we may have created during the removal process
for i in /system/app /system/priv-app /system/vendor/pittpatt /system/usr/srec /system/etc/preferred-apps; do
    find $i -type d | xargs rmdir -p --ignore-fail-on-non-empty;
done;
# _____________________________________________________________________________________________________________________
#                                                  Perform Installs
ui_print "- Installing updated GApps";
ui_print " ";
set_progress 0.15;
folder_extract Core required; # Install Core GApps
folder_extract SetupWizard $device_type; # Install SetupWizard
if ( ! contains "$gapps_list" "keyboardgoogle" ); then
    folder_extract Optional keybd_lib; # Install Keyboard lib to add swipe capabilities to AOSP Keyboard
    mkdir -p /system/app/LatinIME/lib/arm;
    ln -sf /system/lib/$keybd_lib_filename1 /system/lib/$keybd_lib_filename2; # create required symlink
    ln -sf /system/lib/$keybd_lib_filename1 /system/app/LatinIME/lib/arm/$keybd_lib_filename1; # create required symlink
    ln -sf /system/lib/$keybd_lib_filename1 /system/app/LatinIME/lib/arm/$keybd_lib_filename2; # create required symlink
    # Add same code to backup script to insure symlinks are recreated on addon.d restore
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    ln -sf /system/lib/$keybd_lib_filename1 /system/app/LatinIME/lib/arm/$keybd_lib_filename2" $bkup_tail;
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    ln -sf /system/lib/$keybd_lib_filename1 /system/app/LatinIME/lib/arm/$keybd_lib_filename1" $bkup_tail;
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    ln -sf /system/lib/$keybd_lib_filename1 /system/lib/$keybd_lib_filename2" $bkup_tail;
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    mkdir -p /system/app/LatinIME/lib/arm" $bkup_tail;
fi;
set_progress 0.20;
folder_extract GMSCore common; # Install Google Play Services libs
set_progress 0.25;
folder_extract GMSCore $gms; # Install Google Play Services apk

# Install PlayGames if it's in $gapps_list
if ( contains "$gapps_list" "playgames" ); then
    folder_extract PlayGames $gms; # Install Google PlayGames
    gapps_list=${gapps_list/playgames}; # remove PlayGames from gapps list since it's now installed
fi;

# Progress Bar increment calculations for GApps Install process
set_progress 0.30;
gapps_count=$(echo "${gapps_list}" | wc -w); # Count number of GApps left to be installed
if [ $gapps_count -lt 1 ]; then gapps_count=1; fi; # Prevent 'division by zero'
incr_amt=$(( 5000 / $gapps_count )); # Determine increment factor of progress bar during GApps installation
prog_bar=3000; # Set Progress Bar start point (0.3000) for below

# Install the rest of GApps still in $gapps_list
for gapp_name in $gapps_list; do
    folder_extract GApps $gapp_name; # Installing User Selected GApps
    prog_bar=$((prog_bar + incr_amt));
    set_progress 0.$prog_bar;
done;

# Create FaceLock lib symlink if FaceLock was installed
if ( contains "$gapps_list" "faceunlock" ); then
    mkdir -p /system/app/FaceLock/lib/arm;
    ln -sf /system/lib/$FaceLock_lib_filename /system/app/FaceLock/lib/arm/$FaceLock_lib_filename; # create required symlink
    # Add same code to backup script to insure symlinks are recreated on addon.d restore
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    ln -sf /system/lib/$FaceLock_lib_filename /system/app/FaceLock/lib/arm/$FaceLock_lib_filename" $bkup_tail;
    sed -i "\:# Recreate required symlinks (from GApps Installer):a \    mkdir -p /system/app/FaceLock/lib/arm" $bkup_tail;
fi;

# Copy g.prop over to /system/etc
cp -f /tmp/g.prop $g_prop;
# _____________________________________________________________________________________________________________________
#                                                  Build and Install Addon.d Backup Script
# Add 'other' Removals to addon.d script
set_progress 0.80;
other_list=$(echo "${other_list}" | sort -r); # reverse sort list for more readable output
for other_name in $other_list; do
    sed -i "\:# Remove 'other' apps (per installer.data):a \    rm -rf $other_name" $bkup_tail;
done;

# Add 'priv-app' Removals to addon.d script
privapp_list=$(echo "${privapp_list}" | sort -r); # reverse sort list for more readable output
for privapp_name in $privapp_list; do
    sed -i "\:# Remove 'priv-app' apps from 'app' (per installer.data):a \    rm -rf $privapp_name" $bkup_tail;
done;

# Add 'required' Removals to addon.d script
reqd_list=$(echo "${reqd_list}" | sort -r); # reverse sort list for more readable output
for reqdapp_name in $reqd_list; do
    sed -i "\:# Remove 'required' apps (per installer.data):a \    rm -rf $reqdapp_name" $bkup_tail;
done;

# Create final addon.d script in system
bkup_header="#!/sbin/sh\n# \n# /system/addon.d/70-gapps.sh\n#\n. /tmp/backuptool.functions\n\nlist_files() {\ncat <<EOF"
bkup_list="$bkup_list"$'\n'etc/g.prop; # add g.prop to backup list
bkup_list=$(echo "${bkup_list}" | sort -u| sed '/^$/d'); # sort list & remove duplicates and empty lines
mkdir -p /system/addon.d;
echo -e "$bkup_header" > /system/addon.d/70-gapps.sh;
echo -e "$bkup_list" >> /system/addon.d/70-gapps.sh;
cat $bkup_tail >> /system/addon.d/70-gapps.sh;
# _____________________________________________________________________________________________________________________
#                                                  Fix Permissions and say 'Goodnight Gracie'
set_progress 0.83;
ui_print "- Fixing permissions & contexts";
ui_print " ";
set_perm_recursive 0 0 755 644 "/system/app" "/system/framework" "/system/lib" "/system/priv-app" "/system/usr/srec/en-US" "/system/vendor/pittpatt" "/system/etc/permissions" "/system/etc/preferred-apps";

set_progress 0.85;
set_perm_recursive 0 0 755 755 "/system/addon.d";

set_progress 0.87;
find /system/vendor/pittpatt -type d -exec chown 0.2000 '{}' \; -exec chown 0:2000 '{}' \; # Change pittpatt folders to root:shell per Google Factory Settings

set_perm 0 0 644 $g_prop;

# Set contexts on all files we installed
set_progress 0.88;
ch_con_recursive "/system/app" "/system/framework" "/system/lib" "/system/priv-app" "/system/usr/srec/en-US" "/system/vendor/pittpatt" "/system/etc/permissions" "/system/etc/preferred-apps" "/system/addon.d";
ch_con $g_prop;

set_progress 0.92;
quit; # 'Goodnight Gracie'

ui_print "- Installation complete!";
ui_print " ";
exxit 0;