forked from ipfs/distributed-wikipedia-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute-changes.sh
112 lines (91 loc) · 2.58 KB
/
execute-changes.sh
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
#!/bin/bash
# vim: set ts=2 sw=2:
set -euo pipefail
IFS=$'\n\t'
error() {
echo "$@"
exit 1
}
usage() {
echo "USAGE:"
echo " $0 [-h|--help] [--ipns=<ipns hash>] [--date=<date of snapshot>]";
echo " [--search=<cid of searchset>] [--main=<article>] <ipfs files root>"
echo ""
echo " -h|--help - displays help"
echo " --ipns - ipns hash of the archive"
echo " --date - date of snapshot (defaults to this month)"
echo " --search - hash of search IPLD structure"
echo " --main - full name of article containing intro page (e.g. Main_Page.html)"
exit 2
}
if [ "$(getopt --test >/dev/null 2>&1; echo $?)" -ne "4" ]; then
error "getopt enchanced required, 'getopt --test' should have exit code 4"
fi
LONG_OPT="help,search:,ipns:,date:,main:"
SHORT_OPT="h"
PARSED_OPTS=$(getopt -n "$0" -o "$SHORT_OPT" -l "$LONG_OPT" -- "$@") || usage
eval set -- "$PARSED_OPTS"
# defaults
SNAP_DATE=$(date +"%Y-%m-%d")
IPNS_HASH=""
SEARCH=""
MAIN=index.htm
while true; do
case "$1" in
-h|--help)
usage;;
--date)
SNAP_DATE="$2"
shift 2;;
--ipns)
IPNS_HASH="$2"
shift 2;;
--search)
SEARCH="$2"
shift 2;;
--main)
MAIN="$2"
shift 2;;
--)
shift;
break;;
esac
done
if [ -z "${1-}" ]; then
echo "Missing ipfs files root"
usage
fi
ROOT="$1"
ipfs-replace() {
ipfs files rm "$ROOT/$1" >/dev/null 2>&1 || true
ipfs files --flush=false cp "$2" "$ROOT/$1"
}
if ipfs files stat "$ROOT/A" >/dev/null 2>&1; then
ipfs files mv "$ROOT/A" "$ROOT/wiki"
fi
NEW_BODYJS=$(
sed -e 's/{{SNAPSHOT_DATE}}/'"$SNAP_DATE"'/g' \
-e 's/{{IPNS_HASH}}/'"$IPNS_HASH"'/g' scripts/body.js |\
if [ -n "$SEARCH" ]; then
cat - <(sed -e 's/{{SEARCH_CID}}/'"$SEARCH"'/' scripts/search-shim.js)
else
cat -
fi | ipfs add -Q
)
ipfs-replace "-/j/body.js" "/ipfs/$NEW_BODYJS"
ipfs-replace "I/s/Wikipedia-logo-v2-200px-transparent.png" \
"/ipfs/$(ipfs add -q assets/wikipedia-on-ipfs-small-flat-cropped-offset-min.png)"
ipfs-replace "I/s/wikipedia-on-ipfs.png" \
"/ipfs/$(ipfs add -Q assets/wikipedia-on-ipfs-100px.png)"
if [ -n "$SEARCH" ]; then
ipfs-replace "-/j/search.js" "/ipfs/$(ipfs add -Q scripts/search.js)"
fi
# comment out some debug stuff in head.js
HEAD_JS_LOCATION="$(ipfs files stat --hash "$ROOT")/-/j/head.js"
HEAD_JS_HASH="$(ipfs cat "$HEAD_JS_LOCATION" | sed -e "s|^\tdocument.getElementsByTagName( 'head' )|//\0|" | ipfs add -Q)"
ipfs-replace "-/j/head.js" "/ipfs/$HEAD_JS_HASH"
ipfs-replace "/wiki/index.html" "$ROOT/wiki/$MAIN"
ipfs-replace "/index.html" "/ipfs/$(ipfs add -Q redirect-page/index_root.html)"
ipfs files flush "$ROOT"
echo "We are done !!!"
ipfs files stat "$ROOT"