#!/bin/sh
# rc.S:  Basic system initialization.

# Any /etc/mtab that exists here is old, so we start with a new one:
/bin/rm -f /etc/mtab{,~,.tmp} && /bin/touch /etc/mtab

# Add (fake) entry for / to /etc/mtab:
/sbin/mount -f -w /dev/initramfs / -t tmpfs 1> /dev/null

# Mount /proc:
/sbin/mount -v proc /proc -t proc 1> /dev/null

# Mount sysfs next:
/sbin/mount -v sysfs /sys -t sysfs 1> /dev/null

# Activate swap:
/sbin/swapon -a 1> /dev/null

if [ -x /sbin/ldconfig ]; then
  /sbin/ldconfig 1> /dev/null
fi

export TEXTDOMAIN=slint
. gettext.sh
. /usr/lib/setup/SeTlocales

## Detect serial console from kernel command line:
#if cat /proc/cmdline | grep console=ttyS 1> /dev/null 2> /dev/null ; then
#  SERIAL_CONSOLE="true"
#fi

# System logger (mostly to eat annoying messages):
/sbin/syslogd 2> /dev/null
sleep 1
/sbin/klogd -c 3 1> /dev/null

# Try to load the loop module:
modprobe loop 1> /dev/null 2> /dev/null

# Run udev:
if ! grep -wq noudev /proc/cmdline ; then
  /bin/bash /etc/rc.d/rc.udev start

  # Re-assemble RAID volumes:
  /sbin/mdadm -E -s > /etc/mdadm.conf
  /sbin/mdadm -S -s
  /sbin/mdadm -A -s
  # This seems to make the kernel see partitions more reliably:
  fdisk -l /dev/md* 1> /dev/null 2> /dev/null
else

  # Look for USB keyboard or storage:
  /etc/rc.d/rc.usb start
  sleep 3

  # Look for IEEE1394 devices:
  if grep 1394 /proc/pci 1> /dev/null 2> /dev/null ; then
    /etc/rc.d/rc.ieee1394 start
    #sleep 3
  fi

  ### PROBABLY USELESS WITHOUT SOME TIMED DELAY ABOVE
  ## Start USB again (in case we missed a USB keyboard)
  #/etc/rc.d/rc.usb start

  # Make detected partitions:
  /dev/makedevs.sh

  # Re-assemble RAID volumes:
  /sbin/mdadm -E -s > /etc/mdadm.conf
  /sbin/mdadm -S -s
  /sbin/mdadm -A -s
  # This seems to make the kernel see partitions more reliably:
  fdisk -l /dev/md* 1> /dev/null 2> /dev/null

  # Check /proc/partitions again:
  /dev/makedevs.sh

  # Create LVM nodes:
  /dev/devmap_mknod.sh

fi # End Run udev:

sleep 1

# Start brltty if it has not yet been started by udev - Didier Spaier
NBPROC="`ps -C brltty --noheaders|wc -l`"
if [ "$NBPROC" = "0" ]; then
	echo "starting brltty"
	/bin/brltty
sleep 2
fi

touch /.today
/bin/sh /sbin/fakedate

if [ -x /etc/rc.d/rc.inet1 ]; then
  /bin/sh /etc/rc.d/rc.inet1
fi

# pcmciautils is installing rc.pcmcia as chmod 644, so we'll change that.
# It won't be run at boot time, but it'll make it easy for the pcmcia script
# or to run it from the command line.
chmod 755 /etc/rc.d/rc.pcmcia

# Scan for existing LVM partitions:
# We will run 'vgscan -ay' in the setup to prevent a 10 second sleep;
vgscan --mknodes 2> /tmp/foo
cat /tmp/foo | uniq
rm -f /tmp/foo

# Activate LVM volumes:
echo "Activating LVM volumes:"
vgchange -ay

/bin/sh /etc/rc.d/rc.font

