#!/bin/sh
# porn-init - The pornOS init system
# ==================================
# Runs as /sbin/init on the real cum root, and as the first process in
# the initramfs. In the initramfs form it mounts the cum root and
# pivot_root's into it; on the real root it brings up the system and
# spawns getty. YOUR DRIVE IS CUM.

quiet() { "$@" >/dev/null 2>&1; }

banner() {
echo "  ╔═══════════════════════════════════════════╗"
echo "  ║        porn-INIT  v6.9                     ║"
echo "  ║   initializing your miserable existence    ║"
echo "  ╚═══════════════════════════════════════════╝"
}

mount_core() {
  echo "  mounting core filesystems..."
  quiet mount -t proc proc /proc
  quiet mount -t sysfs sysfs /sys
  quiet mount -t devtmpfs devtmpfs /dev
  quiet mkdir -p /dev/pts /dev/shm /run /tmp /sys/fs/cgroup
  quiet mount -t devpts devpts /dev/pts
  quiet mount -t tmpfs tmpfs /run
  quiet mount -t tmpfs tmpfs /tmp
  quiet mount -t cgroup2 none /sys/fs/cgroup
  ln -sfn /proc/mounts /etc/mtab
}

initramfs_main() {
  # We are PID 1 in the initramfs. Parse root= from kernel cmdline.
  root=
  for x in $(cat /proc/cmdline); do
    case "$x" in
      root=*) root="${x#root=}" ;;
    esac
  done

  quiet mkdir -p /newroot
  echo "  mounting cum root filesystem..."
  if [ -n "$root" ] && mount -t cum "$root" /newroot 2>/dev/null; then
    echo "  cum mounted from $root (your drive is cum)."
  elif mount -t cum /dev/disk/by-label/pornOS /newroot 2>/dev/null; then
    echo "  cum mounted from pornOS label."
  elif mount -t cum /dev/sda2 /newroot 2>/dev/null; then
    echo "  cum mounted from /dev/sda2."
  else
    echo "  FATAL: cannot mount cum root. you are miserably stuck."
    echo "  dropping to a rescue shell (good luck)."
    exec /bin/sh
  fi

  # move running /proc, /sys, /dev into the new root and pivot
  for m in proc sys dev run; do
    quiet mkdir -p "/newroot/$m"
    quiet mount --move "/$m" "/newroot/$m"
  done

  exec switch_root /newroot /sbin/porn-init
}

real_root_main() {
  banner

  # udev
  if command -v udevd >/dev/null 2>&1; then
    echo "  starting udev..."
    quiet udevd --daemon
    quiet udevadm trigger --action=add
    quiet udevadm settle
  fi

  # BORE scheduler confirmation (linux-very-hardened)
  if [ -r /proc/sys/kernel/sched_bore ]; then
    echo "  BORE (Burst-Oriented Response Enhancer) CPU Scheduler modification"
    echo "  kernel.sched_bore=$(cat /proc/sys/kernel/sched_bore 2>/dev/null)"
  fi

  # hostname
  if [ -f /etc/hostname ]; then
    hostname "$(cat /etc/hostname)" 2>/dev/null
  fi
  echo "  hostname: $(hostname 2>/dev/null || echo pornbox)"

  # misery daemon
  echo "  starting misery daemon..."
  quiet /usr/bin/misery-daemon &
  disown 2>/dev/null || true

  # drop to emergency shell if requested
  for x in $(cat /proc/cmdline); do
    [ "$x" = "emergency" ] && exec /bin/sh
  done

  echo ""
  echo "  pornOS is ready to fuck. enjoy your misery."
  echo ""

  # spawn getty on the 6 VTs
  for tty in 1 2 3 4 5 6; do
    if command -v agetty >/dev/null 2>&1; then
      (setsid agetty -a root -L tty$tty 38400 linux &)
    fi
  done

  # if no agetty, give a login shell on tty1 directly
  if ! command -v agetty >/dev/null 2>&1; then
    echo "  agetty not found - spawning raw shell on tty1"
    exec /bin/sh
  fi

  # reap
  while true; do
    wait
  done
}

# entry: determine whether we are in the initramfs or on the real root.
# A simple heuristic - the initramfs root is a tmpfs; the real root is cum.
tmpfs_root() { mountpoint -q / 2>/dev/null && awk '$2=="/" && $3=="tmpfs"{f=1} END{exit !f}' /proc/mounts; }

if tmpfs_root; then
  initramfs_main
else
  mount_core
  real_root_main
fi
