#!/bin/bash
#
# Bookstack Service

PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

config_bookstack() {
  echo -ne "\n* Configuring bookstack container.."
  spin &
  SPINPID=$!

  if [ ! -d "/federated/apps/bookstack" ]; then
    mkdir -p /federated/apps/bookstack/data/config
  fi

cat > /federated/apps/bookstack/docker-compose.yml <<EOF
version: "3.7"
services:
  bookstack:
    image: lscr.io/linuxserver/bookstack:\${IMAGE_VERSION}
    container_name: bookstack
    hostname: bookstack.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.36
    env_file:
      - ./.env
    volumes:
      - ./data/config:/config
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.bookstack.rule=Host(\`bookstack.$DOMAIN\`,\`documentation.$DOMAIN\`)"
      - "traefik.http.routers.bookstack.entrypoints=websecure"
      - "traefik.http.routers.bookstack.tls.certresolver=letsencrypt"

networks:
  federated:
    external: true
EOF

EXTERNALIP=$(get_externalip);
ADMINPASS=`cat /federated/apps/panel/.env | grep SMTP_PASSWORD | awk -F= '{ print $2 }'`
BOOKSTACK_ADMINPASSWORD=`htpasswd -bnBC 10 "" $ADMINPASS | tr -d ':\n' | sed 's/$2y/$2b/'`
BOOKSTACK_SECRET=$(create_password);
MYSQL_ROOTPASSWORD=`cat /federated/apps/pdnsmysql/.env | grep MYSQL_ROOT_PASSWORD | awk -F= '{ print $2 }'`
# Set SMTP user based on if fcore exists
[[ $(docker exec ldap slapcat | grep fcore) ]] && SMTPUSER="fcore" || SMTPUSER="admin"

cat > /federated/apps/bookstack/.env <<EOF
IMAGE_VERSION="23.08.3"
PUID=1000
PGID=1000
APP_URL=https://bookstack.$DOMAIN
DB_HOST=pdnsmysql.$DOMAIN
DB_PORT=3306
DB_USER=bookstack
DB_PASS=$BOOKSTACK_SECRET
DB_PORT=3306
DB_DATABASE=bookstack
QUEUE_CONNECTION=database
#MAIL_DRIVER=smtp
MAIL_FROM=bookstack@$DOMAIN
MAIL_FROM_NAME=bookstack
MAIL_HOST=mail.$DOMAIN
MAIL_PORT=587
MAIL_USERNAME=$SMTPUSER
MAIL_PASSWORD=$ADMINPASS
MAIL_ENCRYPTION=TLS
EOF
chmod 600 /federated/apps/bookstack/.env

# Create database and user in mysql
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'create database bookstack;'" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"CREATE USER 'bookstack'@'%' IDENTIFIED BY '$BOOKSTACK_SECRET';\"" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"grant all privileges on bookstack.* to 'bookstack'@'%';\"" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'flush privileges;'" &> /dev/null
 
kill -9 $SPINPID &> /dev/null
echo -ne "done.\n"
}
start_bookstack() {
  # Start service with command to make sure it's up before proceeding
  start_service "bookstack" "nc -z 172.99.0.36 80 &> /dev/null" "7"

  docker exec pdns pdnsutil add-record $DOMAIN bookstack A 86400 $EXTERNALIP &> /dev/null
  [ $? -ne 0 ] && fail "Couldn't add dns record for bookstack"
  docker exec pdns pdnsutil add-record $DOMAIN documentation A 86400 $EXTERNALIP &> /dev/null
  [ $? -ne 0 ] && fail "Couldn't add dns record for bookstack"

  PDNS_MYSQL_COMMAND="update users set password=\"$BOOKSTACK_ADMINPASSWORD\" where id = \"1\";" &> /dev/null
  docker exec pdnsmysql bash -c "mysql -ubookstack -p$BOOKSTACK_SECRET bookstack -e '$PDNS_MYSQL_COMMAND;'" &> /dev/null
  docker exec pdnsmysql bash -c "mysql -ubookstack -p$BOOKSTACK_SECRET bookstack -e \"update users set email='admin@$DOMAIN' where id = 1;\"" &> /dev/null

  # Set mail configuration
  sed -i "s#MAIL_FROM=.*#MAIL_FROM=admin@$DOMAIN#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_FROM_NAME=.*#MAIL_FROM_NAME=admin#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_HOST=.*#MAIL_HOST=mail.$DOMAIN#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_PORT=.*#MAIL_PORT=587#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_USERNAME=.*#MAIL_USERNAME=admin#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_PASSWORD=.*#MAIL_PASSWORD=$ADMINPASS#g" /federated/apps/bookstack/data/config/www/.env
  sed -i "s#MAIL_ENCRYPTION=.*#MAIL_ENCRYPTION=TLS#g" /federated/apps/bookstack/data/config/www/.env

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
email_bookstack() {
  echo -ne "* Sending email to customer.."
  spin &
  SPINPID=$!

cat > /federated/apps/mail/data/root/certs/mailfile <<EOF
<html>
<img src="https://www.federated.computer/wp-content/uploads/2023/11/logo.png" alt="" /><br>
<p>
<h4>Bookstack is now installed on $DOMAIN</h4>
<p>
Here is your applications chart on how to access this service:<br>
<p>
<h4>Applications</h4>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-cul6{border-color:inherit;color:#340096;text-align:left;text-decoration:underline;vertical-align:top}
.tg .tg-acii{background-color:#FFF;border-color:inherit;color:#333;text-align:left;vertical-align:top}
.tg .tg-0hty{background-color:#000000;border-color:inherit;color:#ffffff;font-weight:bold;text-align:left;vertical-align:top}
.tg .tg-kwiq{border-color:inherit;color:#000000;text-align:left;vertical-align:top}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 996px">
<colgroup>
<col style="width: 101.333333px">
<col style="width: 203.333333px">
<col style="width: 282.333333px">
<col style="width: 185.33333px">
<col style="width: 78.333333px">
<col style="width: 220.333333px">
</colgroup>
<thead>
  <tr>
    <th class="tg-0hty">Service</th>
    <th class="tg-0hty">Link</th>
    <th class="tg-0hty">User / Pass</th>
    <th class="tg-0hty">Access</th>
    <th class="tg-0hty">Docs</th>
    <th class="tg-0hty">Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td class="tg-0pky">Bookstack</td>
    <td class="tg-0pky"><a href="https://bookstack.$DOMAIN/login" target="_blank" rel="noopener noreferrer"><span style="color:#340096">bookstack.$DOMAIN</span></a></td>
    <td class="tg-0pky">admin@$DOMAIN<br>$ADMINPASS</td>
    <td class="tg-0pky">User access is separate from panel</td>
    <td class="tg-cul6"><a href="https://documentation.federated.computer/docs/getting_started/welcome/" target="_blank" rel="noopener noreferrer"><span style="color:#340096">Click here</span></a></td>
    <td class="tg-0pky">Bookstack is a simple, open-source, self-hosted, easy-to-use platform (Wiki) for organising and storing information</td>
  </tr>
</tbody>
</table>
<h4>Thanks for your support!</h4>
<p>
Thank you for your support of Federated Computer. We really appreciate it and hope you have a very successful
time with Federated Core.
<p>
Again, if we can be of any assistance, please don't hesitate to get in touch.
<p>
Support: https://support.federated.computer<br>
Phone: (970) 722-8715<br>
Email: support@federated.computer<br>
<p>
It's <b>your</b> computer. Let's make it work for you!
</html>
EOF

  # Send out e-mail from mail container with details
  docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"Application installed on $DOMAIN\" $EMAIL < /root/certs/mailfile"
  rm /federated/apps/mail/data/root/certs/mailfile

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
