#!/bin/bash
set -e
make installer
(
    cp capp-installer test/
    cd test
    DOCKER_BUILDKIT=1 docker build --network host -t capptest .
    rm capp-installer
)
cat <<'EOF'
`capp` will run in a container.
You can reach the container with `docker exec -ti capptest /bin/bash`.
`tmux` is available in the container so feel free to use it.

The following ports are binded (local → container):
    1022 → 22
    1122 → 122
    1080 → 80
    1443 → 443

You can ssh with:
    ssh -p 1022 root@127.0.0.1
Password is root

The server is configured to use `test.capp` as main domain.
To test your application, get the container’s IP with `IP=$(docker inspect capptest|sed -rn '/"IPAddress"/{s/.*: "(.+)",/\1/;p;q}')`
Then add temporary the IP to your `/etc/hosts` file:
    IP   test.capp myapp.test.capp anysubdomain.test.capp
EOF
trap 'docker kill capptest; docker rmi capptest' EXIT
docker run \
    --name capptest \
    --rm \
    --privileged \
    -p 1022:22 \
    -p 1122:122 \
    -p 1080:80 \
    -p 1443:443 \
    -d \
    capptest
echo "Wait for systemd to start"
sleep 1
docker exec -t capptest /root/capp-installer test.capp mail@example.com "$@"
echo "capptest IP is $(docker inspect capptest|sed -rn '/"IPAddress"/{s/.*: "(.+)",/\1/;p;q}')"
echo "please temporarily add '$(docker inspect capptest|sed -rn '/"IPAddress"/{s/.*: "(.+)",/\1/;p;q}') test.capp myapp.test.capp' to your /etc/hosts file"
echo ""
echo "hit Ctrl-C to stop and exit"
docker logs -f capptest
