summaryrefslogtreecommitdiffstats
path: root/bin/emerge-webrsync
blob: 9e2a44af214efaad0aa9ee5562ceb0e830e27f56 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: /var/cvsroot/gentoo-src/portage/bin/Attic/emerge-webrsync,v 1.8.2.4 2005/02/26 11:22:38 carpaski Exp $
# Author: Karl Trygve Kalleberg <karltk@gentoo.org>
# Rewritten from the old, Perl-based emerge-webrsync script

GENTOO_MIRRORS="${GENTOO_MIRRORS} $(/usr/lib/portage/bin/portageq gentoo_mirrors)"
PORTDIR="$(/usr/lib/portage/bin/portageq portdir)"
FETCHCOMMAND="$(/usr/lib/portage/bin/portageq envvar FETCHCOMMAND)"
USERLAND="$(/usr/lib/portage/bin/portageq envvar USERLAND)"
DISTDIR="$(/usr/lib/portage/bin/portageq envvar PORTAGE_TMPDIR)/emerge-webrsync"

if [ ! -d $DISTDIR ] ; then
	mkdir -p $DISTDIR
fi

cd "$DISTDIR"

found=0
if [ "$1" == "-v" ] ; then
	wgetops=
else	
	#this sucks.  probably better to do 1> /dev/null
	#that said, waiting on the refactoring.
	if [ "${FETCHCOMMAND/wget}" != "${FETCHCOMMAND}" ]; then
		wgetops="-q"
	elif [ "${FETCHCOMMAND/curl}" != "${FETCHCOMMAND}" ]; then
		wgetops="-s -f"
	fi
fi

if type -p md5sum > /dev/null; then
	md5_com='md5sum -c "${FILE}.md5sum"'
elif type -p md5 > /dev/null; then
	md5_com='[ "$(md5 -q ${FILE})" == "$(cut -d \  -f 1 ${FILE}.md5sum)" ]'
else
	echo "warning, unable to do md5 verification of the snapshot!"
	echo "no suitable md5/md5sum binary was found!"
	md5_com='true'
fi

sync_local() {
	echo Syncing local tree...
	if ! tar jxf $FILE; then
		echo "Tar failed to extract the image. Please review the output."
		echo "Executed command: tar jxf $FILE"
		exit 1
	fi
	rm -f $FILE
	# Make sure user and group file ownership is root
	chown -R 0:0 portage
	cd portage
	rsync -av --progress --stats --delete --delete-after \
	--exclude='/distfiles' --exclude='/packages' \
	--exclude='/local' . ${PORTDIR%%/}
	cd ..
	echo "cleaning up"
	rm -rf portage
	echo "transferring metadata/cache"
	emerge metadata
}

echo "Fetching most recent snapshot"

declare -i attempts=-1
while (( $attempts <  40 )) ; do
	attempts=$(( attempts + 1 ))

	#this too, sucks.  it works in the interim though.
	if [ "$USERLAND" == "BSD" ] || [ "$USERLAND" == "Darwin" ] ; then
		daysbefore=$(expr $(date +"%s") - 86400 \* $attempts)
		day=$(date -r $daysbefore +"%d")
		month=$(date -r $daysbefore +"%m")
		year=$(date -r $daysbefore +"%Y")
	else
		day=$(date -d "-$attempts day" +"%d")
		month=$(date -d "-$attempts day" +"%m")
		year=$(date -d "-$attempts day" +"%Y")
	fi

	FILE_ORIG="portage-${year}${month}${day}.tar.bz2"

	echo "Attempting to fetch file dated: ${year}${month}${day}"
		
	got_md5=0

	if [  ! -e "${FILE_ORIG}.md5sum" ]; then
		FILE="${FILE_ORIG}.md5sum"
		for i in $GENTOO_MIRRORS ; do 
			URI="${i}/snapshots/${FILE}"
			if (eval "$FETCHCOMMAND $wgetops") && [ -s "${FILE}" ]; then
				got_md5=1
				break
			fi
		done
	else
		got_md5=1
	fi
	FILE="${FILE_ORIG}"

	if (($got_md5 == 0 )); then
		echo " --- No md5sum present on the mirror. (Not yet available.)"
		continue
	elif [ -s "${FILE}" ]; then
		if eval "$md5_com"; then
			echo " === snapshot $FILE is correct, using it"
			sync_local
			echo
			echo " === Snapshot has beed sync'd"
			echo
			exit 0
		else
			rm $FILE
		fi
	fi
	
	for i in $GENTOO_MIRRORS ; do
		URI="${i}/snapshots/$FILE"
		rm -f "$FILE"
		if (eval "$FETCHCOMMAND $wgetops") && [ -s "$FILE" ]; then
			if ! eval "$md5_com"; then
				echo "md5 failed on $FILE"
				rm ${FILE}
				continue
			else
				sync_local
				echo
				echo " *** Completed websync, please now perform a normal rsync if possible."
				echo "     Update is current as of the of YYYYMMDD: ${year}${month}${day}"
				echo
				exit 0
			fi
		fi

	done
done

rm -rf portage

exit 1