#!/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
cd web-vault

wget https://github.com/dani-garcia/bw_web_builds/raw/refs/heads/master/patches/v2025.4.1.patch
git apply v2025.4.1.patch

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

cd ..

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