#! /usr/bin/env bash
pkg="$(echo "$1" | sed 's@/*$@@;s@^\.*/*@@')"
if [ -z "${pkg}" ]; then
echo "Usage: build <package>" 2>&1
exit 1
fi
function determineOsArch() {
os="$(uname -s | dd conv=lcase 2>/dev/null)"
arch="$(uname -m | dd conv=lcase 2>/dev/null)"
case "${arch}" in
i?86)
arch='ix86'
;;
esac
}
function determinePrefix() {
determineOsArch
prefixsuffix="${pkg}/${os}-${arch}/${version}"
prefix="/opt/appfs/${domain}/${prefixsuffix}"
destdir="$(pwd)/INST"
mkdir "${destdir}" || die
}
function preconfigure() {
:
}
function postconfigure() {
:
}
function configure() {
glibcvers=2.20
glibcdir="/opt/appfs/core.appfs.rkeene.org/glibc/platform/${glibcvers}"
dynlinker="$(ls "${glibcdir}"/lib/ld-linux*.so.* | tail -n 1)"
CFLAGS="-I${glibcdir}/include"
CPPFLAGS="-I${glibcdir}/include"
LDFLAGS="-Wl,--rpath,${glibcdir}/lib -Wl,--dynamic-linker,${dynlinker}"
export CFLAGS CPPFLAGS LDFLAGS
./configure --prefix="${prefix}" --sysconfdir=/etc --localstatedir=/var
}
function prebuild() {
:
}
function postbuild() {
:
}
function build() {
grep "DESTDIR" Makefile || die "Don't know how to build this software"
make
}
function preinstall() {
:
}
function postinstall() {
:
}
function install() {
make install DESTDIR="${destdir}"
}
function cleanup() {
cd "${workdir}" || exit 1
cd .. || exit 1
rm -rf "${workdir}"
}
function die() {
local message
message="$1"
if [ -n "${message}" ]; then
echo "error: ${message}" >&2
fi
cleanup
exit 1
}
cd "$(dirname "$(which "$0")")" || exit 1
if [ -f 'build.conf' ]; then
. 'build.conf'
fi
if [ -d "pkgs/${pkg}" ]; then
pkgdir="pkgs/${pkg}"
pkgfile="${pkgdir}/info"
else
pkgfile="pkgs/${pkg}"
pkgdir="${pkgfile}"
fi
if [ ! -e "${pkgfile}" ]; then
echo "Invalid package." >&2
exit 1
fi
pkgdate="$(for artifact in $(find "${pkgdir}" -type f -print0 | xargs -n 1 -0 fossil finfo --limit 1 --width 0 2>/dev/null | grep '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ' | sed 's@^[^ ]* \[@@;s@\].*@@' | sort -u); do fossil info "${artifact}" | awk '/^uuid:/{ print $3 "T" $4 }' | sed 's@[-:]@@g;s@..$@\.&@'; done | sort -n | tail -n 1)"
if [ -z "${pkgdate}" ]; then
pkgdate="$(find "${pkgdir}" -type f -printf '%TY%Tm%Td%TH%TM.%TS\n' 2>/dev/null | cut -f 1-2 -d '.' | sort -n | tail -n 1)"
fi
. "${pkgfile}"
archivedir="$(pwd)/ARCHIVE"
workdir="workdir-$$${RANDOM}${RANDOM}${RANDOM}"
srcfile="${archivedir}/${pkg}"
mkdir "${archivedir}" >/dev/null 2>/dev/null
mkdir "${workdir}" || exit 1
cd "${workdir}" || exit 1
workdir="$(pwd)"
if [ ! -e "${srcfile}" ]; then
# Download
## Cleanup
rm -f src.new src
## Fetch file
wget -O src.new "${url}" || exit 1
## Verify signature
src_sha256="$(openssl sha256 'src.new' | sed 's@^.*= @@')"
if [ "${src_sha256}" != "${sha256}" ]; then
echo "SHA256 mismatch: Downloaded: ${src_sha256} != Expected: ${sha256}" >&2
exit 1
fi
## Move file into place
mv src.new "${archivedir}/${pkg}"
fi
# Decompress archive
## Determine type of archive
case "${url}" in
*.tar.xz|*.tar.xz'?'*|*.txz)
decompress='xz'
;;
*.tar.gz|*.tar.gz'?'*|*.tgz)
decompress='gzip'
;;
*.tar.bz2|*.tar.bz2'?'*|*.tbz2)
decompress='bzip2'
;;
*.zip|*.zip'?'*)
decompress='unzip'
;;
*)
echo "Unknown compression method: ${url}" >&2
exit 1
;;
esac
## Do decompression
case "${decompress}" in
unzip)
unzip "${srcfile}" || die 'Unable to uncompress archive'
;;
*)
"${decompress}" -dc "${srcfile}" | tar -xf - || die 'Unable to uncompress archive'
;;
esac
## Cleanup source
rm -f src
# If we just have one directory, use that directory
dir="$(echo *)"
if [ -e "${dir}" ]; then
mv "${dir}"/* .
fi
# Start logging
set -x
# Determine properties
determinePrefix
# Start the build
preconfigure || die 'preconfigure failed'
configure || die 'configure failed'
postconfigure || die 'postconfigure failed'
prebuild || die 'prebuild failed'
build || die 'build failed'
postbuild || die 'postbuild failed'
preinstall || die 'preinstall failed'
install || die 'install failed'
postinstall || die 'postinstall failed'
(
appdir="$(appfsinstalldir)/${prefixsuffix}"
mkdir -p "${appdir}"
cd "${destdir}/${prefix}" || exit 1
cp -rp * "${appdir}"
find "${appdir}" -print0 | xargs -0 touch -t "${pkgdate}"
)
cleanup
exit 0