#!/bin/sh
#
# linbo_wrapper: wrapper for linbo commands
# thomas@linuxmuster.net
# 20250816
#

SECRETS=/tmp/rsyncd.secrets


#### functions begin ####

# print usage infos
usage(){
  echo
  echo "Usage: linbo_wrapper <command1 command2 ...>"
  echo
  echo "linbo_wrapper is a linbo command wrapper."
  echo "It reads the start.conf and creates the according commands."
  echo
  echo "Available commands are:"
  echo
  echo "partition                  : Partitions the disk."
  echo "label                      : Assings labels to partitions."
  echo "format                     : Partitions the disk and formats the partions."
  echo "format:<#>                 : Partitions the disk and formats only"
  echo "                             partition nr. <#>."
  echo "initcache:<dltype>         : Updates the local cache. <dltype> is"
  echo "                             rsync, multicast or torrent. If <dltype> is"
  echo "                             not given it will be fetched from start.conf."
  echo "sync:<#>                   : Synchronizes operating system nr. <#>."
  echo "new:<#>                    : Formats and synchronizes operating system nr. <#>."
  echo "start:<#>                  : Starts operating system nr. <#>."
  echo "create_image:<#>:<\"msg\"> : Creates a baseimage from operating system nr. <#>."
  echo "create_qdiff:<#>:<\"msg\"> : Creates a differential image from operating system nr. <#>."
  echo "upload_image:<#>           : Uploads the baseimage of operating system nr. <#>."
  echo "upload_qdiff:<#>           : Uploads the differential image of operating system nr. <#>."
  echo "update                     : Updates LINBO's kernel and initrd and installs GRUB."
  echo "reboot                     : Reboots the client."
  echo "halt                       : Shuts down the client."
  echo "help                       : Show this help."
  echo
  echo "<\"msg\"> image comment (optional)."
  echo "The position numbers depend on the position in the start.conf."
  echo "The Commands are invoked in the order they are given."
  echo "The upload command needs a file /tmp/rsyncd.secrets with the RSYNC credentials"
  echo "in the form: <username>:<password>"
  echo
  exit
}


# get rsync user and password
get_passwd(){
  [ -s "$SECRETS" ] || return 1
  user=linbo
  password="$(grep ^"$user:" "$SECRETS" | awk -F\: '{ print $2 }')"
}


# creates image description
create_desc(){
  local image="$1"
  linbo_mountcache
  cat /proc/mounts | grep -q /cache || return 1
  if [ -n "$msg" ]; then
    msg="$(date): $msg"
  else
    msg="$(date): $image created by linbo_wrapper."
  fi
  echo "$msg" > /cache/msg.tmp
  [ -s "/cache/$image.desc" ] && cat "/cache/$image.desc" >> /cache/msg.tmp
  mv /cache/msg.tmp "/cache/$image.desc"
}

#### functions end ####


# print help
[ -z "$1" -o "$1" = "help" ] && usage

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

# process command line args
while [ "$#" -gt "0" ]; do

  cmd="$(echo "$1" | awk -F\: '{ print $1 }')"
  osnr="$(echo "$1" | awk -F\: '{ print $2 }')"
  msg="$(echo "$1" | awk -F\: '{ print $3 }')"

  # do not print linbo password
  echo "Command     : $cmd"
  [ -n "$osnr" -a "$cmd" != "linbo" ] && echo "OS number   : $osnr"
  [ -n "$msg" ] && echo "Comment        : $msg"

  case "$cmd" in

    linbo)
      password="$osnr"
      echo "${cmd}:${password}" > "$SECRETS"
      ;;

    partition) linbo_partition || exit 1 ;;

    format)
      if [ -n "$osnr" ]; then
        linbo_format "$osnr" || exit 1
      else
        linbo_partition_format || exit 1
      fi
      ;;

    initcache) linbo_initcache || exit 1 ;;

    create_image|create_qdiff)
      source /conf/os.$osnr
      # differential image
      if [ "$cmd" = "create_qdiff" ]; then
        qdiff="qdiff"
        imagefile="${baseimage/.qcow2/.qdiff}"
      else
        imagefile="$baseimage"
      fi
      linbo_create_image "$osnr" "$qdiff" || exit 1
      create_desc "$imagefile"
      ;;

    upload_image|upload_qdiff)
      source /conf/os.$osnr || exit 1
      imagefile="$baseimage"
      get_passwd
      # differential image
      [ "$cmd" = "upload_qdiff" ] && imagefile="${imagefile/.qcow2/.qdiff}"
      echo "Uploading $imagefile to $LINBOSERVER ..."
      linbo_upload "$password" "$imagefile" || exit 1
      ;;

    new)
      source /conf/os.$osnr || exit 1
      echo "Formatting and syncing $name ..."
      linbo_sync "$osnr" "force" || exit 1
      ;;

    sync)
      source /conf/os.$osnr || exit 1
      echo "Syncing $name ..."
      linbo_sync "$osnr" || exit 1
      ;;

    postsync) linbo_postsync "$osnr" || exit 1 ;;

    start)
      source /conf/os.$osnr || exit 1
      echo "Starting $name ..."
      linbo_cmd start "$osnr" || exit 1
      ;;

    prestart) linbo_prestart "$osnr"r || exit 1 ;;

    update) linbo_update || exit 1 ;;

    label) linbo_label "all" || exit 1 ;;

    reboot) /sbin/reboot || exit 1 ;;

    halt|poweroff) /sbin/poweroff || exit 1 ;;

    gui_ctl_disable) gui_ctl disable || exit 1 ;;

    gui_ctl_enable) gui_ctl enable || exit 1 ;;

    gui_ctl_restore) gui_ctl restore || exit 1 ;;

  esac

  shift

done

exit 0
