#!/bin/bash
# Phase 4 validation — run ON the clone after run-cleanup.sh + reboot:  bash /root/template-cleanup/validate-template.sh
export LC_ALL=C
P="sudo -u postgres psql -d idempiere -Atc"
echo "host: $(hostname) $(hostname -I)"
echo "--- services (expect active):"; for s in idempiere postgresql@17-main nginx postgrest docker pm2-root; do printf "%-20s %s\n" $s "$(systemctl is-active $s)"; done
echo "postfix: $(systemctl is-active postfix 2>&1) / enabled: $(systemctl is-enabled postfix 2>&1)"
echo "crontab non-comment lines: $(crontab -l 2>/dev/null | grep -cE '^[^#]' || echo 0)"
echo "postqueue: $(postqueue -p 2>&1 | tail -1)"
echo "signin http: $(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3001/signin)"
echo "cmk connections: $(cmk-agent-ctl status 2>/dev/null | grep -c Connection)"
echo "data/ entries: $(ls -A /opt/erp-nuxt-frontend/data | tr '\n' ' ')"
echo "/root/storage files: $(find /root/storage -type f 2>/dev/null | wc -l)"
echo "app-root logship_*.db: $(ls /opt/erp-nuxt-frontend/logship_*.db 2>/dev/null | wc -l)"
echo "hosts logship entries: $(grep -c logship.de /etc/hosts)"
echo "--- orgs client 1000000:"; $P "select ad_org_id||' '||name from adempiere.ad_org where ad_client_id=1000000"
echo "--- zero checks:"; for t in c_order c_invoice m_inout c_payment fact_acct m_storageonhand m_transaction r_request c_bankstatement c_contactactivity c_payselection m_inoutlinema m_attributesetinstance cust_amazoninvoiceinbox cust_incominginvoice cust_commissionactivity es_sync_queue ad_changelog; do printf "%-28s %s\n" $t "$($P "select count(*) from adempiere.$t where ad_client_id=1000000" 2>/dev/null || $P "select count(*) from adempiere.$t")"; done
echo "--- kept config:"; printf "%-28s %s\n" m_attributeset_LogShip "$($P "select count(*) from adempiere.m_attributeset where name like 'LogShip%'")"
printf "%-28s %s\n" jasper_processes "$($P "select count(*) from adempiere.ad_process where name like '%Jasper'")"
printf "%-28s %s\n" m_shipper.dhlkey_col "$($P "select count(*) from information_schema.columns where table_schema='adempiere' and table_name='m_shipper' and column_name='dhlkey'")"
printf "%-28s %s\n" c_bpartner "$($P "select count(*) from adempiere.c_bpartner where ad_client_id=1000000")"
printf "%-28s %s\n" m_product "$($P "select count(*) from adempiere.m_product where ad_client_id=1000000")"
printf "%-28s %s\n" ad_user_client "$($P "select count(*) from adempiere.ad_user where ad_client_id=1000000")"
printf "%-28s %s\n" cust_printer "$($P "select count(*) from adempiere.cust_printer")"
printf "%-28s %s\n" db_size "$(sudo -u postgres psql -Atc "select pg_size_pretty(pg_database_size('idempiere'))")"
echo "--- ES indices:"; curl -s 'http://127.0.0.1:9200/_cat/indices?h=index,docs.count' | grep -v '^\.' | tr '\n' ';'; echo
echo "--- strapi:"; sudo -u postgres psql -d strapi -Atc "select 'files '||count(*) from files union all select 'ad_attachments '||count(*) from ad_attachments union all select 'admin_users '||count(*) from admin_users"
echo "uploads: $(find /opt/mediaserver/public/uploads -type f 2>/dev/null | wc -l) files"
echo "--- env:"; grep -E '^(GITHASH|BUILDTIME|STRAPIPUBLIC|WEBAUTHN_ORIGIN|MOBILE_APP_PUBLIC_BASE|CLICK_TO_DIAL_ENABLED|LARAVEL|SENTRY_ENVIRONMENT)=' /opt/erp-nuxt-frontend/.env
echo "blanked check (should print nothing):"; grep -E "^(AMAZON_ADS_CLIENT_SECRET|ASTERISK_AMI_SECRET|PBX_CDR_PASSWORD|SEPA_CREDITOR_ID|DHLKEY|LANDING_LEAD_TOKEN|LOGSHIP_SERVICE_TOKEN|MOBILE_APP_DOWNLOAD_SECRET)=" /opt/erp-nuxt-frontend/.env | grep -vE "=''$|=$"
echo "--- disk:"; df -h / | tail -1
