#!/bin/bash

# Copyright (c) 2011-2012, NVIDIA CORPORATION
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of NVIDIA CORPORATION nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#
# This script sync's the android kernel source from nv-tegra, NVIDIA's
# public git repository, to the exact commit ID that the binary kernel
# shipped was built from. This allows replication of the exact kernel
# build.
#

# verify that git is installed
if  ! which git > /dev/null  ; then
  echo "ERROR: git is not installed. If your linux distro is 10.04 or later,"
  echo "git can be installed by 'sudo apt-get install git-core'."
  exit
fi

LDK_KERNEL_DIR=$(cd `dirname $0` && pwd)
LDK_KSOURCE_DIR=${LDK_KERNEL_DIR}/kernel_source

if [ -d "${LDK_KSOURCE_DIR}" ] ; then
    echo "${LDK_KSOURCE_DIR} already exists!"
else
	echo "Downloading default kernel source..."
	git clone git://nv-tegra.nvidia.com/linux-2.6.git ${LDK_KSOURCE_DIR} 2>&1 >/dev/null
	if [ $? -ne 0 ]; then
		echo "Kernel source sync failed!"
		exit
	fi

	echo "The default kernel source is downloaded in:"
	echo "   ${LDK_KSOURCE_DIR}"
fi

TAG=
if [ ! -z $1 ]; then
	TAG=$1
else
	echo "Please enter a tag to sync kernel source to"
	echo -n "(enter nothing to skip): "
	read TAG
	TAG=`echo $TAG`
fi

if [ ! -z "$TAG" ]; then
	pushd ${LDK_KSOURCE_DIR} > /dev/null
	git tag -l 2>/dev/null | grep -q "$TAG"
	if [ $? -eq 0 ]; then
		echo "Syncing up with tag $TAG..."
		git checkout -b mybranch_$(date +%Y-%m-%d) $TAG
		echo ""
		echo "Kernel source sync'ed to tag $TAG successfully!"
	else
		echo ""
		echo "Couldn't find tag $TAG"
		echo "Kernel source sync to tag $TAG failed!"
	fi
	popd > /dev/null
fi
