#!/bin/bash

set -e

show_help() {
  cat << EOF

Simple tool to generate Mender Artifact suitable for directory Update Module

Usage: $0 [options] directory [-- [options-for-mender-artifact] ]

    Options: [ -n|artifact-name -t|--device-type -d|--dest-dir -o|--output_path -h|--help ]

        --artifact-name     - Artifact name
        --device-type       - Target device type identification (can be given more than once)
        --dest-dir          - Target destination directory where to deploy the update
        --output-path       - Path to output file. Default: file-install-artifact.mender
        --help              - Show help and exit
        directory           - File tree to bundle in the update

Anything after a '--' gets passed directly to the mender-artifact tool.

EOF
}

show_help_and_exit_error() {
  show_help
  exit 1
}

check_dependency() {
  hash "$1" || exit 1
}

check_dependency mender-artifact

device_types=""
artifact_name=""
dest_dir=""
output_path="directory-artifact.mender"
update_files_tar="update.tar"
dest_dir_file="dest_dir"
file_tree=""
passthrough=0
passthrough_args=""

while (( "$#" )); do
  if test $passthrough -eq 1
  then
    passthrough_args="$passthrough_args $1"
    shift
    continue
  fi
  case "$1" in
    --device-type | -t)
      if [ -z "$2" ]; then
        show_help_and_exit_error
      fi
      device_types="$device_types $1 $2"
      shift 2
      ;;
    --artifact-name | -n)
      if [ -z "$2" ]; then
        show_help_and_exit_error
      fi
      artifact_name=$2
      shift 2
      ;;
    --dest-dir | -d)
      if [ -z "$2" ]; then
        show_help_and_exit_error
      fi
      dest_dir=$2
      shift 2
      ;;
    --output-path | -o)
      if [ -z "$2" ]; then
        show_help_and_exit_error
      fi
      output_path=$2
      shift 2
      ;;
    -h | --help)
      show_help
      exit 0
      ;;
    --)
      passthrough=1
      shift
      ;;
    -*)
      echo "Error: unsupported option $1"
      show_help_and_exit_error
      ;;
    *)
      if [ -n "$file_tree" ]; then
        echo "File tree already specified. Unrecognized argument \"$1\""
        show_help_and_exit_error
      fi
      file_tree="$1"
      shift
      ;;
  esac
done

if [ -z "${artifact_name}" ]; then
  echo "Artifact name not specified. Aborting."
  show_help_and_exit_error
fi

if [ -z "${device_types}" ]; then
  echo "Device type not specified. Aborting."
  show_help_and_exit_error
fi

if [ -z "${dest_dir}" ]; then
  echo "Destination dir not specified. Aborting."
  show_help_and_exit_error
fi

if [ -z "${file_tree}" ]; then
  echo "File tree not specified. Aborting."
  show_help_and_exit_error
fi

# Check dest-dir is an absolute path
case $dest_dir in
  /*)
    ;;
  *)
    echo "Destination dir must be an absolute path. Aborting"
    exit 1
  ;;
esac

# Create tarball, accepts directory.
if [ -e "${file_tree}" ]; then
  if [ -d "${file_tree}" ]; then
    if [ "$dest_dir" == "/" ]; then
      echo "Error: this Update Module does not support file tree deployment at /"
      exit 1
    fi
    tar -cf ${update_files_tar} -C "${file_tree}" .
  else
    echo "Error: \"${file_tree}\" is not a directory. Aborting."
    exit 1
  fi
else
  echo "Error: File/directory \"${file_tree}\" does not exist. Aborting."
  exit 1
fi


# Create dest_dir file in plain text
echo "$dest_dir" > $dest_dir_file

mender-artifact write module-image \
  -T directory \
  $device_types \
  -o $output_path \
  -n $artifact_name \
  -f $update_files_tar \
  -f $dest_dir_file \
  $passthrough_args

rm $update_files_tar
rm $dest_dir_file

echo "Artifact $output_path generated successfully:"
mender-artifact read $output_path

exit 0