#!/bin/bash
# Bind host data directories into the packaged rootfs and run the original entrypoint.
set -euo pipefail

ROOT=/opt/umgrid/rootfs
HOST=/var/lib/umgrid
STATE=/run/umgrid

log() {
  echo "umgrid-start: $*" >&2
}

record() {
  echo "$1" >>"$STATE/mounts"
}

bind_mount() {
  local src=$1
  local dst=$2
  mkdir -p "$src"
  mkdir -p "$dst"
  if mountpoint -q "$dst"; then
    log "already mounted $dst"
  else
    mount --bind "$src" "$dst"
  fi
  record "$dst"
}

if [ ! -d "$ROOT/data/umapp" ]; then
  log "rootfs is incomplete: $ROOT"
  exit 1
fi

mkdir -p "$STATE" \
  "$HOST/config" "$HOST/mysql" "$HOST/log" "$HOST/file" "$HOST/upload" "$HOST/download" \
  "$ROOT/proc" "$ROOT/sys" "$ROOT/dev" \
  "$ROOT/data/config" "$ROOT/data/log" "$ROOT/var/lib/mysql" \
  "$ROOT/data/umapp/app/lua" \
  "$ROOT/data/umapp/app/html/file" \
  "$ROOT/data/umapp/app/html/apps/download" \
  "$ROOT/data/umapp/app/upload" \
  "$ROOT/run/php" "$ROOT/run/mysqld" "$ROOT/run/lock" \
  "$ROOT/var/log/nginx" "$ROOT/var/log/mysql" \
  "$ROOT/tmp" "$ROOT/var/tmp" \
  "$ROOT/etc/mysql/mariadb.conf.d"

: >"$STATE/mounts"

if [ ! -f "$HOST/config/umconfig.lua" ] || [ ! -f "$HOST/config/run.sh" ]; then
  log "missing generated config in $HOST/config"
  exit 1
fi

if [ -L "$ROOT/etc/resolv.conf" ] || [ ! -e "$ROOT/etc/resolv.conf" ]; then
  rm -f "$ROOT/etc/resolv.conf"
  touch "$ROOT/etc/resolv.conf"
fi
resolv=$(readlink -f /etc/resolv.conf || true)
if [ -n "${resolv}" ] && [ -f "$resolv" ]; then
  if ! mountpoint -q "$ROOT/etc/resolv.conf"; then
    mount --bind "$resolv" "$ROOT/etc/resolv.conf"
  fi
  record "$ROOT/etc/resolv.conf"
fi

bind_mount "$HOST/config" "$ROOT/data/config"
bind_mount "$HOST/mysql" "$ROOT/var/lib/mysql"
bind_mount "$HOST/log" "$ROOT/data/log"
bind_mount "$HOST/file" "$ROOT/data/umapp/app/html/file"
bind_mount "$HOST/download" "$ROOT/data/umapp/app/html/apps/download"
bind_mount "$HOST/upload" "$ROOT/data/umapp/app/upload"

if ! mountpoint -q "$ROOT/proc"; then
  mount --bind /proc "$ROOT/proc"
fi
record "$ROOT/proc"
if ! mountpoint -q "$ROOT/sys"; then
  mount --bind /sys "$ROOT/sys"
fi
record "$ROOT/sys"
if ! mountpoint -q "$ROOT/dev"; then
  mount --bind /dev "$ROOT/dev"
fi
record "$ROOT/dev"
if ! mountpoint -q "$ROOT/dev/pts"; then
  mount --bind /dev/pts "$ROOT/dev/pts"
fi
record "$ROOT/dev/pts"
if ! mountpoint -q "$ROOT/dev/shm"; then
  mount --bind /dev/shm "$ROOT/dev/shm"
fi
record "$ROOT/dev/shm"

# The image has no /etc/hosts; Docker creates it at container start.
if [ ! -e "$ROOT/etc/hosts" ] || [ -L "$ROOT/etc/hosts" ]; then
  rm -f "$ROOT/etc/hosts"
  printf '127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\n' >"$ROOT/etc/hosts"
fi
chmod 1777 "$HOST/log" || true
if [ ! -f "$ROOT/etc/hosts.umgrid-base" ]; then
  cat "$ROOT/etc/hosts" >"$ROOT/etc/hosts.umgrid-base"
fi
cat "$ROOT/etc/hosts.umgrid-base" >"$ROOT/etc/hosts"

ln -sfn /data/config/umconfig.lua "$ROOT/data/umapp/app/lua/umconfig.lua"
# Frontend loads /static/umconfig.js; without this link nginx falls back to index.html
# and the browser keeps a wrong websocket host (login hangs on “登录中...”).
mkdir -p "$ROOT/data/umapp/app/html/static"
ln -sfn /data/config/umconfig.js "$ROOT/data/umapp/app/html/static/umconfig.js"
if [ -f "$HOST/config/50-charactors.cnf" ]; then
  ln -sfn /data/config/50-charactors.cnf "$ROOT/etc/mysql/mariadb.conf.d/50-charactors.cnf"
fi

chmod 1777 "$ROOT/tmp" "$ROOT/var/tmp" || true

# mysqld_safe drops to the mysql user. The image ships /run/mysqld as root:root,
# so the daemon cannot create its socket until that directory is owned by mysql.
mysql_uid=$(awk -F: '$1=="mysql" {print $3}' "$ROOT/etc/passwd")
mysql_gid=$(awk -F: '$1=="mysql" {print $4}' "$ROOT/etc/passwd")
mkdir -p "$ROOT/run/mysqld"
if [ -n "$mysql_uid" ] && [ -n "$mysql_gid" ]; then
  chown "$mysql_uid:$mysql_gid" "$ROOT/run/mysqld"
fi
chmod 755 "$ROOT/run/mysqld"

# dockerrun_full.sh starts php-fpm, mariadb, nginx, coturn and cron. It does not start sshd.
exec chroot "$ROOT" /bin/bash /data/umapp/linux/dockerrun_full.sh
