#!/bin/sh
#
# split start.conf in machine parseable chunks
# thomas@linuxmuster.net
# 20250924
#


# get environment
source /usr/share/linbo/shell_functions
echo "### $timestamp $(basename "$0") ###"

# check for complete start.conf
if grep -qi ^'\[os\]' /start.conf; then
  [ -z "$QUIET" ] && echo "Splitting start.conf to /conf."
else
  [ -z "$QUIET" ] && echo "Not splitting minimal start.conf."
  exit
fi

prefix="/tmp/start.conf."
pc=0
oc=0

# replace uniform device names with real ones
grep -o /dev/disk\[0-9\]p\[0-9\]\* /start.conf | sort -ur | while read item; do
  sed -i "s|$item|$(get_real_partname "$item")|g" /start.conf
done

# split sections, remove comments
grep ^"[\[a-zA-Z]" /start.conf | sed 's|# .*||' | awk '{$1=$1};1' | csplit -s -z -f "$prefix" - '/^\[/' '{*}'

# where to place the chunks
rm -rf /conf
mkdir -p /conf

# process chunks
for item in "$prefix"*; do
  section="$(grep ^"\[" "$item" | awk -F\[ '{print $2}' | awk -F\] '{print $1}' | tr A-Z a-z)"
  case "$section" in
    linbo) cfgfile="/conf/linbo" ;;
    partition)
      pc=$(( pc + 1 ))
      dev="$(grep -iw ^dev "$item" | awk -F\= '{print $2}' | awk '{$1=$1};1')"
      cfgfile="/conf/part.$pc.${dev/\/dev\//}"
      ;;
    os)
      oc=$(( oc + 1 ))
      cfgfile="/conf/os.$oc"
      ;;
    *) continue ;;
  esac
  # write chunk
  [ -z "$QUIET" ] && echo " * $(basename $cfgfile)"
  grep -v ^"\[" "$item" | sed -e 's|[ ]*=[ ]*|="|' | awk -F= '{ st = index($0,"=");print tolower($1) "=" substr($0,st+1) "\""}' > "$cfgfile"
done

# write menufile for nogui mode
[ -s /conf/linbo ] && source /conf/linbo
rm -f /conf/menu
if [ -n "$group" -a -n "$NOGUI" -a -z "$NOMENU" ]; then
  echo "----------------------------------------------" > /conf/menu
  echo " Console boot menu of group $group" >> /conf/menu
  echo "----------------------------------------------" >> /conf/menu
  # iterate start.conf os items and create menu entries and according start commands
  count=0
  for item in /conf/os.*; do
    [ -s "$item" ] || continue
    name=""
    source "$item"
    [ -z "$name" ] && continue
    osnr="${item#*.}"
    # create menu entry for start
    if [ "$startenabled" = "yes" -o -z "$startenabled" ]; then
      count=$(( count + 1 ))
      echo " [$count] start $osnr Start $name" >> /conf/menu
    fi
    # create menu entry for sync & start
    if [ "$syncenabled" = "yes" -o -z "$syncenabled" ]; then
      count=$(( count + 1 ))
       echo " [$count] syncstart $osnr Sync & start $name" >> /conf/menu
    fi
    # create menu entry for new & start
    if [ "$newenabled" = "yes" -o -z "$newenabled" ]; then
      count=$(( count + 1 ))
      echo " [$count] forcesyncstart $osnr New & start $name" >> /conf/menu
    fi
  done
  echo "----------------------------------------------" >> /conf/menu
  echo " [C] Console" >> /conf/menu
  echo " [R] Reboot" >> /conf/menu
  echo " [S] Shutdown" >> /conf/menu
  echo "----------------------------------------------" >> /conf/menu
fi 

# write icons to linbo section
icons="$(grep -w ^iconname /conf/os.* | awk -F\" '{print $2}' | sort -u | sed ':a;N;$!ba;s/\n/ /g')"
echo "icons=\"$icons\"" >> /conf/linbo