#!/bin/bash
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

set -euxo pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"


cp "$DIR/new-badssl-dockerfile" Dockerfile
grep -q 'start of badssl\.test hosts' /etc/hosts || make list-hosts | sudo tee -a /etc/hosts

# we manually create this in the dockerfile. Tell the makefile not to bother to generate it.
sed -i '/ 480/c \\ttrue' certs/Makefile
# badssl does not create an expired certificate;
# it creates a certificate that expires after 1 day and waits for 1 day to run the "expired certificate" test.
# This command patches this behavior to run the test immediately.
# See: https://github.com/chromium/badssl.com/blob/df8d5a9d062f4b99fc19d8aacdea5333b399d624/certs/Makefile#L177
sed -i 's%./tool sign $@ $(D) 1 sha256 req_v3_usr $^%faketime -f "-2d" ./tool sign $@ $(D) 1 sha256 req_v3_usr $^%' certs/Makefile
# there is a command "make serve" We don't want to actually run that because we want to error out early on `docker build`
sudo make certs-test
sudo make docker-build

# manually invoke the "serve" part of things
# if things are broken, try removing the screen session to see any failure logs.
screen -dmS badssl sudo docker run -t -p 80:80 -p 443:443 -p 1000-1024:1000-1024 badssl