# Don't automatically blank the screen, or it will go black during the install
# process when stray keystrokes might be dangerous:
/bin/setterm -blank 0

TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi

TMP=/var/log/setup/tmp
if grep -wq brltty /proc/cmdline; then
  if [ ! -r /dev/tty1 ]; then
    mknod /dev/tty1 c 4 1
    chown root:tty /dev/tty1
    chmod 620 /dev/tty1
  fi
  if [ ! -r /dev/tty2 ]; then
    mknod /dev/tty2 c 4 2
    chown root:tty /dev/tty2
    chmod 620 /dev/tty2
  fi
  if [ ! -r /dev/tty3 ]; then
    mknod /dev/tty3 c 4 3
    chown root:tty /dev/tty3
    chmod 620 /dev/tty3
  fi
  if [ ! -r /dev/tty4 ]; then
    mknod /dev/tty4 c 4 4
    chown root:tty /dev/tty4
    chmod 620 /dev/tty4
  fi
    /bin/stty -F /dev/tty1 cols 80 rows 25
    /bin/stty -F /dev/tty2 cols 80 rows 25
    /bin/stty -F /dev/tty3 cols 80 rows 25
    /bin/stty -F /dev/tty4 cols 80 rows 25
fi

kbd_stored="`cat /tmp/Pkeymap`"
kbd=${kbd_stored%.*}
#SavedLANG=$LANG
#LANG=en_US.utf8
ll_TT=${LANG%.*}
SeTFonts
PutFonts
set_voice () {
	locale="$1"
	rawlocale=${locale%.utf8}
	lang=$(echo $rawlocale |cut -d_ -f1)
	country=$(echo $rawlocale |cut -d_ -f2|tr A-Z a-z)
	if [ "$lang" = gl ]; then
		# Not supported by espeak yet, but pt should be fine enough, better
		# than English anyway
		# Comment above from Debian. We don't propose gv in Slint yet, but
		# maybe later ?
		lang=pt
	fi

	if grep -qr "^language $lang-$country$" /usr/share/espeak-ng-data/lang; then
		VOICE="$lang-$country"
	elif grep -qr "^language $lang$" /usr/share/espeak-ng-data/lang; then
		VOICE="$lang"
	else
		VOICE=en
	fi
	echo $VOICE > /tmp/speech
}
# Taken from Debian
. /usr/share/alsa/utils.sh
preinit_levels all
sanify_levels all
# speakup initialization.
# If a hard synth is set in the command line (how do I provide this ability
# with grub?) no need to start espeakup.
if ! grep -q speakup /proc/cmdline; then
	modprobe speakup_soft
	echo "soft" > /sys/accessibility/speakup/synth
	sleep 1
	# chmod 666 not needed in the installer, we are root.
	# chmod 666 /sys/accessibility/speakup/soft/*
	echo 1 > /sys/accessibility/speakup/soft/direct
else
	touch /tmp/noespeakup
fi
# End speakup initialization
# Mostly from Debian
if [ ! -f /tmp/noespakeup ]; then
	set_voice $LANG
	# Probe the sound cards as in Debian
	sh /usr/sbin/probe_sound_cards
	sleep 2
fi
# If a working sound card has been found, its id has been written in
# /tmp/alsacard, else /tmp/noespeakup has been written
if [ ! -f /tmp/noespeakup ]; then
	sleep 2
	clear
	read -N 10000000 -t 0.01
	gettext  "This installer has speech and Braille device support in order
to be accessible to visually impaired users. To have speech turned on
during installation type S, then press [Enter]. Else, just press Enter.
"
	read sound
	if [ ! "$sound" = "s" ] && [ ! "$sound" = "S" ]; then
		PID=$(ps -C espeakup --noheaders -o pid)
		kill -s 9 $PID
		sleep 1
		touch /tmp/noespeakup
	fi
fi
clear

if [ ! -f /tmp/noespeakup ] || grep -q speakup /proc/cmdline; then
	gettext  "To display keyboard shortcuts for reviewing the screen with speech
