#!/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"
LIBSELINUX_REPO="${ANDROID_MIRROR}/platform/external/libselinux"
BUILD_REPO="${ANDROID_MIRROR}/platform/build"
GITIGNORE="
*
!.gitignore
!*.indirectionsymlink
!*.[ch]
!*.mk
!NOTICE
!MODULE_LICENSE_*
!/system/
!/system/core/
!/system/core/adb/
!/system/core/fastboot/
!/system/core/fs_mgr/
!/system/core/fs_mgr/include/
!/system/core/include/
!/system/core/include/android/
!/system/core/include/cutils/
!/system/core/include/log/
!/system/core/include/mincrypt/
!/system/core/include/private/
!/system/core/include/utils/
!/system/core/include/zipfile/
!/system/core/liblog/
!/system/core/liblog/tests/
!/system/core/libcutils/
!/system/core/libmincrypt/
!/system/core/libzipfile/
!/system/core/libsparse/
!/system/core/libsparse/include/
!/system/core/libsparse/include/sparse/
!/system/core/libsparse/simg_dump.py
!/system/core/mkbootimg/
!/system/core/mkbootimg/mkbootimg
!/system/extras/
!/system/extras/ext4_utils/
!/system/extras/ext4_utils/mkuserimg.sh
!/system/extras/ext4_utils/test_ext4fixup
!/system/extras/f2fs_utils/
!/hardware/
!/hardware/libhardware/
!/hardware/libhardware/include/
!/hardware/libhardware/include/hardware/
!/external/
!/external/libselinux/
!/external/libselinux/include/
!/external/libselinux/include/selinux/
!/external/libselinux/src/
!/external/f2fs-tools/
!/external/f2fs-tools/include/
!/external/f2fs-tools/lib/
!/external/f2fs-tools/mkfs/
!/build/
!/build/core/
!/build/core/version_defaults.mk
!/build/core/combo/
!/build/core/combo/include/
!/build/core/combo/include/arch/
!/build/core/combo/include/arch/linux-*/
!/build/core/combo/include/arch/linux-*/AndroidConfig.h
"

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() {
    mkdir -p system hardware external
    log_i "Cloning core..."
    git clone "$CORE_REPO" system/core
    git_checkout_some_tag system/core
    log_i "Cloning extras..."
    git clone "$EXTRAS_REPO" system/extras
    git_checkout_some_tag system/extras
    log_i "Cloning libhardware..."
    git clone "$LIBHARDWARE_REPO" hardware/libhardware
    git_checkout_some_tag hardware/libhardware
    log_i "Cloning libselinux..."
    git clone "$LIBSELINUX_REPO" external/libselinux
    git_checkout_some_tag external/libselinux
    log_i "Cloning build..."
    git clone "$BUILD_REPO"
    git_checkout_some_tag build

    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"
