#!/bin/bash

apt install libmariadb-dev -y

mkdir /home/vw
cd /home/vw
git init
git pull https://github.com/dani-garcia/vaultwarden
cargo build --features sqlite

mkdir /usr/local/vaultwarden
mv target/debug/vaultwarden /usr/local/vaultwarden

cd /usr/local/vaultwarden
git init web-vault
git -C web-vault pull https://github.com/bitwarden/clients.git

ENV=selfhosted NODE_OPTIONS="--max-old-space-size=4096" NODE_ENV=production bun i --cwd web-vault/apps/web --no-save
ENV=selfhosted NODE_OPTIONS="--max-old-space-size=4096" NODE_ENV=production bun --cwd web-vault/apps/web webpack

mv web-vault/apps/web/build /tmp
rm -r web-vault
mv /tmp/build web-vault

cp /home/vw/.env.template .env

rm -r /home/vw