1#!/bin/bash
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2019 Intel Corporation. All rights reserved.
4
5# SOF documentation building & publishing.
6# According to instructions:
7# https://thesofproject.github.io/latest/howtos/process/docbuild.html
8
9# fail immediately on any errors
10set -e
11
12function print_usage()
13{
14	echo "usage: $0 <-b|-c|-p> [-r]"
15	echo "       [-b] Build"
16	echo "       [-c] Clean"
17	echo "       [-p] Publish"
18	echo "       [-r] Fetch missing repositories"
19}
20
21# make it runnable from any location
22# user shouldn't be confused from which dir this script has to be run
23SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
24
25# expected paths of repositories needed for documentation process
26SOF_REPO=$(dirname "$SCRIPT_DIR")
27DOCS_REPO="$(dirname "$SOF_REPO")/sof-docs"
28PUBLISH_REPO="$(dirname "$SOF_REPO")/thesofproject.github.io"
29
30# parse arguments
31DO_BUILD=false
32DO_CLEAN=false
33DO_PUBLISH=false
34DO_FETCH=false
35
36while getopts "bcpr" OPTION; do
37        case "$OPTION" in
38        b) DO_BUILD=true ;;
39        c) DO_CLEAN=true ;;
40        p) DO_PUBLISH=true ;;
41        r) DO_FETCH=true ;;
42        *) print_usage; exit 1 ;;
43        esac
44done
45shift "$(($OPTIND - 1))"
46
47if ! { "$DO_BUILD" || "$DO_CLEAN" || "$DO_PUBLISH"; }
48then
49        print_usage
50        exit 1
51fi
52
53if [ ! -d "$DOCS_REPO" ]
54then
55        if "$DO_FETCH"
56	then
57		echo "No sof-docs repo at: $DOCS_REPO"
58		echo "Cloning sof-docs repo"
59
60		read -p 'Enter your GitHub user name: ' GITHUB_USER
61		git clone \
62			git@github.com:$GITHUB_USER/sof-docs.git \
63			"$DOCS_REPO"
64
65		# set up fork for upstreaming, just like in instructions
66		cd "$DOCS_REPO"
67		git remote add upstream git@github.com:thesofproject/sof-docs.git
68	else
69		echo "Error: No sof-docs repo at: $DOCS_REPO"
70		echo "You can rerun the script with -r argument to fetch missing repositories:"
71		echo "Example: $0 $@ -r"
72		exit 1
73	fi
74fi
75
76cd "$SOF_REPO/doc"
77
78if "$DO_CLEAN"
79then
80	echo "Cleaning $SOF_REPO"
81	cmake .
82	make clean
83	make doc-clean
84fi
85
86if "$DO_BUILD"
87then
88	echo "Building $SOF_REPO"
89	cmake .
90	make doc
91fi
92
93cd "$DOCS_REPO"
94
95if "$DO_CLEAN"
96then
97	echo "Cleaning $DOCS_REPO"
98	make clean
99fi
100
101if "$DO_BUILD"
102then
103	echo "Building $DOCS_REPO"
104	make html
105
106	echo "Documentation build finished"
107	echo "It can be viewed at: $DOCS_REPO/_build/html/index.html"
108fi
109
110if "$DO_PUBLISH"
111then
112	if [ ! -d "$PUBLISH_REPO" ]
113	then
114
115		if "$DO_FETCH"
116		then
117			echo "No publishing repo at: $PUBLISH_REPO"
118			echo "Cloning thesofproject.github.io.git repo"
119
120			git clone \
121				git@github.com:thesofproject/thesofproject.github.io.git \
122				"$PUBLISH_REPO"
123		else
124			echo "Error: No publishing repo at: $PUBLISH_REPO"
125			echo "You can rerun the script with -r argument to fetch missing repositories:"
126			echo "Example: $0 $@ -r"
127			exit 1
128		fi
129	fi
130
131	echo "Publishing $PUBLISH_REPO"
132
133	cd "$DOCS_REPO"
134	make publish
135fi
136