#!/usr/bin/env bash

echo "Docker Linux packages also include Docker Buildx when installed using the DEB or RPM packages. See docker-buildx-plugin."
exit 0

ARCH=$(dpkg --print-architecture)
if [[ ${ARCH} == "armhf" ]]; then ARCH="arm-v7"; fi
USER="docker"
PROJECT="buildx"
LICENSE="apache2"
SECTION="admin"
HOMEPAGE="https://docs.docker.com/buildx/working-with-buildx/"
BUILD_HOME="/var/tmp/build_docker-buildx"
#VERSION="2.1.0"
PACKAGE="docker-buildx"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq '[ .[] | select(.name|test("v[[:digit:].]+$"))]|.[0] | .name')
DEB_VERSION=$(echo ${VERSION} | tr -d "\"" | tr -d "v") # strip the preceding 'v'
#VERSION="1.0.0"
ARCH=$(dpkg --print-architecture)
MACHINE=$(uname -m)
OS=$(uname -s)
DEBEMAIL="cyteen@ring-zero.co.uk"
DEBFULLNAME="Cyteen May"
DESCRIPTION="."
LONG_DESCRIPTION=" ."

EXCLUDES="/root/.wget-hsts"

REQUIRES="docker-ce"
BUILD_DIR="/var/tmp/build_docker-buildx"
WORKDIR="/var/tmp/build_docker-buildx"

## Checkinstall variables - see defaults in /etc/checkinstallrc
DOC_DIR=/usr/share/doc

# make a new temporary directory for this use to avoid permission issues.
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

#-------------------------------------------------------------------------------
mkdir -p ${BUILD_DIR}

## Create and populate the install scripts and documentation for checkinstall
cat >${BUILD_DIR}/description-pak <<EOF
Docker Buildx is a CLI plugin that extends the docker command with the full support of the features provided by Moby BuildKit builder toolkit. It provides the same user experience as docker build with many new features like creating scoped builder instances and building against multiple nodes concurrently.
EOF

# | preinstall-pak  | Run BEFORE the package is INSTALLED  |
cat >${BUILD_DIR}/preinstall-pak <<EOF
#!/usr/bin/env bash
EOF

# | postinstall-pak | Run AFTER the package is INSTALLED   |
cat >${BUILD_DIR}/postinstall-pak <<EOF
#!/usr/bin/env bash
EOF

# | preremove-pak   | Run BEFORE the package is REMOVED    |
cat >${BUILD_DIR}/preremove-pak <<EOF
#!/usr/bin/env bash
EOF

# | postremove-pak  | Run AFTER the package is REMOVED
cat >${BUILD_DIR}/postremove-pak <<EOF
#!/usr/bin/env bash
EOF

## Make the doc-pak directory for README, INSTALL, COPYING, Changelog, TODO, CREDITS
mkdir -p ${BUILD_DIR}/doc-pak
#cp README INSTALL COPYING Changelog TODO CREDITS doc-pak

cat >${BUILD_DIR}/doc-pak/README <<'EOF'
Docker CLI plugin for extended build capabilities with BuildKit

buildx is Tech Preview
TL;DR

    Familiar UI from docker build
    Full BuildKit capabilities with container driver
    Multiple builder instance support
    Multi-node builds for cross-platform images
    Compose build support
    WIP: High-level build constructs (bake)
    In-container driver support (both Docker and Kubernetes)

see: http://github.com/docker/buildx
EOF

cat >${BUILD_DIR}/checkinstall_it.sh <<FOE

## Checkinstall variables - see defaults in /etc/checkinstallrc
DOC_DIR=/usr/share/doc

# make a new temporary directory for this use to avoid permission issues.
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
mkdir -p '${BASE_TMP_DIR}'


# do your work
checkinstall -y --fstrans \
	--pkgname=${PACKAGE} \
	--pkgversion=${DEB_VERSION}\
	--pkgrelease=${RELEASE} \
	--pkgarch=${ARCH} \
	--pkggroup=admin \
	--pkglicense=LICENSE \
 	--pkgsource=${LATEST_URL} \
 	--maintainer=cyteen@ring-zero.co.uk \
 	--requires=${REQUIRES} \
	--recommends=${RECOMENDS} \
	--suggests=${SUGGESTS} \
	--exclude=${EXCLUDES} \
 	-D \
 	bash ${BUILD_DIR}/install.sh
FOE

echo "Writing install.sh to: ${BUILD_DIR}/install.sh"
cat >${BUILD_DIR}/install.sh <<FOE
#!/usr/bin/env bash
set -e
#set -x

# buildx-${VERSION}.darwin-amd64
# buildx-${VERSION}.linux-amd64
# buildx-${VERSION}.linux-arm-v6
# buildx-${VERSION}.linux-arm-v7
# buildx-${VERSION}.linux-arm64
# buildx-${VERSION}.linux-ppc64le
# buildx-${VERSION}.linux-s390x

DEST_DIR=/root/.docker/cli-plugins
#mkdir -p ${DEST_DIR}
mkdir -p /root/.docker/cli-plugins
#wget -O ${DEST_DIR}/docker-buildx http://github.com/docker/${PROJECT}/releases/download/${VERSION}/buildx-${VERSION}.linux-${ARCH}
wget -O /root/.docker/cli-plugins/docker-buildx https://github.com/docker/${PROJECT}/releases/download/${VERSION}/buildx-${VERSION}.linux-${ARCH}
FOE

bash ${BUILD_DIR}/checkinstall_it.sh
