#!/bin/sh
# create-snapshot - create an upstream snapshot as a tarball w/o unneeded files
#
# Copyright (C) 2013 Canonical Ltd.
#
# This program is 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 2 of the License, or (at your option) any later
# version.
#
# This program is 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.
# .
# You should have received a copy of the GNU General Public
# License along with this package; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA  02110-1301 USA
#
# depends: git, xz-utils (xz)
# author: Loïc Minier <loic.minier@ubuntu.com>

ANDROID_TAG="${ANDROID_TAG:-}"
ANDROID_TAG_PATTERN="${ANDROID_TAG_PATTERN:-android-[0-9].*}"

ANDROID_MIRROR="https://android.googlesource.com/"
CORE_REPO="$ANDROID_MIRROR/platform/system/core"
EXTRAS_REPO="$ANDROID_MIRROR/platform/system/extras"
LIBHARDWARE_REPO="$ANDROID_MIRROR/platform/hardware/libhardware"
GITIGNORE="
*
!*.[ch]
!/core/
!/core/adb/
!/core/fastboot/
!/core/include/
!/core/include/cutils/
!/core/include/mincrypt/
!/core/include/private/
!/core/include/utils/
!/core/include/zipfile/
!/core/libcutils/
!/core/libzipfile/
!/core/libsparse/
!/core/libsparse/include/
!/core/libsparse/include/sparse/
!/core/libsparse/simg_dump.py
!/core/mkbootimg/
!/extras/
!/extras/ext4_utils/
!/extras/ext4_utils/mkuserimg.sh
!/extras/ext4_utils/test_ext4fixup
!/extras/f2fs_utils/
!/libhardware/
!/libhardware/include/
!/libhardware/include/hardware/
"

self="$(basename "$0")"
work_dir=""

log() {
    echo "$*" >&2
}

log_i() {
    log "I:" "$@"
}

cleanup() {
    if [ -n "$work_dir" ]; then
        log_i "Cleaning up..."
        rm -rf "$work_dir"
    fi
}

trap "cleanup" 0 1 2 3 9 11 13 15

work_dir="$(mktemp -dt "$self.XXXXXXXXXX")"

git_latest_tag() {
    git for-each-ref --sort='-taggerdate' --format='%(refname:short)' --count=1 "refs/tags/$ANDROID_TAG_PATTERN"
}

git_checkout_some_tag() {
    cd "$1"
    if [ -z "$ANDROID_TAG" ]; then
        ANDROID_TAG=`git_latest_tag`
    fi
    if ! git checkout "$ANDROID_TAG"; then
        log "E: failed to check out $ANDROID_TAG"
        exit 1
    else
        log_i "$1: tag=`git describe`"
        rm -rf ".git"
        cd -
    fi
}

# run from within the android-tools directory
in_work_dir() {
    log_i "Cloning core..."
    git clone "$CORE_REPO"
    git_checkout_some_tag core
    log_i "Cloning extras..."
    git clone "$EXTRAS_REPO"
    git_checkout_some_tag extras
    log_i "Cloning libhardware..."
    git clone "$LIBHARDWARE_REPO"
    git_checkout_some_tag libhardware

    log_i "Importing in git..."
    git init
    echo "$GITIGNORE" >.gitignore
    git add .
    git commit -m "Initial import"
    log_i "Committed $ANDROID_TAG: core extras libhardware"
}

oldpwd="$PWD"
cd "$work_dir" && in_work_dir
cd "$oldpwd"

log_i "Creating tarball..."
FULLNAME="android-tools_${ANDROID_TAG#android-}"
GIT_DIR="$work_dir/.git" git archive --format=tar \
  --prefix="$FULLNAME/" HEAD | xz >"$FULLNAME.tar.xz"
