maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1#!/bin/sh
  2
  3destdir=/
  4builddir="$PWD/build"
  5prefix=/usr/local
  6version=
  7static=0
  8if [ "${GOFLAGS}" = "" ]; then
  9	GOFLAGS="-trimpath" # set some flags to avoid passing "" to go
 10fi
 11
 12print_help() {
 13	cat >&2 <<EOF
 14Usage:
 15	./build.sh [options] {build,install}
 16
 17Script to build, package or install Maddy Mail Server.
 18
 19Options:
 20    -h, --help              guess!
 21    --builddir              directory to build in (default: $builddir)
 22
 23Options for ./build.sh build:
 24    --static                build static self-contained executables (musl-libc recommended)
 25    --tags <tags>           build tags to use
 26    --version <version>     version tag to embed into executables (default: auto-detect)
 27
 28Additional flags for "go build" can be provided using GOFLAGS environment variable.
 29
 30Options for ./build.sh install:
 31    --prefix <path>         installation prefix (default: $prefix)
 32    --destdir <path>        system root (default: $destdir)
 33EOF
 34}
 35
 36while :; do
 37	case "$1" in
 38		-h|--help)
 39		   print_help
 40		   exit
 41		   ;;
 42		--builddir)
 43		   shift
 44		   builddir="$1"
 45		   ;;
 46		--prefix)
 47		   shift
 48		   prefix="$1"
 49		   ;;
 50		--destdir)
 51			shift
 52			destdir="$1"
 53			;;
 54		--version)
 55			shift
 56			version="$1"
 57			;;
 58		--static)
 59			static=1
 60			;;
 61		--tags)
 62			shift
 63			tags="$1"
 64			;;
 65		--)
 66			break
 67			shift
 68			;;
 69		-?*)
 70			echo "Unknown option: ${1}. See --help." >&2
 71			exit 2
 72			;;
 73		*)
 74			break
 75	esac
 76	shift
 77done
 78
 79configdir="${destdir}etc/maddy"
 80
 81if [ "$version" = "" ]; then
 82	version=unknown
 83	if [ -e .version ]; then
 84		version="$(cat .version)"
 85	fi
 86	if [ -e .git ] && command -v git 2>/dev/null >/dev/null; then
 87		version="${version}+$(git rev-parse --short HEAD)"
 88	fi
 89fi
 90
 91set -e
 92
 93build_man_pages() {
 94	set +e
 95	if ! command -v scdoc >/dev/null 2>/dev/null; then
 96		echo '-- [!] No scdoc utility found. Skipping man pages building.' >&2
 97		set -e
 98		return
 99	fi
100	set -e
101
102	echo '-- Building man pages...' >&2
103
104	mkdir -p "${builddir}/man"
105	for f in ./docs/man/*.1.scd; do
106		scdoc < "$f" > "${builddir}/man/$(basename "$f" .scd)"
107	done
108}
109
110build() {
111	mkdir -p "${builddir}"
112	echo "-- Version: ${version}" >&2
113	if [ "$(go env CC)" = "" ]; then
114        echo '-- [!] No C compiler available. maddy will be built without SQLite3 support and default configuration will be unusable.' >&2
115    fi
116
117	if [ "$static" -eq 1 ]; then
118		echo "-- Building main server executable..." >&2
119		# This is literally impossible to specify this line of arguments as part of ${GOFLAGS}
120		# using only POSIX sh features (and even with Bash extensions I can't figure it out).
121		go build -trimpath -buildmode pie -tags "$tags osusergo netgo static_build" \
122			-ldflags "-extldflags '-fno-PIC -static' -X \"github.com/foxcpp/maddy.Version=${version}\"" \
123			-o "${builddir}/maddy" ${GOFLAGS} ./cmd/maddy
124	else
125		echo "-- Building main server executable..." >&2
126		go build -tags "$tags" -trimpath -ldflags="-X \"github.com/foxcpp/maddy.Version=${version}\"" -o "${builddir}/maddy" ${GOFLAGS} ./cmd/maddy
127	fi
128
129	build_man_pages
130
131	echo "-- Copying misc files..." >&2
132
133	mkdir -p "${builddir}/systemd"
134	cp dist/systemd/*.service "${builddir}/systemd/"
135	cp maddy.conf "${builddir}/maddy.conf"
136}
137
138install() {
139	echo "-- Installing built files..." >&2
140
141	command install -m 0755 -d "${destdir}/${prefix}/bin/"
142	command install -m 0755 "${builddir}/maddy" "${destdir}/${prefix}/bin/"
143	command ln -sf maddy "${destdir}/${prefix}/bin/maddyctl"
144	command install -m 0755 -d "${configdir}"
145
146
147	# We do not want to overwrite existing configuration.
148	# If the file exists, then save it with .default suffix and warn user.
149	if [ ! -e "${configdir}/maddy.conf" ]; then
150		command install -m 0644 ./maddy.conf "${configdir}/maddy.conf"
151	else
152		echo "-- [!] Configuration file ${configdir}/maddy.conf exists, saving to ${configdir}/maddy.conf.default" >&2
153		command install -m 0644 ./maddy.conf "${configdir}/maddy.conf.default"
154	fi
155
156	# Attempt to install systemd units only for Linux.
157	# Check is done using GOOS instead of uname -s to account for possible
158	# package cross-compilation.
159	# Though go command might be unavailable if build.sh is run
160	# with sudo and go installation is user-specific, so fallback
161	# to using uname -s in the end.
162	set +e
163	if command -v go >/dev/null 2>/dev/null; then
164		set -e
165		if [ "$(go env GOOS)" = "linux" ]; then
166			command install -m 0755 -d "${destdir}/${prefix}/lib/systemd/system/"
167			command install -m 0644 "${builddir}"/systemd/*.service "${destdir}/${prefix}/lib/systemd/system/"
168		fi
169	else
170		set -e
171		if [ "$(uname -s)" = "Linux" ]; then
172			command install -m 0755 -d "${destdir}/${prefix}/lib/systemd/system/"
173			command install -m 0644 "${builddir}"/systemd/*.service "${destdir}/${prefix}/lib/systemd/system/"
174		fi
175	fi
176
177	if [ -e "${builddir}"/man ]; then
178		command install -m 0755 -d "${destdir}/${prefix}/share/man/man1/"
179		for f in "${builddir}"/man/*.1; do
180			command install -m 0644 "$f" "${destdir}/${prefix}/share/man/man1/"
181		done
182	fi
183}
184
185# Old build.sh compatibility
186install_pkg() {
187	echo "-- [!] Replace 'install_pkg' with 'install' in build.sh invocation" >&2
188	install
189}
190package() {
191	echo "-- [!] Replace 'package' with 'build' in build.sh invocation" >&2
192	build
193}
194
195if [ $# -eq 0 ]; then
196	build
197else
198	for arg in "$@"; do
199		eval "$arg"
200	done
201fi