1#!/bin/sh
2#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# Script used to patch a source directory from a series of patches
19# located under a directory hierarchy
20
21. `dirname $0`/prebuilt-common.sh
22
23PROGRAM_PARAMETERS="<src-dir> <patches-dir>"
24PROGRAM_DESCRIPTION=\
25"Patch a target source directory with a series of patches taken
26from another directory hierarchy. The idea is that anything that
27is found under <patches-dir>/subdir/foo.patch will be applied with
28'patch -p1' in <src-dir>/subdir.
29
30Patches are applied in the order they are found by 'find'."
31
32OPTION_REVERSE=no
33register_var_option "--reverse" OPTION_REVERSE "Reverse the patches applied previously"
34
35parse_parameters ()
36{
37    SRC_DIR=$1
38    if [ -z "$SRC_DIR" ] ; then
39        echo "ERROR: Missing source directory. See --help for usage."
40        exit 1
41    fi
42
43    if [ ! -d "$SRC_DIR" ] ; then
44        echo "ERROR: Invalid target source directory: $SRC_DIR"
45        exit 1
46    fi
47
48    PATCHES_DIR=$2
49    if [ -z "$PATCHES_DIR" ] ; then
50        echo "ERROR: Missing patches directory. See --help for usage."
51        exit 1
52    fi
53
54    if [ ! -d "$PATCHES_DIR" ] ; then
55        echo "ERROR: Invalid patches directory: $PATCHES_DIR"
56        exit 1
57    fi
58}
59
60extract_parameters "$@"
61parse_parameters $PARAMETERS
62
63if [ "$OPTION_REVERSE" = "yes" ]; then
64    SORT="-r"
65    REVERSE="-R"
66fi
67
68PATCHES=`(cd $PATCHES_DIR && find . -name "*.patch" | sort $SORT) 2> /dev/null`
69
70if [ -z "$PATCHES" ] ; then
71    log "No patches files in $PATCHES_DIR"
72    exit 0
73fi
74PATCHES=`echo $PATCHES | sed -e s%^\./%%g`
75for PATCH in $PATCHES; do
76    PATCHDIR=`dirname $PATCH`
77    PATCHNAME=`basename $PATCH`
78    if [ -d $SRC_DIR/$PATCHDIR ]; then
79        log "Applying $PATCHNAME into $SRC_DIR/$PATCHDIR"
80        cd $SRC_DIR/$PATCHDIR && patch $REVERSE -p1 < $PATCHES_DIR/$PATCH
81        fail_panic "Patch failure with $PATCHES_DIR/$PATCH!! !! Please check your patches directory!"
82    else
83        echo "Ignore non-existance $SRC_DIR/$PATCHDIR"
84    fi
85done
86
87dump "Done!"
88