#!/bin/sh
#This file is part of The Open GApps script of @mfonville.
#
#    The Open GApps 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.
#

#Check architecture
if { [ "$1" != "arm" ] && [ "$1" != "arm64" ] && [ "$1" != "x86" ] && [ "$1" != "x86_64" ]; } || [ -z "$2" ]; then
  echo "Usage: $0 (arm|arm64|x86|x86_64) API_LEVEL [VARIANT]"
  exit 1
fi

#OPENGAPPSDATE=""  # this can be set to override the date
if [ -n "$OPENGAPPSDATE" ]; then
  DATE="$OPENGAPPSDATE"
else
  DATE=$(date +"%Y%m%d")
fi
a="/$0"; a=${a%/*}; a=${a#/}; a=${a:-.}; TOP=$(cd "$a/.."; pwd -P) || exit 1
ARCH="$1"
API="$2"
VARIANT="$3"
BUILD="$TOP/build"
CACHE="$TOP/cache"
SOURCES="$TOP/sources"
GO_DIR_SUFFIX="-go"
SCRIPTS="$TOP/scripts"
CERTIFICATES="$SCRIPTS/certificates"
#CERTIFICATEFILE=""  # this can be set to a filepath to use as certificate file for signing
#KEYFILE=""  # this can be set to a filepath to use as key file for signing
#OPENGAPPSLICENSEFILE=""  # this can be set to a filepath to include as a LICENSE file
OUTFOLDER="$TOP/out"
#OUTFILE='$OUTFOLDER/open_gapps-$ARCH-$PLATFORM-$VARIANT-$DATE.zip'  # this can be set to a filepath to use as alternative outputfile; use ' to allow variables to be evaluated later
LOGFOLDER="$TOP/log"
#VERSIONLOG='$LOGFOLDER/open_gapps-$ARCH-$PLATFORM-$VARIANT-$DATE.versionlog.txt'  # if set to a non-zero value a version log will be created at the set filepath; use ' to allow variables to be evaluated later
COMPRESSION="lz" # xz # none # this sets the default compression method, override is possible in compressapp
#TMPSIGNDIR=""  $ if set to a value, at this location the zip signing temporary file is created
#ZIPALIGNRECOMPRESS=""  # if set to a non-zero value, APKs will be recompressed with zopfli during zipalign
ZIPCOMPRESSIONLEVEL="0" # Store only the files in the zip without compressing them (-0 switch): further compression will be useless and will slow down the building process
# shellcheck source=scripts/inc.tools.sh
. "$SCRIPTS/inc.tools.sh"
# shellcheck source=scripts/inc.aromadata.sh
. "$SCRIPTS/inc.aromadata.sh"
# shellcheck source=scripts/inc.buildhelper.sh
. "$SCRIPTS/inc.buildhelper.sh"
# shellcheck source=scripts/inc.buildtarget.sh
. "$SCRIPTS/inc.buildtarget.sh"
# shellcheck source=scripts/inc.compatibility.sh
. "$SCRIPTS/inc.compatibility.sh"
# shellcheck source=scripts/inc.installer.sh
. "$SCRIPTS/inc.installer.sh"
# shellcheck source=scripts/inc.packagetarget.sh
. "$SCRIPTS/inc.packagetarget.sh"
# shellcheck source=scripts/inc.sourceshelper.sh
. "$SCRIPTS/inc.sourceshelper.sh"

# Check tools
checktools aapt apksigner coreutils java jarsigner unzip zip tar zipalign

case "$API" in
19) PLATFORM="4.4" ;;
21) PLATFORM="5.0" ;;
22) PLATFORM="5.1" ;;
23) PLATFORM="6.0" ;;
24) PLATFORM="7.0" ;;
25) PLATFORM="7.1" ;;
26) PLATFORM="8.0" ;;
27) PLATFORM="8.1" ;;
28) PLATFORM="9.0" ;;
29) PLATFORM="10.0" ;;
30) PLATFORM="11.0" ;;
31) PLATFORM="12" ;;
32) PLATFORM="12L" ;;
*)
  echo "ERROR: Unknown API version! Aborting..."
  exit 1
  ;;
esac

if [ "$API" -lt "21" ] && [ "$ARCH" != "arm" ] && [ "$ARCH" != "x86" ]; then
  echo "ERROR! Platform $ARCH cannot be built on API level $API"
  exit 1
fi

get_supported_variants "$VARIANT"
SUPPORTEDVARIANTS="$supported_variants"
GAPPSREMOVEVARIANT="$gappsremove_variant"

if [ -z "$SUPPORTEDVARIANTS" ]; then
  echo "ERROR: Unknown variant! aborting..."
  exit 1
fi

case "$VARIANT" in
*_go)
    if [ "$API" -lt "27" ]; then
        echo "ERROR! Go edition cannot be built on API level $API. Go edition appeared with API 27.
More info here: https://developer.android.com/docs/quality-guidelines/build-for-billions/device-capacity#androidgo"
        exit 1
    fi
    ;;
esac

if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ]; then #For all non-arm(64) platforms
  case "$VARIANT" in
  aroma)
    echo "ERROR! Variant $VARIANT cannot be built on a non-arm platform"
    exit 1
    ;;
  super | stock | full)
    if [ "$API" -lt "21" ]; then
      echo "ERROR! Variant $VARIANT cannot be built on a non-arm < 5.0 platform"
      exit 1
    fi
    ;; #because system wide libs will probably not work with libhoudini
  esac
fi
if [ "$API" -lt "22" ]; then
  case "$VARIANT" in
  super | tvstock | tvmini)
    echo "ERROR! Variant $VARIANT cannot be built on API level $API"
    exit 1
    ;;
  esac
fi
echo "Generating Open GApps $VARIANT package for $ARCH with API level $API..."

kitkatpathshack #kitkat has different apk and lib paths which impact installer.data
kitkatdatahack  #kitkat installs some applications on /data/ instead of /system/
keyboardlibhack #only 5.0+ has gestures for the aosp keyboard possible, which impact installer.data and an extra file in the package
api19hack       #4.4- has a no setupwizard product type
api21hack       #only 5.0+ supports google tag
api22hack       #only 5.1+ supports google webview (Stock Google 5.0 ROMs too, but we merged stock and fornexus) and GCS
api23hack       #only on 6.0+ we also include Google Contacts, Dialer, Calculator, Packageinstaller and Configupdater
api24hack       #only on 7.0+ we also include Google ExtServices, ExtShared, PrintService, VR
api25hack       #only on 7.1+ we also include AndroidPlatform, GMSSetup, Pixel Launcher, StorageManager
api26hack       #only on 8.0+ we also include AndroidPlatformServices
api27hack       #only here for completeness
api28hack       #only on 9.0+ we also include Actions Services, AndroidPlatformServices, Data Transfer Tool, Markup, Sounds
api29hack       #only on 10.0+ we also include Actions Services with Pixel Launcher and TrichromeLibrary with Chrome and Webview
api30hack       #only on 11.0+ we also include Actions Services with Pixel Launcher and TrichromeLibrary with Chrome and Webview
api31hack       #only on 12 - Don't Panic
api32hack       #only on 12L - Don't Panic
gohack          #Create go variants based on the non-go variants
buildtarget
alignbuild
commonscripts
if [ "$VARIANT" = "aroma" ]; then
  aromascripts
fi
createzip
