abinfo "Setting up Golang build parameters ..."
export \
    GOPATH="${SRCDIR}/go" \
    GOFLAGS="" \
    GOFLAGS_SNAP="-tags nomanagers" \
    CGO_ENABLED="1" \
    CGO_CFLAGS="${CFLAGS}" \
    CGO_CPPFLAGS="${CPPFLAGS}" \
    CGO_CXXFLAGS="${CXXFLAGS}" \
    CGO_LDFLAGS="${LDFLAGS}" \
    GOURL="github.com/snapcore/snapd"
flags=(-buildmode=pie -ldflags "-s -linkmode external -extldflags '$LDFLAGS'" -trimpath -mod=vendor)
staticflags=(-buildmode=pie -ldflags "-s -linkmode external -extldflags '$LDFLAGS -static'" -trimpath -mod=vendor)

abinfo "Creating symlink in GOPATH ..."
mkdir -pv "${SRCDIR}/go/src/github.com/snapcore/"
ln --no-target-directory -vfs "${SRCDIR}" \
    "${SRCDIR}/go/src/${GOURL}"

abinfo "Building the snap exectuable ..."
go build \
    -v "${flags[@]}" \
    -o "$srcdir/go/bin/snap" \
    $GOFLAGS_SNAP "${GOURL}/cmd/snap"

for component in snapd snap-seccomp snap-failure snap snap-bootstrap \
    snap-preseed snap-fde-keymgr snapd-apparmor; do
    abinfo "Building the ${component} executable ..."
    go build \
        -v "${flags[@]}" \
        -o "${SRCDIR}/go/bin/${component}" \
        $GOFLAGS "${GOURL}/cmd/${component}"
done

for static_component in snap-update-ns snap-exec snapctl; do
    abinfo "Building the static ${static_component} exectuable ..."
    go build \
        -v "${staticflags[@]}" \
        -o "${SRCDIR}/go/bin/${static_component}" \
        $GOFLAGS \
        "${GOURL}/cmd/${static_component}"
done

abinfo "Generating data files ..."
make \
    -C "${SRCDIR}/data"

cd "${SRCDIR}/cmd"
abinfo "Configuring other Snap utilities ..."
./configure \
    "${AUTOTOOLS_DEF[@]}" \
    --prefix="${PREFIX}" \
    --libexecdir="${LIBDIR}/snapd" \
    --with-snap-mount-dir=/snap \
    --enable-apparmor \
    --enable-nvidia-biarch \
    --enable-merged-usr

abinfo "Building other Snap utilities ..."
make

abinfo "Installing Bash completions ..."
install -Dvm644 "${SRCDIR}/data/completion/bash/snap" \
    "${PKGDIR}/usr/share/bash-completion/completions/snap"
install -Dvm644 "${SRCDIR}/data/completion/bash/complete.sh" \
    "${PKGDIR}/${LIBDIR}/snapd/complete.sh"
install -Dvm644 "${SRCDIR}/data/completion/bash/etelpmoc.sh" \
    "${PKGDIR}/${LIBDIR}/snapd/etelpmoc.sh"

abinfo "Installing Zsh completions ..."
install -Dvm644 "${SRCDIR}/data/completion/zsh/_snap" \
    "${PKGDIR}/usr/share/zsh/site-functions/_snap"

abinfo "Installing systemd units and D-Bus services ..."
make install \
    -C "${SRCDIR}/data" \
    DESTDIR="${PKGDIR}" \
    SYSTEMDSYSTEMUNITDIR="/usr/lib/systemd/system"

abinfo "Installing PolicyKit policy for snapd ..."
install -Dvm644 "${SRCDIR}/data/polkit/io.snapcraft.snapd.policy" \
    "${PKGDIR}/usr/share/polkit-1/actions/io.snapcraft.snapd.policy"

abinfo "Installing the snap executable ..."
install -Dvm755 "${SRCDIR}/go/bin/snap" \
    "${PKGDIR}/${BINDIR}/snap"

for executable in snapctl snapd snap-seccomp snap-failure snap-update-ns \
    snap-exec snap-bootstrap snap-preseed snap-fde-keymgr snapd-apparmor; do
    abinfo "Installing ${executable} to ${LIBDIR}/snapd/ ..."
    install -Dvm755 "${SRCDIR}/go/bin/${executable}" \
        "${PKGDIR}/${LIBDIR}/snapd/${executable}"
done

abinfo "Creating symlink for snapctl ..."
ln -sv ../lib/snapd/snapctl \
    "${PKGDIR}/${BINDIR}/snapctl"

abinfo "Creating necessary directories ..."
install -dvm755 \
    "${PKGDIR}/var/lib/snapd/snap" \
    "${PKGDIR}/var/cache/snapd" \
    "${PKGDIR}/var/lib/snapd/apparmor" \
    "${PKGDIR}/var/lib/snapd/assertions" \
    "${PKGDIR}"/var/lib/snapd/dbus-1/{,system-}services \
    "${PKGDIR}/var/lib/snapd/desktop/applications" \
    "${PKGDIR}/var/lib/snapd/device" \
    "${PKGDIR}/var/lib/snapd/hostfs" \
    "${PKGDIR}/var/lib/snapd/mount" \
    "${PKGDIR}/var/lib/snapd/seccomp/bpf" \
    "${PKGDIR}/var/lib/snapd/snap/bin" \
    "${PKGDIR}/var/lib/snapd/snaps" \
    "${PKGDIR}/var/lib/snapd/inhibit" \
    "${PKGDIR}"/var/lib/snapd/lib/gl{,32} \
    "${PKGDIR}"/var/lib/snapd/lib/{glvnd,vulkan}
install -dvm111 \
    "${PKGDIR}/var/lib/snapd/void"
install -dvm700 \
    "${PKGDIR}"/var/lib/snapd/{cache,cookie}

abinfo "Installing other Snap utilities ..."
make install \
    -C "${SRCDIR}/cmd" \
    DESTDIR="${PKGDIR}"

abinfo "Installing manpage for the snap executable ..."
mkdir -pv "${PKGDIR}/usr/share/man/man8"
"${SRCDIR}/go/bin/snap" help --man > "${PKGDIR}/usr/share/man/man8/snap.8"

abinfo "Installing the info data file with snapd version..."
install -Dvm644 "${SRCDIR}/go/src/${GOURL}/data/info" \
    "${PKGDIR}/${LIBDIR}/snapd/info"

abinfo "Creating symlink to enable systemd user socket unit by default ..."
install -dvm755 "${PKGDIR}/usr/lib/systemd/user/socket.target.wants"
ln -sv ../user/snapd.session-agent.socket \
    "${PKGDIR}/usr/lib/systemd/user/socket.target.wants/snapd.session-agent.socket"

# Remove Ubuntu Core specific self-repair scripts and services
# Thses scripts or services refuse to run on OSes other than Ubuntu Core
rm -v \
    "${PKGDIR}/${SYDDIR}"/snapd.snap-repair.* \
    "${PKGDIR}/${SYDDIR}"/snapd.core-fixup.* \
    "${PKGDIR}/${LIBDIR}/snapd/snapd.core-fixup.sh"
