#!/bin/bash
#***********************************************************************************************************
#
# Starfish Storage Corporation ("Starfish") CONFIDENTIAL
# Unpublished Copyright (c) 2011 - present Starfish Storage Corporation, All Rights Reserved.
#
# NOTICE: This file and its contents (1) constitute Starfish's "External Code" under Starfish's most-recent
# Limited Software End-User License Agreement, and (2) is and remains the property of Starfish. The
# intellectual and technical concepts contained herein are proprietary to Starfish and may be covered by
# U.S. and/or foreign patents or patents in process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material is strictly forbidden unless prior
# written permission is obtained from Starfish. Access to the source code contained herein is hereby
# forbidden to anyone except (A) current Starfish employees, managers, or contractors who have executed
# confidentiality or nondisclosure agreements explicitly covering such access, and (B) licensees of
# Starfish's software.
#
# ANY REPRODUCTION, COPYING, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE, OR PUBLIC DISPLAY OF OR
# THROUGH USE OF THIS SOURCE CODE WITHOUT THE EXPRESS WRITTEN CONSENT OF STARFISH IS STRICTLY PROHIBITED
# AND IS IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR POSSESSION OF THIS
# FILE OR ITS CONTENTS AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE,
# DISCLOSE, OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN
# WHOLE OR IN PART.
#
# FOR U.S. GOVERNMENT CUSTOMERS REGARDING THIS DOCUMENTATION/SOFTWARE
#   These notices shall be marked on any reproduction of this data, in whole or in part.
#   NOTICE: Notwithstanding any other lease or license that may pertain to, or accompany the delivery of,
#   this computer software, the rights of the Government regarding its use, reproduction and disclosure are
#   as set forth in Section 52.227-19 of the FARS Computer Software-Restricted Rights clause.
#   RESTRICTED RIGHTS NOTICE: Use, duplication, or disclosure by the Government is subject to the
#   restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer
#   Software clause at DFARS 52.227-7013.
#
#***********************************************************************************************************

set -euo pipefail

# SF tool to extract metadata from files and save into database for further processing/query, etc.

SFHOME="${SFHOME:-/opt/starfish}"
STARFISH_BIN_DIR="${SFHOME}/bin"
SF="${STARFISH_BIN_DIR}/client"
PROG="$0"
# prereq - install exiftool to use this script
EXIFWRAPPER="${STARFISH_BIN_DIR}/examples/job/exifwrapper.sh"
EXIFTOOL="$(type -P exiftool || { >&2 echo "exiftool is required for this script. Please install exiftool with yum or apt-get and re-run"; exit 1; })"
JQ="$(type -P jq || { >&2 echo "jq is required for this script. Please install jq with yum or apt-get and re-run"; exit 1; })"


check_parameters_value() {
    local param="$1"

    [[ $# -gt 1 ]] || fatal "Missing value for parameter ${param}"
}

usage() {
    local msg="${1:-""}"

    if [[ ! -z "${msg}" ]]; then
        echo "${msg}" >&2
    fi

    cat <<EOF

${PROG} <source volume>:<source path> [options]
      Extract specific Date, Compression and mime-type headers from file and add to Starfish

options:
    -h, --help          - print this help and exit
    --ext  [extension]  - only process this extension, if more than one, use "--ext jpg --ext jpeg"
    --meta options      - default of -Compression -ImageWidth -ImageHeight -Model -OwnerName -CreateDate. Use in quotes to specify the metadata to extract
    --wait              - Wait until job is complete. Default is to spawn off job

EXAMPLES:
Extract Date, Compression, size, model and owner for jpg files in sfvolume:/videos location:

  ${PROG} sfvolume:/videos --ext jpg

Extract MIMEType, Subject, Author, RevisionNumber, Date, Pages and Words from doc files
in sfvolume:/videos location. This is run from scratch with no scan initiated prior to the exiftool extraction.

  ${PROG} sfvolume:/docs --ext doc --meta "-MIME* -Title -Subject -Author -Revision*Number -*Date -Pages -Words" --from-scratch --no-prescan

EOF
    exit 1
}

[[ $# -lt 1 ]] && usage "Not enough arguments"

if [[ "$1" = "--help" ]] || [[ "$1" = "-h" ]]; then
    usage
    exit 1
fi


readonly SRC_VOL_WITH_PATH="$1"  # examples: src_vol:src_path, src_vol:
shift

WAIT_OPT=""
EXTS=""
EXIF_OPTIONS="-Compression -ImageWidth -ImageHeight -Model -OwnerName -CreateDate"

while [[ $# -gt 0 ]]; do
    case $1 in
    "-h"|"--help")
        usage
        ;;
    "--ext")
        check_parameters_value "$@"
        shift
        EXTS="${EXTS} --ext $1"
        ;;
    "--meta")
        check_parameters_value "$@"
        shift
        EXIF_OPTIONS="$1"
        ;;
    "--wait")
        WAIT_OPT="--wait"
        ;;
    *)
        break
        ;;
    esac;
    shift
done

${SF} job start ${EXTS} --type f ${WAIT_OPT} --cmd-output-format json --job-name exif "$@" \
    "${EXIFWRAPPER} ${EXIFTOOL} %PATH% ${EXIF_OPTIONS}" "${SRC_VOL_WITH_PATH}"
