#!/bin/bash

. /etc/colorrc

test -f /tmp/boot_info.txt || exit 0
. /tmp/boot_info.txt
test -f /tmp/locale || exit 0
. /tmp/locale
test -f /tmp/autoconfig.txt || exit 0
. /tmp/autoconfig.txt

#BOOTDEV_MountPoint="/media/cdrom"
#BOOTDEV="/dev/cdrom"

function data_copy() {
  mkdir -p /mnt/$1
  cp -a /$1/* /mnt/$1/
  sync
  echo -n "."
}
# --------------------------------------------
GetHD() {
case "$1" in
/dev/sda*)
    echo "/dev/sda"
    ;;
/dev/sdb*)
    echo "/dev/sdb"
    ;;
/dev/sdc*)
    echo "/dev/sdc"
    ;;
/dev/sdd*)
    echo "/dev/sdd"
    ;;
/dev/sde*)
    echo "/dev/sde"
    ;;
/dev/sdf*)
    echo "/dev/sdf"
    ;;
/dev/sdg*)
    echo "/dev/sdg"
    ;;
/dev/sdh*)
    echo "/dev/sdh"
    ;;
esac
}

DEST_EFI_dev=`GetHD $1`"1"
DEST_dev=`GetHD $1`
BOOT_dev=`GetHD $BOOTDEV`

# --------------------------------------------

if test x$1 = x || test x$DEST_dev = x || test x$DEST_dev = x$1; then
  echo ${rcC3}"ERROR: Usage: mkboothd BOOT_DISK (/dev/sda2)"${rcLF}
  exit 1
fi

if test x$BOOT_dev = x; then
  echo ${rcC3}"ERROR: No Boot dev ???  \"/tmp/boot_info.txt\""${rcLF}
  exit 1
fi

if test $BOOT_dev = $DEST_dev; then
  echo ${rcC3}"ERROR: USB ($BOOT_dev) = HD ($DEST_dev)"${rcLF}
  exit 1
fi

if test x$DEST_EFI_dev = x$1; then
  echo ${rcC3}"ERROR: $1 = EFI"${rcLF}
  echo ${rcC3}"ERROR: Usage: mkboothd BOOT_DISK (${DEST_dev}2)"${rcLF}
  exit 1
fi

cat /proc/mounts | while read mdev type rest; do
  if test x"$mdev" = x"$1" ; then
    echo ${rcC3}"ERROR: $1 is mounted !!! "${rcLF}
    exit 1
  fi
done
test $? != 0 && exit 1

mount -l | while read mdev type rest; do
  if test x"$mdev" = x"$1" ; then
    echo ${rcC3}"ERROR: $1 is mounted !!! "${rcLF}
    exit 1
  fi
done
test $? != 0 && exit 1

IsOk=`sfdisk -l $DEST_dev | while read mdev type rest; do \
  if test "$mdev" = "$1" ; then \
    echo "y" ;\
    break ;\
  fi ;\
done`
test $? != 0 && exit 1

if test x$IsOk != x"y" ; then
  sfdisk -l $DEST_dev
  echo ${rcC3}"ERROR: no $1 found"${rcLF}
fi

#BOOT_dev


BOOTDEV_EFI=${BOOT_dev}1

mount -o ro $BOOTDEV_EFI $BOOTDEV_MountPoint
if test $? != 0; then
  echo ${rcC3}"ERROR: USB $BOOTDEV_EFI"${rcLF}
  exit 1; 
fi;
umount $BOOTDEV_MountPoint

mount -o ro $BOOTDEV $BOOTDEV_MountPoint
if test $? != 0; then
  echo ${rcC3}"ERROR: USB $BOOTDEV "${rcLF}
  exit 1; 
fi;
umount $BOOTDEV_MountPoint

# --------------------------------------------

if test x$DEST_dev != x; then
  sfdisk -l $DEST_dev
  test $? != 0 && exit 1
  echo "    -----------"
fi


# -------------------------------------------
echo -n "Format DEST_dev $1 [y,n,q] :"; read a;
if [ x$a = x"q" ]; then 
  exit 0;
fi
if test x$a = x"y" || test x$a = x"j"; then
  mkfs.ext4 $1
  test $? != 0 && exit 1
  sync
fi

# --------------------------------------------

mount -o ro $BOOTDEV_EFI $BOOTDEV_MountPoint
if test $? != 0; then
  echo ${rcC3}"ERROR: USB $BOOTDEV_EFI"${rcLF}
  exit 1; 
fi;

mount $DEST_EFI_dev /mnt
if test $? != 0; then 
  umount $BOOTDEV_MountPoint
  echo ${rcC3}"ERROR: mount $DEST_EFI_dev /mnt"${rcLF}
  exit 1;
fi;

echo -n ${rcC2}"copy EFI data "${rcLF}
cp -ar $BOOTDEV_MountPoint/EFI /mnt

cat > /mnt/EFI/BOOT/extlinux.conf <<EOF
default vesamenu.c32
timeout 300
prompt 0
noescape 1
MENU MARGIN 5
 MENU BACKGROUND syslinux_splash.jpg
# Set the color for unselected menu item and timout message
 MENU COLOR UNSEL 7;32;41 #c0000090 #00000000
 MENU COLOR TIMEOUT_MSG 7;32;41 #c0000090 #00000000
 MENU COLOR TIMEOUT 7;32;41 #c0000090 #00000000
 MENU COLOR HELP 7;32;41 #c0000090 #00000000

MENU TITLE ALDccp Embedded 64Bit x86_64 Linux X11R7.7 V1.2.4 Rev.0

LABEL 1
  MENU DEFAULT
  MENU LABEL Linux
  KERNEL /EFI/BOOT/bzImage
  APPEND root=$1 vga=ext
  TIMEOUT 300
EOF

sync
umount /mnt
umount $BOOTDEV_MountPoint
echo ${rc_Ok}

# --------------------------------------------
#mount -o ro $BOOTDEV $BOOTDEV_MountPoint
#if test $? != 0; then
#  echo ${rcC3}"ERROR: USB $BOOTDEV "${rcLF}
#  exit 1; 
#fi;

mount $1 /mnt
if test $? != 0; then 
#  umount $BOOTDEV_MountPoint
  echo ${rcC3}"ERROR: mount $1 /mnt"${rcLF}
  exit 1;
fi;

# --------------------------------------------
echo -n ${rcC2}"copy data "${rcLF}

data_copy bin

mkdir -p /mnt/dev

data_copy etc

if test -e /mnt/etc/X11; then
 cp -a /tmp/XF86auto /mnt/etc/X11/
fi

mkdir -p /mnt/home
data_copy home
if test -e /lib; then
data_copy lib
fi
if test -e /lib64 ; then
data_copy lib64
fi
mkdir -p /mnt/media
mkdir -p /mnt/mnt
data_copy opt
mkdir -p /mnt/proc
#mkdir -p /mnt/ram

mkdir -p /mnt/root

data_copy sbin
mkdir -p /mnt/sys
mkdir -p /mnt/tmp
#cp -a /tmp/autoconfig.txt /mnt/tmp/
data_copy usr

data_copy var/lib
if test -d /mnt/var/lib/nfs ; then
  rm -r /mnt/var/lib/nfs
  mkdir -p /mnt/var/lib/nfs/sm
  mkdir -p /mnt/var/lib/nfs/sm.bak
  echo -n >/mnt/var/lib/nfs/etab
  echo -n >/mnt/var/lib/nfs/rmtab
  echo -n >/mnt/var/lib/nfs/state
fi
mkdir -p /mnt/var/cache
mkdir -p /mnt/var/lock
mkdir -p /mnt/var/log
mkdir -p /mnt/var/run
mkdir -p /mnt/var/spool
mkdir -p /mnt/var/tmp

echo ${rc_Ok}

#umount $BOOTDEV_MountPoint

# --------------------------------------------
rm -r /mnt/etc/init.d/div
rm /mnt/etc/init.d/rc2/S80umountboot
rm /mnt/etc/init.d/boot
rm /mnt/etc/init.d/mkboot_efi

mkdir -p /mnt/root/.cache
mkdir -p /mnt/root/.gconf
mkdir -p /mnt/root/.local/share
mkdir -p /mnt/root/.thumbnails
mkdir -p /mnt/root/Downloads

if test -d /root/.ALDccp ; then
  mkdir -p /mnt/root/.ALDccp
  cp -ar /root/.ALDccp/* /mnt/root/.ALDccp/
  rm -f /mnt/root/.ALDccp/Desktop/sd*
fi

if test -d /root/.config ; then
  mkdir -p /mnt/root/.config
  cp -ar /root/.config/* /mnt/root/.config/
  if test -f /mnt/root/.config/audacious/config; then
   sed -i s:"/tmp/shm":: /mnt/root/.config/audacious/config
   sed -i s:"/tmp/shm":: /mnt/root/.config/audacious/plugin-registry
  fi
fi

# --------------------------------------------
cat > /mnt/etc/init.d/boot <<EOF
#!/bin/bash

mount -n -o remount,size=1M -t devtmpfs devtmpfs /dev

mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys

if test ! -d /dev/.udev; then
  mkdir -p /dev/.udev/rules.d
fi

if test ! -d /dev/pts; then
  mkdir /dev/pts
fi
mount -n -t devpts devpts /dev/pts

#mkdir /tmp
mount -n -t tmpfs tmpfs /tmp

mkdir -p -m0775 /tmp/shm
chgrp users /tmp/shm
ln -s /tmp/shm /dev/shm

fsck.ext4 -p -t $1

#mount -o remount,rw,noatime $1 /
mount -o remount,rw $1 /

chmod 755 /
hostname linux

rm -rf /var/run/dbus
rm -rf /var/run/network-*

syslogd -m 0
klogd -c 4

EOF

if [ x$KMS = x"y" ]; then

  if [ x$KGraphDriver = x"i915" ]; then
    echo "modprobe i915 modeset=1" >>/mnt/etc/init.d/boot
  fi

  if [ x$KGraphDriver = x"gma500_gfx" ]; then
    echo "modprobe gma500_gfx" >>/mnt/etc/init.d/boot
  fi

  if [ x$KGraphDriver = x"radeon" ]; then
    echo "modprobe radeon modeset=1 audio=1" >>/mnt/etc/init.d/boot
  fi

  if [ x$KGraphDriver = x"amdgpu" ]; then
    echo "modprobe amdgpu modeset=1 audio=1" >>/mnt/etc/init.d/boot
  fi

  if [ x$KGraphDriver = x"nouveau" ]; then
    echo "modprobe nouveau modeset=1" >>/mnt/etc/init.d/boot
  fi

else

  if [ x$KGraphDriver = x"i810" ]; then
    echo "modprobe i810" >>/mnt/etc/init.d/boot
  fi
  echo "setfont lat1-08" >>/mnt/etc/init.d/boot
fi


cat >> /mnt/etc/init.d/boot <<EOF

echo "boot" >/var/run/init

echo -n > /var/run/utmp
chmod 664 /var/run/utmp
chown root.tty /var/run/utmp

if test -e /sbin/udevd; then
  echo -n "Starting the hotplug events dispatcher udevd"
  udevd --daemon
  if [ \$? -eq 0 ]; then
    sleep 1
    echo \$rc_Ok
    
    echo -n "Synthesizing the initial hotplug events"
    udevadm trigger --action=add
    if [ \$? -eq 0 ]; then
      sleep 1
      echo \$rc_Ok
    else
      echo \$rc_Err
    fi

  else
    echo \$rc_Err
  fi

fi

ifconfig lo 127.0.0.1

if test -f /proc/modules -a -x /sbin/modprobe -a -e /etc/modules; then
  grep '^[^#]' /etc/modules | \
  while read module args; do
    test -n \$module || continue
    echo -n "Load module \$module \$args"
    modprobe \$module \$args 2>/dev/null
    if test \$? != 0; then
      echo "\$rc_Err"
    else
      echo "\$rc_Ok"
    fi
  done
fi

EOF

chmod 700 /mnt/etc/init.d/boot
# --------------------------------------------

rm -f /mnt/opt/aldccp/bin/aldccp
rm -f /mnt/opt/aldccp/bin/aldccplast

cat > /mnt/opt/aldccp/bin/aldccp <<EOF
#!/bin/bash

PATH=\$PATH:/usr/X11/bin:/opt/aldccp/bin:/usr/lib64/qt5/bin
export PATH

if test -f /etc/aldccp/system; then
. /etc/aldccp/system
else
  ENV_LANG=en_US.utf8
fi
export LANG=\$ENV_LANG

#export QTDIR="/usr/lib64/qt5"

rm -f /var/run/ALDesk/aldesk

XF86="/etc/X11/XF86auto"

xinitres=/tmp/ALDxinitres

cat > \${xinitres} <<END_xinitres

#setxkbmap -option terminate:ctrl_alt_bksp
su fred -c aldesk

END_xinitres

xinit \${xinitres} -- /usr/X11/bin/X -s 0 -xf86config \$XF86

rm -f /tmp/ALDxinitres
EOF
# ------------

if test x$KMS = x"n"; then
cat >> /mnt/opt/aldccp/bin/aldccp <<EOF
  setfont lat1-12
  setfont lat1-08

EOF
fi

cat >> /mnt/opt/aldccp/bin/aldccp <<EOF
if test -f /var/run/ALDesk/aldesk; then
  ALD_stat=\`cat /var/run/ALDesk/aldesk\`
  if test X\$ALD_stat = X"ShutDown"; then
    rm -f /var/run/ALDesk/aldesk
    halt
  fi
fi
EOF
# ------------

chmod 750 /mnt/opt/aldccp/bin/aldccp
chgrp users /mnt/opt/aldccp/bin/aldccp
cp -a /mnt/opt/aldccp/bin/aldccp  /mnt/opt/aldccp/bin/aldccpfred
sed -i s:"su fred -c aldesk":"aldesk": /mnt/opt/aldccp/bin/aldccp
# ------------

cat > /mnt/etc/init.d/startmsg <<EOF
#!/bin/bash
case "\$1" in
    start)
	if test ! x$X11_AUTO_START = x"yes"; then
	  sleep 1
	  echo "-----------------------------------------------------"
	  echo "   start X11       ->aldccp<-      + ->Enter<-"
	  echo "   shutdown        ->halt<-        + ->Enter<-"
	  echo "-----------------------------------------------------"
	fi
	;;
    force)
	;;
    stop)
	;;
    *)
	echo "Usage: {start|stop}"
	exit 1
	;;
esac
EOF

if test "$AlsaWork" = "y"; then
  if test ! -e /mnt/etc/init.d/rcS/S50alsa_ctl ; then
    ln -s ../alsa_ctl /mnt/etc/init.d/rcS/S50alsa_ctl
  fi
  if test ! -e /mnt/etc/init.d/rcS/K90alsa_ctl ; then
    ln -s ../alsa_ctl /mnt/etc/init.d/rcS/K90alsa_ctl
  fi
fi

umount /mnt
echo ${rcC2}"    ------ OK ------"${rcLF}
