1#!/bin/sh
 2set -e -u -o pipefail
 3#set -v
 4
 5# run this from a directory outside the script directory
 6# as it will clone the repo and create general mess
 7# plus it will nuke anything called root
 8
 9SRCDIR=$(dirname $0)
10DESTDIR=$(pwd)
11
12generate_file_etag() {
13	file=$1
14	prefix=$2
15	# trim: ./ -> /
16	WEB_PATH=$(echo $file | cut -c 2-)
17	SHA=$(shasum $file | awk '{print $1}')
18	echo "$prefix$WEB_PATH	\"$SHA\";"
19}
20
21generate_directory_etag() {
22	dir=$1
23	prefix=$2
24	# trim: ./ -> /
25	WEB_PATH=$(echo $dir | cut -c 2-)
26	SHA=$(shasum $dir/index.html | awk '{print $1}')
27	echo "$prefix$WEB_PATH/	\"$SHA\";"
28}
29
30generate_etags() {
31	prefix=$1
32	find . -type f | while read -r file; do
33		generate_file_etag "$file" "$prefix"
34	done | tee -a $DESTDIR/root/usr/local/etc/nginx/etag.map
35	find . -type d | while read -r dir; do
36		if [ -e $dir/index.html ]; then
37			generate_directory_etag "$dir" "$prefix"
38		fi
39	done | tee -a $DESTDIR/root/usr/local/etc/nginx/etag.map
40}
41
42# clean and create the output dirs
43rm -rf root
44mkdir -p root/usr/local/www/git
45mkdir -p root/usr/local/etc
46mkdir -p root/etc/newsyslog.conf.d
47
48# copy in the syslog and nginx conf
49cp $SRCDIR/newsyslog/* root/etc/newsyslog.conf.d/
50cp -r $SRCDIR/nginx root/usr/local/etc
51
52# generate the blog files
53pushd $SRCDIR/blog
54hugo --cleanDestinationDir=true --minify=true
55popd
56
57# move that in
58mv $SRCDIR/blog/public/ root/usr/local/www/blog
59
60# generate the etags
61pushd root/usr/local/www/blog
62generate_etags ""
63popd
64
65# get a www.git git checkout and update it for dumb transport
66git clone --no-hardlinks --bare $SRCDIR root/usr/local/www/git/www.git
67git -C root/usr/local/www/git/www.git remote remove origin
68git -C root/usr/local/www/git/www.git update-server-info
69
70# generate static site
71pushd root/usr/local/www/git/www.git
72~/go/bin/gitmal --minify .
73mv output ../www
74popd
75
76# get a gitmal.git git checkout and update it for dumb transport
77# (assumes it's in www/../gitmal)
78git clone --no-hardlinks --bare $SRCDIR/../gitmal root/usr/local/www/git/gitmal.git
79git -C root/usr/local/www/git/gitmal.git remote remove origin
80git -C root/usr/local/www/git/gitmal.git update-server-info
81
82# generate static site
83pushd root/usr/local/www/git/gitmal.git
84~/go/bin/gitmal --minify .
85mv output ../gitmal
86popd
87
88# generate the etags
89pushd root/usr/local/www/git/
90generate_etags "/git"
91popd
92
93# compress the files
94find root/usr/local/www -type f -exec $SRCDIR/scripts/compress.sh \{\} \;
95
96# copy in cgi-bin last so it doesn't get compressed
97cp -r $SRCDIR/cgi-bin root/usr/local/www/