summaryrefslogtreecommitdiffstats
path: root/scripts/upd-updates
blob: 5e0e7998e64086a359f834cb9ce27043f66ed41b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#
# upd-updates
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# 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 program.  If not, see <http://www.gnu.org/licenses/>.
#

isAnacondaGitDir() {
	[ -f .git/config ] || return 1
	grep -q 'url = git+ssh://git\.fedoraproject\.org/git/hosted/anaconda\.git' .git/config
}

usage() {
	if [ $1 -ne 0 ]; then
		>&2
	fi
	echo "upd-updates <updates.img> [<file0> [<file1> [...]]]"
	exit $1
}

[ -z "$1" ] && usage 1
[ "$1" == "--help" ] && usage 0
updateAll=no
if [ -z "$2" ]; then
	if isAnacondaGitDir ; then
		updateAll=yes
	else
		usage 1
	fi
fi

TARGET="$1" ; shift
if [ ! -f "$1" ]; then
	echo -n | cpio -H newc --quiet -o | gzip -9 > "$1"
fi

OK=no
if [ -e "$TARGET" -a -w "$TARGET" -o -w $(dirname "$TARGET") ] ; then
	OK=yes
fi

if [ "$OK" == "no" ]; then
	echo "upd-updates: cannot write to $TARGET" 1>&2
	exit 2
fi

if [ "$updateAll" == "no" ]; then
	n=1
	while [ -n "$(eval echo '${'$n'}')" ]; do
		arg="$(eval echo '${'$n'}')"
		[ ${arg} == "--help" -o ${arg} == "-h" ] && usage 0

		if [ ! -r ${arg} ]; then
			echo "upd-updates: cannot read ${arg}" 1>&2
			exit 3
		fi
		n=$(($n + 1))
	done
fi

TMPDIR=$(mktemp -d)
if [ ! -d ${TMPDIR} ]; then
	echo "upd-updates: cannot make tmpdir ${TMPDIR}" 1>&2
	exit 4
fi

if [ -e "$TARGET" ]; then
	zcat "$TARGET" | ( cd $TMPDIR ; cpio -di --quiet -dm --unconditional )
	[ $? -eq 0 ] || exit 5
fi

if [ "$updateAll" == "yes" ]; then
	# this may do the wrong thing if we have two files named the same...
	# so, uh, don't do that.
	for oldfile in $(find "$TMPDIR" -type f) ; do
		for newfile in $(find . -name $(basename $oldfile)) ; do
			if [ "$oldfile" -ot "$newfile" ]; then
				cp -av "$newfile" "$oldfile"
			fi
		done
	done
else
	echo ${@} | tr ' ' '\0' | tr -d '\n' | \
		cpio --null -H newc --quiet -o | \
		( cd $TMPDIR ; cpio -di --quiet -dm --unconditional )

	[ $? -eq 0 ] || exit 6
fi

(cd $TMPDIR ; find . -depth -print0 | cpio -H newc --quiet --null -o ) | \
	gzip -9 > "$TARGET"

[ $? -eq 0 ] || exit 7

rm -rf "$TMPDIR"

[ $? -eq 0 ] || exit 8

exit 0