#!/bin/bash

# Copyright (C) 2013-2025 Marcin Krol <hawk@tld-linux.org>
# All Rights Reserved

if ! [ -f "/lib/cri/boot" ]; then
  if [ "$(tty 2>/dev/null)" = "/dev/tty1" ]; then
    touch /lib/cri/boot
    rootpw=$(tr -dc 'A-Za-z0-9' </dev/urandom 2>/dev/null | head -c 10 2>/dev/null)
    rootpwhash=$(mkpwdhash -m yescrypt "${rootpw}" 2>/dev/null)
    usermod -p "${rootpwhash}" root 1>/dev/null 2>&1
    cat <<EOF

Current root password: [1;37m${rootpw}[0m
Save or change it to be able to log in.

List of special commands:

[0;32msyschroot[0m  - chroot into installed system
[0;32msysconfig[0m  - configure system installation
[0;32msysinit[0m    - initialize installed system
[0;32msysinstall[0m - install system image
[0;32mhowto[0m      - display installation howto
[0;32mlsmedia[0m    - list available block devices
[0;32mnetconfig[0m  - configure network
[0;32mpcimod[0m     - list kernel modules for detected PCI devices

EOF
    unset rootpw
    unset rootpwhash
    sed -i -e '/^1:/s/--autologin root --noclear //' /etc/inittab 1>/dev/null 2>&1
    sed -i -e '/^\/lib\/cri\/bootinit/d' /etc/profile 1>/dev/null 2>&1
    init q 1>/dev/null 2>&1
  fi
fi
