#!/bin/sh
#
# download linbo_gui from server
#
# thomas@linuxmuster.net
# 20250425
#

if [ -e /tmp/.gui.done ]; then
  echo "linbo_gui is up-to-date."
  exit 0
fi

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

# is gui disabled?
[ -n "$NOGUI" ] && exit 0

# print status message
print_status(){
  if [ -n "$SPLASH" ]; then
    plymouth --update="$1"
  else
    echo "$1"
  fi
}

# filename of gui archive
gui_archive="linbo_gui64_7.tar.lz"


# check if isoboot and try to get linbo_gui archive from cdrom
if [ -n "$ISOBOOT" ]; then
  print_status "ISO/USB boot detected, trying to get linbo_gui from removable media."
  mkdir -p /media
  for i in /dev/disk/by-id/*; do
    if mount -t iso9660 -o ro "$i" /media; then
      # check for linbo_gui on iso
      if [ -s "/media/$gui_archive" ]; then
        tar xf "/media/$gui_archive" -C / || return 1
        break
      else
        ISOBOOT=""
      fi
      umount /media
    fi
    if [ -n "$ISOBOOT" ]; then
      print_status "Successfully installed linbo_gui from removable media."
      touch /tmp/.gui.done
      exit 0
    fi
  done
fi


print_status "Trying to download linbo_gui from server to cache."
linbo_mountcache &> /dev/null
mount | grep -qw /cache && cache_mounted="yes"
if [ -n "$cache_mounted" ]; then
  print_status "Successfully mounted cache partition."
  dl_dir="/cache"
else
  print_status "Continuing without cache partition."
  dl_dir="/tmp"
fi


# download to cache if present else download to tmp
gui_dir="$dl_dir/gui"
icons_dir="$gui_dir/icons"
themes_dir="$gui_dir/themes"


if [ -z "$SERVERID" ]; then

  print_status "Cannot get linbo server, continuing offline."

# download if linbo server is known ...
else

  # move old archive to gui folder
  mkdir -p "$icons_dir"
  [ -e "$dl_dir/$gui_archive" ] && mv "$dl_dir/$gui_archive" "$gui_dir"
  [ -e "$dl_dir/$gui_archive.md5" ] && rm -f "$dl_dir/$gui_archive.md5"
  # sync gui archive
  cd "$gui_dir"
  RC="1"
  for item in "$gui_archive" "gui/$gui_archive"; do
    if isdownloadable "$item"; then
      linbo_download "$item" &> /dev/null ; RC="$?"
    fi
  done
  if [ "$RC" = "0" ]; then
    print_status "$gui_archive successfully downloaded."
  else
    print_status "Failed to download $gui_archive!"
    exit 1
  fi
  # sync gui icons
  cd "$icons_dir"
  for icon in $icons; do
    RC="1"
    for item in "icons/$icon" "gui/icons/$icon"; do
      if isdownloadable "$item"; then
        linbo_download "$item" &> /dev/null ; RC="$?"
      fi
    done
    if [ "$RC" = "0" ]; then
      print_status "All icons successfully downloaded."
      # remove old icons folder
      [ -d "$dl_dir/icons" ] && rm -rf "$dl_dir/icons"
    fi
  done
  # sync custom gui theme
  if [ -n "$theme" ]; then
    if isdownloadable "gui/themes/$theme"; then
      mkdir -p "$themes_dir"
      cd "$themes_dir"
      # trailing / to download the theme dir recursively
      linbo_download "gui/themes/$theme/" &> /dev/null ; RC="$?"
    fi
  fi

fi # linbo server


# unpack gui archive if present
cd "$gui_dir"
if [ -s "$gui_archive" ]; then
  print_status "Unpacking linbo_gui archive."
  tar xf "$gui_archive" -C /
fi

# provide icons and theme
case "$dl_dir" in
  /cache)
    [ -d "$icons_dir" ] && cp -r "$icons_dir" /
    if [ -d "$themes_dir/$theme" ]; then
      mkdir -p /themes
      cp -r "$themes_dir/$theme" /themes
    fi
  ;;
  *)
    rm -rf /icons /themes
    [ -d "$icons_dir" ] && ln -s "$icons_dir" /
    [ -d "$themes_dir" ] && ln -s "$themes_dir" /
  ;;
esac


# leave download dir
cd /
if [ -s /usr/bin/linbo_gui ]; then
  print_status "Successfully installed linbo_gui from cache."
  touch /tmp/.gui.done
  exit 0
else
  print_status "Failed to install linbo_gui from cache."
  exit 1
fi
