#!/bin/bash
#
# Vaultwarden Service

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

config_vaultwarden() {
  echo -ne "\n* Configuring /federated/apps/vaultwarden container.."

  if [ ! -d "/federated/apps/vaultwarden" ]; then
    mkdir -p /federated/apps/vaultwarden/data/data
  fi
  VAULTWARDEN_SECRET=$(create_password)

cat > /federated/apps/vaultwarden/docker-compose.yml <<EOF
services:
  vaultwarden:
    image: vaultwarden/server:\${IMAGE_VERSION}
    container_name: vaultwarden
    hostname: vaultwarden.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.22
    env_file:
      - ./.env
    volumes:
      - ./data/data:/data
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.vaultwarden.rule=Host(\`vaultwarden.$DOMAIN\`)"
      - "traefik.http.routers.vaultwarden.entrypoints=websecure"
      - "traefik.http.routers.vaultwarden.tls.certresolver=letsencrypt"

networks:
  core:
    external: true
EOF

cat > /federated/apps/vaultwarden/.env <<EOF
IMAGE_VERSION="$(current_version vaultwarden)"
DATABASE_URL=postgresql://vaultwarden:$VAULTWARDEN_SECRET@postgresql.$DOMAIN:5432/vaultwarden
WEBSOCKET_ENABLED=true
DOMAIN=https://vaultwarden.$DOMAIN
ADMIN_TOKEN=$VAULTWARDEN_SECRET
#- SIGNUPS_ALLOWED=false
SMTP_HOST=mail.$DOMAIN
SMTP_FROM=vaultwarden@$DOMAIN
SMTP_PORT=587
SMTP_SECURITY=starttls
SMTP_USERNAME=fcore
SMTP_PASSWORD=$ADMINPASS
SIGNUPS_ALLOWED=false
SIGNUPS_DOMAINS_WHITELIST=$DOMAIN
SIGNUPS_VERIFY=true
EOF
chmod 600 /federated/apps/vaultwarden/.env

# Create database and user in postgresql
docker exec postgresql psql -U postgres -c "CREATE DATABASE vaultwarden" &> /dev/null
docker exec postgresql psql -U postgres -c "CREATE USER vaultwarden WITH PASSWORD '$VAULTWARDEN_SECRET'" &> /dev/null
docker exec postgresql psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE vaultwarden TO vaultwarden" &> /dev/null
 
echo -ne "done."
}
start_vaultwarden() {
  # Start service with command to make sure it's up before proceeding
  start_service "vaultwarden" "nc -z 192.168.0.22 80 &> /dev/null" "8"

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

  echo -ne "done."
}
uninstall_vaultwarden() {
  echo -ne "* Uninstalling vaultwarden container.."
  spin &
  SPINPID=$!

  # First stop the service
  cd /federated/apps/vaultwarden && docker compose -f docker-compose.yml -p vaultwarden down &> /dev/null

  # Delete database and user in postgresql
  docker exec postgresql psql -U postgres -c "DROP DATABASE vaultwarden" &> /dev/null
  docker exec postgresql psql -U postgres -c "DROP USER vaultwarden" &> /dev/null

  # Delete the app directory
  rm -rf /federated/apps/vaultwarden

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
email_vaultwarden() {
  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>Vaultwarden is now installed on $DOMAIN</h4>
<p>
Here is your applications chart with 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;word-wrap:break-word}
.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-kwiq">Vaultwarden</td>
    <td class="tg-kwiq"><a href="https://vaultwarden.$DOMAIN" target="_blank" rel="noopener noreferrer"><span style="color:#340096">vaultwarden.$DOMAIN</span></a></td>
    <td class="tg-kwiq">Create your own user</td>
    <td class="tg-kwiq">User access is separate from panel. Create your own initial user</td>
    <td class="tg-kwiq"><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-kwiq">Vaultwarden is a password manager</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"
}