press R, then press [Enter],
Else, just press [Enter]: "
	read ANSWER
	if [ "$ANSWER" = "R" ] ||  [ "$ANSWER" = "r" ]; then
		clear
		/bin/less /Speakup.txt
		read
	fi
fi
clear
INITLANG=$LANG
TITLE=$(gettext "LANGUAGE TO USE DURING INSTALLATION")
dialog --visit-items --no-tags --no-cancel --nook --title "$TITLE" --default-item "$INITLANG" --menu \
"$(eval_gettext "Please select the language to use for installation.")" 0 0 0 \
en_US.utf8 "$(gettext "English (USA)")" \
nl_NL.utf8 "$(gettext "Dutch")" \
fr_FR.utf8 "$(gettext "French")" \
de_DE.utf8 "$(gettext "German")" \
el_GR.utf8 "$(gettext "Greek")" \
it_IT.utf8 "$(gettext "Italian")" \
nb_NO.utf8 "$(gettext "Norwegian")" \
pl_PL.utf8 "$(gettext "Polish")" \
pt_BR.utf8 "$(gettext "Portuguese (Brazil)")" \
pt_PT.utf8 "$(gettext "Portuguese (Portugal)")" \
ru_RU.utf8 "$(gettext "Russian")" \
es_CR.utf8 "$(gettext "Spanish (Latin America)")" \
sk_SK.utf8 "$(gettext "Slovak")" \
sv_SE.utf8 "$(gettext "Swedish")" \
tr_TR.utf8 "$(gettext "Turkish")" \
uk_UA.utf8 "$(gettext "Ukrainian")" \
 2> /tmp/.SlintInstallationLANG
if [ $? -ne 0 ]; then
	echo $INITLANG > /tmp/.SlintInstallationLANG
fi
LANG=$(cat /tmp/.SlintInstallationLANG)
ll_TT=${LANG%.*}
SeTlocales
SeTKeymap
SeTFonts
PutFonts
if [ ! -f /tmp/noespeakup ] && [ ! "$INITLANG" = "$LANG" ]; then
	PID=$(ps -C espeakup --noheaders -o pid)
	kill -s 9 $PID
	sleep 1
	set_voice $LANG
	ALSA_CARD=$(cat /tmp/alsacard)
	ALSA_CARD=$ALSA_CARD /usr/bin/espeakup -V $(cat /tmp/speech) >> /tmp/espeakup.log 2>&1
	echo "ALSA_CARD=$ALSA_CARD /usr/bin/espeakup -V $(cat /tmp/speech)" >> /tmp/report
fi
clear

eval_gettext "<OPTION TO LOAD SUPPORT FOR ANOTHER KEYBOARD>

If you are not using a \"\$kbd\" keyboard map, you may load another one now.
To select a different keyboard map, please enter 1 now.
To continue using the \"\$kbd\" keyboard map, just hit [Enter]."
echo
echo
gettext "Enter 1 to select a keyboard map: "
read ONE
if [ "$ONE" = "1" ]; then
  /usr/lib/setup/SeTkeymap
fi
clear
. /etc/profile

# Provision for unattended network configuration:
/usr/lib/setup/SeTnet boot
# Start dropbear ssh server (only if a configured interface is present):
/etc/rc.d/rc.dropbear start

# Fake login:  (fooled ya! ;^)
#
Version=14.2.1
GCOUNTMIN=20

eval_gettext "Welcome to the Slint installer! (version \${Version})

Available commands (do not type the quotes):

'doc' to know the features and usage of the installer.
'auto' to start a semi-automatic, guided installation.
'setup' to start a manual installation.

We recommend that you type 'doc' first to prepare a manual installation, or if
you want to encrypt the drive where Slint will be installed, or if you need to
reduce the size of a partition to make room for Slint alongside another system.
When you finish reading this menu will be displayed again.
"
