set -e

export DEBIAN_FRONTEND=noninteractive

apt-get update
apt-get install -y gnupg

mkdir -m700 $HOME/.gnupg
gpg --no-default-keyring --keyring /usr/share/keyrings/mysql-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B7B3B788A8D3785C
echo "deb [signed-by=/usr/share/keyrings/mysql-keyring.gpg] http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7" > /etc/apt/sources.list.d/mysql.list
apt-get update

apt-get install -y --allow-downgrades python3-pip mysql-server supervisor \
                   mysql-server=5.7.42-1ubuntu18.04 mysql-client=5.7.42-1ubuntu18.04 libmysqlclient-dev=5.7.42-1ubuntu18.04 && \
apt-get -y clean
pip install git+https://github.com/coderanger/supervisor-stdout
rm -rf /var/lib/apt/lists/*
rm -rf /usr/share/doc/
rm -rf /usr/share/man/
rm -rf /usr/share/locale/
rm -rf /var/log/*

mkdir -p /var/log/supervisor /var/log/mysql
chgrp -R 0 /etc/supervisor /var/lib/mysql /var/log/supervisor /var/log/mysql
chmod -R g=u /etc/supervisor /var/lib/mysql /var/log/supervisor /var/log/mysql
sed -r -i /etc/mysql/mysql.conf.d/mysqld.cnf \
    -e 's#/var/run/mysqld/mysqld.sock#/app/tmp/sockets/mysqld.sock#' \
    -e 's#/var/run/mysqld/mysqld.pid#/app/tmp/pids/mysqld.pid#'
echo "user=1001" >> /etc/mysql/mysql.conf.d/mysqld.cnf
cat >> /etc/mysql/conf.d/mysql.cnf << EOF
[client]
socket   = /app/tmp/sockets/mysqld.sock
[mysql_upgrade]
socket   = /app/tmp/sockets/mysqld.sock
find /etc/mysql/
EOF
