#!/bin/bash
# Test if live-config and upstart are used. If so, we have to wait for live-config to be finished. Otherwise the clonezilla-related process might be faster than live-config. E.g. if the "sudo" config (/lib/live/config/003-sudo) is not yet run, but S03prep-drbl-clonezilla is run, the autologin account won't be found, then the clonezilla auto startup config in the autologin account won't be generated.
if [ -d /lib/live/config -a -e /etc/init/live-config.conf ]; then
  echo "Waiting for live-config to be finished..."
  to_wait=""
  while [ -z "$to_wait" ]; do
    # Find the last task in /lib/live/config/ of live-config.
    # The files list in /lib/live/config/ is like (v2.x):
    # 001-hostname 002-user-setup 003-sudo 004-locales 005-tzdata 006-gdm 007-gdm3 008-kdm 009-lxdm 010-nodm 011-slim 012-xinit 014-upstart-tty 015-keyboard-configuration 102-gnome-panel-data 103-gnome-power-manager 104-gnome-screensaver 105-initramfs-tools 106-kaboom 107-kde-services 109-debian-installer-launcher 110-module-init-tools 111-policykit 112-sslcert 113-update-notifier 114-anacron 115-util-linux 116-login 117-xserver-xorg 119-openssh-server 120-umountfs 999-hooks
    # For v3.x:
    # 0010-debconf 0020-hostname 0030-user-setup 0040-sudo 0050-locales 0060-locales-all 0070-tzdata 0080-gdm 0090-gdm3 0100-kdm 0110-lightdm 0120-lxdm 0130-nodm 0140-slim 0150-xinit 0160-keyboard-configuration 0190-upstart 1020-gnome-panel-data 1030-gnome-power-manager 1040-gnome-screensaver 1050-kaboom 1060-kde-services 1070-debian-installer-launcher 1080-module-init-tools 1090-policykit 1100-sslcert 1110-update-notifier 1120-anacron 1130-util-linux 1140-login 1150-xserver-xorg 1170-openssh-server 1170-xfce4-panel 9990-hooks
    # We find the last one, but exclude 999-hooks (live-config 2.x) since 999-hooks does not create any tag in /var/lib/live/config/
    # For v3.x, we can not use the tag in /var/lib/live/config/ since "1170-xfce4-panel" is the last one, and for Clonezilla live we do not include xfce4-panel, so it will be skipped, and there is no tag file /var/lib/live/config/xfce4-panel will be created since it's skipped in the beginning of "1170-xfce4-panel". Therefore it's better and easier to use the log file /var/log/live/config.log.
    if [ -e /lib/live/config/999-hooks ]; then
      # For live-config 2.x
      last_num="$(LC_ALL=C ls --color=never -1 /lib/live/config/ | grep -Ev "999-" | awk -F"-" '{print $1}' | sort -r -g | uniq | head -n 1)"
      last_task="$(LC_ALL=C find /lib/live/config/${last_num}-* -exec basename {} \; | sort -r -g | uniq | head -n 1 | sed -r -e "s/^${last_num}-//g")"
      if [ -e "/var/lib/live/config/$last_task" ]; then
        echo "live-config is finished."
        to_wait="no"
      else
        sleep 0.2
      fi
    else
      # For live-config 3.x
      # When live-config is finished, a "." will be appended in the end of log file /var/log/live/config.log, like:
      #live-config: debconf hostname user-setup sudo locales tzdata keyboard-configuration upstart util-linux login openssh-server.
      # //NOTE// The content of /var/log/live/config.log might be like:
      # ======================
      # live-config: debconf hostname user-setup sudo locales tzdata keyboard-configuration upstartstop: Unknown instance:
      # stop: Unknown instance:
      # stop: Unknown instance:
      # stop: Unknown instance:
      # stop: Unknown instance:
      # stop: Unknown instance:
      #  util-linux login openssh-server.
      # Or when some errors or warnings happen, it's like:
      # live-config: debconf hostnameDevice "eth0" does not exists.
      # Device "eth0" does not exists.
      # Device "eth0" does not exists.
      # user-setup sudo locales tzdata keyboard-configuration upstart util-linux login openssh-server.
      # ======================
      # Therefore we have to patch live-config by adding an obvious endding tag "End of live-config jobs."
      if grep -q -E "End of live-config jobs\.$" /var/log/live/config.log; then
        echo "live-config is finished."
        to_wait="no"
      else
        sleep 0.2
      fi
    fi
  done
fi
echo "Starting to prepare Clonezilla live env..."
