From: Frank Brehm Date: Sat, 18 Nov 2017 12:28:18 +0000 (+0100) Subject: Base things in bin/convert-stack.sh X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=bb1a126ec395518e72e4b2fbf6e4fc01ce999281;p=my-stuff%2Ftools.git Base things in bin/convert-stack.sh --- diff --git a/bin/convert-stack.sh b/bin/convert-stack.sh old mode 100644 new mode 100755 index 63a2d7c..bae2a12 --- a/bin/convert-stack.sh +++ b/bin/convert-stack.sh @@ -9,8 +9,9 @@ export LANG="en_US.utf8" VERBOSE="n" DEBUG="n" QUIET='n' +SIMULATE="n" -VERSION="0.1" +VERSION="0.2" # console colors: RED="" @@ -23,8 +24,287 @@ HAS_TTY='y' HAS_COLORS="n" BASENAME="$(basename ${0})" -BASE_DIR="$(dirname ${0})" +BIN_DIR="$(dirname ${0})" +BASE_DIR=$( pwd ) +CONVERT_SCRIPT="${BIN_DIR}/convert_movie.sh" +LINK_DIR= +OWNING_GROUP='plex' +PERMS='0664' +FIELD_SEPARATOR='|' +CSV_FILE= +RESOLUTION=1280 + +#------------------------------------------------------------------- +detect_color() { + + local safe_term="${TERM//[^[:alnum:]]/?}" + local match_lhs="" + local use_color="false" + [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" + [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ + && match_lhs=$(dircolors --print-database) + [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true" + + # console colors: + if [ "${use_color}" = "true" ] ; then + RED="\033[38;5;196m" + YELLOW="\033[38;5;226m" + GREEN="\033[38;5;46m" + BLUE="\033[38;5;27m" + NORMAL="\033[39m" + HAS_COLORS="y" + else + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + fi + + local my_tty=$(tty) + if [[ "${my_tty}" =~ 'not a tty' ]] ; then + my_tty='-' + fi + + if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then + HAS_TTY='n' + fi + +} +detect_color + +#------------------------------------------------------------------------------ +description() { + echo -e "$( cat <<-EOF + Converts a stack of movies by using convert_movie.sh contolled by a CSV file. + + Each line of the CSV file must consist from 4 fields: + * the directory of the source movie, absolute or relative to the current working dir + * the file name of the source movie file + * the file name of the target converted movie file, + must be a base name and should have the extension '.avi' + * textual information to the movie, will be inserted inside the target file as metadata + + As field separator will be used '${FIELD_SEPARATOR}' by default. + Empty lines and lines starting with a '#'-sign (comments) will be ignored. + All fields will be stripped on both sides. + + EOF + )" +} + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASENAME} OPTIONS CSV_FILE + ${BASENAME} [-h|--help] + ${BASENAME} [-V|--version] + + Options: + -R|--resolution PIXEL + The scaling of the target movie. The larger dimension has be given + (mostly the width). Default: ${RESOLUTION} + -L|--linkdir DIRECTORY + Directory, where to place a hard link to the converted Movie. + Not done, if omitted. + -P|--perms PERMISSIONS + Permissions of the converted movies an an octal number. Default: '${PERMS}' + -G|--group GROUP + Name of the owning group of the converted movies. Default: '${OWNING_GROUP}' + -F|--field-separator CHARACTER + The character used as field separator in the CSV file. Default: '${FIELD_SEPARATOR}' + -s|--simulate Simulation mode, don't convert the movies. + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. + --nocolor Don't use colors on display. + -h|--help Show this output and exit. + -V|--version prints out version number of the script and exit + EOF +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + + set +e + tmp=$( getopt -o R:P:G:F:sdvhV \ + --long resolution:,linkdir:,perms:,group:,field-separator:,simulate,debug,verbose,nocolor,help,version \ + -n "${BASENAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + + local p= + + while true ; do + case "$1" in + -R|--resolution) + RESOLUTION="$2" + shift + shift + ;; + -L|--linkdir) + LINK_DIR="$2" + shift + shift + ;; + -P|--perms) + PERMS="$2" + shift + shift + ;; + -G|--group) + OWNING_GROUP="$2" + shift + shift + ;; + -F|--field-separator) + FIELD_SEPARATOR="$2" + shift + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + -s|--simulate) + SIMULATE="y" + shift + ;; + --nocolor) + RED="" + YELLOW="" + GREEN="" + BLUE="" + NORMAL="" + HAS_COLORS="n" + shift + ;; + -h|--help) + description + echo + usage + exit 0 + ;; + -V|--version) + echo "${BASENAME} version: ${VERSION}" + exit 0 + ;; + --) shift + break + ;; + *) echo "Internal error!" + exit 1 + ;; + esac + done + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "$#" -lt "1" ]] ; then + error "No CSV file given." + echo >&2 + description >&2 + echo + usage >&2 + exit 1 + fi + + if [[ "$#" -gt "1" ]] ; then + error "Only one CSV has to be given." + echo >&2 + description >&2 + echo + usage >&2 + exit 1 + fi + + CSV_FILE="$1" + if [[ ! -f "${CSV_FILE}" ]] ; then + error "CSV file '${RED}${CSV_FILE}${NORMAL}' does not exists." + exit 1 + fi + + if [[ ! -x "${CONVERT_SCRIPT}" ]] ; then + error "Conversion script '${CONVERT_SCRIPT}' not found" + exit 5 + fi + + if [[ -n "${LINK_DIR}" && ! -d "${LINK_DIR}" ]] ; then + error "Given link directory '${RED}${LINK_DIR}${NORMAL}' does not exists." + exit 1 + fi + +} + +######################################### +# Some often used funktions + +#------------------------------------------------------------------------------ +my_date() { + date +'%F %T.%N %:::z' +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" +} + +#------------------------------------------------------------------------------ +info() { + if [[ "${QUIET}" == "y" ]] ; then + return 0 + fi + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +MKDIR() { + local cmd="mkdir" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} + +#------------------------------------------------------------------------------ +LN() { + local cmd="ln" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} ################################################################################ ## @@ -35,7 +315,7 @@ BASE_DIR="$(dirname ${0})" #------------------------------------------------------------------------------ main() { -# get_options "$@" + get_options "$@" # prepare_dirs # info "Starting backup ..." @@ -44,7 +324,7 @@ main() { # backup_zones # empty_line -# info "Finished." + info "Finished." }