#!/usr/bin/env bash
echo "docker-compose is now available as docker-compose-plugin via apt."

exit 0

ARCH=$(dpkg --print-architecture)
if [[ ${ARCH} == "armhf" ]]; then ARCH="arm-v7"; fi
USER="docker"
PROJECT="compose"
# LICENSE="apache2"
# SECTION="admin"
# HOMEPAGE="https://https://docs.docker.com/compose/"
# BUILD_HOME="/var/tmp/build_docker-compose"
#VERSION="2.1.0"
PACKAGE="docker-compose"
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="person@company.tld"
# DEBFULLNAME="Testy McTester"
# DESCRIPTION="."
# LONG_DESCRIPTION=" ."

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

EXCLUDES="/root/.wget-hsts"

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

## 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 ${BUILD_DIR}

## Create and populate the install scripts and documentation for checkinstall
cat >${BUILD_DIR}/description-pak <<EOF
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.
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'
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your applications services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.

Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.

Using Compose is basically a three-step process:

1. Define your apps environment with a Dockerfile so it can be reproduced anywhere.

2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

3. Run docker-compose up and Compose starts and runs your entire app.

see: https://docs.docker.com/compose/
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

wget -O /usr/local/bin/docker-compose http://github.com/docker/compose/releases/download/${VERSION}/${PACKAGE}-${OS}-${MACHINE} && chmod +x /usr/local/bin/${PACKAGE}

FOE

bash ${BUILD_DIR}/checkinstall_it.sh
