#!/bin/sh
# Nextcloud/Ubuntu recovery diagnostics — run from Ubuntu installer ISO shell
# Usage: wget -O /tmp/nc-check.sh https://DEIN-SERVER/nc-check.sh && sh /tmp/nc-check.sh

R=/tmp/nc-report.txt
exec >"$R" 2>&1

echo "========== NC-CHECK START =========="
date
echo

echo "========== LSBLK =========="
lsblk -f
echo

echo "========== LVM =========="
vgchange -ay || true
vgs || true
lvs || true
echo

echo "========== MOUNT TARGET =========="
mkdir -p /target
# Root (LVM) — adjust only if your LV name differs
if [ -e /dev/mapper/ubuntu--vg-lv--0 ]; then
  mount /dev/mapper/ubuntu--vg-lv--0 /target || true
else
  echo "WARN: /dev/mapper/ubuntu--vg-lv--0 missing"
  ls -la /dev/mapper/ || true
fi

# Typical Ubuntu layout on sda; ignore errors if already mounted
mount /dev/sda2 /target/boot || true
mount /dev/sda1 /target/boot/efi || true

echo "========== MOUNTS =========="
mount
echo

echo "========== HOSTNAME =========="
cat /target/etc/hostname 2>/dev/null || echo "no hostname file"
echo

echo "========== FSTAB =========="
cat /target/etc/fstab 2>/dev/null || echo "no fstab"
echo

echo "========== HOME =========="
ls -la /target/home 2>/dev/null || echo "no /home"
echo

echo "========== PASSWD (human users) =========="
cat /target/etc/passwd 2>/dev/null || echo "no passwd"
echo

echo "========== SHADOW exists? =========="
ls -l /target/etc/shadow 2>/dev/null || echo "no shadow"
echo

echo "========== INIT / SYSTEMD =========="
ls -l /target/sbin/init /target/bin/sh /target/usr/lib/systemd/systemd 2>/dev/null || true
echo

echo "========== EFI =========="
ls -la /target/boot/efi 2>/dev/null || true
ls -la /target/boot/efi/EFI 2>/dev/null || true
ls -la /target/boot/efi/EFI/ubuntu 2>/dev/null || true
ls -la /target/boot/efi/EFI/BOOT 2>/dev/null || true
echo

echo "========== WEBMIN =========="
ls -la /target/etc/webmin 2>/dev/null || echo "no /etc/webmin"
ls -la /target/usr/share/webmin 2>/dev/null || echo "no /usr/share/webmin"
echo

echo "========== WWW / NEXTCLOUD =========="
ls -la /target/var/www 2>/dev/null || echo "no /var/www"
ls -la /target/var/www/nextcloud 2>/dev/null || echo "no /var/www/nextcloud"
ls -la /target/var/www/html 2>/dev/null || echo "no /var/www/html"
ls -la /target/var/www/html/nextcloud 2>/dev/null || echo "no html/nextcloud"
echo

echo "========== NEXTCLOUD config.php =========="
if [ -f /target/var/www/nextcloud/config/config.php ]; then
  cat /target/var/www/nextcloud/config/config.php
elif [ -f /target/var/www/html/nextcloud/config/config.php ]; then
  cat /target/var/www/html/nextcloud/config/config.php
else
  echo "config.php not found in usual paths"
  find /target/var/www -name config.php 2>/dev/null
fi
echo

echo "========== NEXTCLOUD data dir listing =========="
# Will also show path from config if present; try common locations
ls -la /target/var/www/nextcloud/data 2>/dev/null || true
ls -la /target/var/www/html/nextcloud/data 2>/dev/null || true
ls -la /target/mnt/ncdata 2>/dev/null || true
ls -la /target/data/nextcloud 2>/dev/null || true
echo

echo "========== DATABASE dirs (presence only) =========="
ls -la /target/var/lib/mysql 2>/dev/null || echo "no mysql datadir"
ls -la /target/var/lib/postgresql 2>/dev/null || echo "no postgresql datadir"
echo

echo "========== INSTALLER / CURTIN =========="
ls -la /target/var/log/installer 2>/dev/null || echo "no installer log dir"
tail -n 100 /target/var/log/installer/curtin-install.log 2>/dev/null || echo "no curtin-install.log"
echo

echo "========== APT sources =========="
cat /target/etc/apt/sources.list 2>/dev/null || true
ls /target/etc/apt/sources.list.d 2>/dev/null || true
echo

echo "========== DISK USAGE (target) =========="
df -h /target /target/boot /target/boot/efi 2>/dev/null || true
echo

echo "========== NC-CHECK DONE =========="
echo "Report saved to: $R"
echo "Upload example (if your server allows PUT):"
echo "  wget --method=PUT --body-file=$R -O- https://DEIN-SERVER/upload/nc-report.txt"
echo "Or: cat $R   (screenshot pages)"
