summaryrefslogtreecommitdiffstats
path: root/scripts/apply_patch_series.sh
blob: 6132344938fcd698be426436f4b48faadb1b8924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
#
# apply_patch_series: either apply a patch series to a directory or
#                     apply all patches from a given directory
#

. `dirname $0`/libptxdist.sh

usage() {
	echo
	[ -n "$1" ] && echo -e "${PREFIX} error: $1\n"
	echo "usage: $0 <args>"
	echo
	echo " Arguments:"
	echo
	echo "  -s <file>      series file to use"
	echo "  -p <path>      apply all patches from <path>"
	echo "  -d <directory> target directory"
	exit 1
}

#
# Option parser
#
while getopts "hs:d:p:" OPT
do
    case "$OPT" in
        h)  usage
	    exit 1
            ;;
        s)  SERIES=`ptxd_abspath $OPTARG`;
            ;;
	d)  TARGET=`ptxd_abspath $OPTARG`;
	    ;;
	p)  PATCHESPATH=`ptxd_abspath $OPTARG`;
    esac
done
shift `expr $OPTIND - 1`

#
# Sanity checks
#
[ -z "$SERIES" ] && [ -z "$PATCHESPATH" ] && usage "${PREFIX} error: specify a series file with -s or a patches directory with -p"
[ -n "$SERIES" ] && [ -n "$PATCHESPATH" ] && usage "${PREFIX} error: the -s and -p option may not be used together"
[ -z "$TARGET" ]                          && usage "${PREFIX} error: specify a target directory with -d"

[ -n "$SERIES" ] && PATCHESPATH=`dirname $SERIES`

pushd "$TARGET" || exit 1

{
	if [ -f "$SERIES" ]; then
		cat "$SERIES"
	else
		cd $PATCHESPATH && find  -name "*.patch" -or -name "*.diff" -or -name "*.gz" -or -name "*.bz2"
	fi
} |
egrep -v "^[[:space:]]*#" | egrep -v "^[[:space:]]*$" | while read patchfile patchpara; do
	abspatch="$PATCHESPATH"/"$patchfile"
	if [ ! -e "$abspatch" ]; then
		echo "patch $abspatch does not exist. aborting"
		exit 1
	fi
	case "$patchfile" in
	*.gz)
		CAT=zcat
		;;
	*.bz2)
		CAT=bzcat
		;;
	*)
		CAT=cat
		;;
	esac;
	echo "applying $abspatch"
	if [ $patchpara ] && [ `echo $patchpara | egrep '\-p[0-9]+'` ] ;then
		$CAT "$abspatch" | patch $patchpara || exit 1
	else
		$CAT "$abspatch" | patch -p1 || exit 1
	fi
done

if [ "$?" != 0 ]; then
	exit 1;
fi

popd