Program now executes on a track by track basis, rather than image id by image id basis

Added support for track  selection/exclusion.
Rewrote code
This commit is contained in:
ayakael 2018-03-26 19:34:44 -04:00
parent 48d5fb91af
commit d024a0dc02
No known key found for this signature in database
GPG key ID: 575626A4AE5F4026
17 changed files with 372 additions and 138 deletions

View file

@ -1,18 +1,27 @@
#!/bin/bash
###
# Cleans target of all FLAC files that are owned by IMAGEID that exist in TARGET directory
###
# doc clean_target {
#
# DESCRIPTION
# clean_target - Cleans target of IMAGEID
#
# USAGE
# clean_target </path/to/target> <IMAGEID> </path/to/db/file>
#
# }
clean_target() {
local TARGET="${1}"
local IMAGEID="${2}"
local MANIFEST="${3}"
local DB_FILE="${3}"
for old_file in $(grep ${IMAGEID} ${MANIFEST} | cut -d'=' -f1); do
rm ${TARGET}/${old_file}
rownoList=($(_cfg query '$2=='${IMAGEID} ${DB_FILE}))
for rowno in ${rownoList[@]}; do
FILE_PATH=$(_cfg print TARGET_PATH ${rowno} ${DB_FILE})
rm "${TARGET}/${FILE_PATH}"
_cfg drop row ${rowno}
done
sed -i "/${IMAGEID}/d" ${MANIFEST}
find ${TARGET}/* -empty -type d -delete >/dev/null 2>&1
}

38
src/cmd_select Normal file
View file

@ -0,0 +1,38 @@
#!/bin/bash
# doc cmd_select {
#
# DESCRIPTION
# cmd_select - Command selects all IMAGEID that matches the CONDITION
#
# USAGE
# cmd_select </path/to/git/dir> </path/to/db/file> <BOULEAN> <condition> [<...>]
#
# CONDITIONS
# CONDITIONS are defined using the following format:
# <metadata field>=<value>
# The metadata field can be anything that can be injected as metadata in a Vorbis or id3tag.
#
# }
cmd_select() {
local GIT_DIR="${1}"
local DB_FILE="${2}"
local SELECTED="${3}"; shift 3
local conditionList=(${@})
for condition in ${conditionList[@]}; do
local FIELD="$(echo ${condition} | cut -d'=' -f1)"
local VALUE="$(echo ${condition} | cut -d'=' -f2)"
local imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(grep -i -l '"'${FIELD}'" : "'${VALUE}'"' ${GIT_DIR}/*.tags)))
for imageid in ${imageidList[@]}; do
local rowList=($(_cfg query '$3="'${imageid}'"' ${DB_FILE}))
for rowno in ${rowList[@]}; do
_cfg change SELECTED ${rowno} ${SELECTED} ${DB_FILE}
done
done
done
}

88
src/deploy Normal file
View file

@ -0,0 +1,88 @@
#!/bin/bash
# doc deploy {
#
# DESCRIPTION
# deploy - Deploys IMAGEID to TARGET and then updates DB file.
#
# USAGE
# deploy </path/to/git/dir> </path/to/target> <path/to/db/file> <imageid>
#
deploy() {
local GIT_DIR="${1}"
local TARGET="${2}"
local DB_FILE="${TARGET}/${3}"
local IMAGEID="${4}"
## targetmetaList, targetpathList, deployedpathList, deployedmetaList generators
# targetmetaList and targetpathList are used to define both the metadata, and path
# of split files. deployedpathList and deployedmetaList are used later to determine
# if a change occured in the targetpath and targetmeta, if the IMAGEID has already been
# deployed.
# Defines the TARGET and DEPLOYED total tracks.
local TOTALTRACKS="$(grep -e TITLE "${GITDIR}/${IMAGEID}.tags" | wc -l)"
local TARGET_TOTALTRACKS="$(grep -e TITLE "${GITDIR}/${IMAGEID}.tags" | wc -l)"
local DEPLOYED_TOTALTRACKS="$(grep -e ${IMAGEID} "${DB_FILE}" | wc -l)"
# Current working track variable
local COUNT=1
while [[ ${COUNT} -le ${TARGET_TOTALTRACKS} ]]; do
local targetmetaList[${COUNT}]="$(print_target_meta ${IMAGEID}.tags ${COUNT})"
local targetpathList[${COUNT}]="${TARGET}/$(print_target_path ${targetmetaList[${COUNT}]})"
local deployedpathList[${COUNT}]="${TARGET}/$(print_deployed_path ${IMAGEID} ${COUNT} ${DB_FILE})"
local deployedmetaList[${COUNT}]="$(print_deployed_meta ${deployedpathList[${COUNT}]})"
# Bumps up working track number
local COUNT=$(( ${COUNT} + 1 ))
done
# Checks is there's a discrepancy between what has been deployed and what is targetted to be deployed.
# This can be caused by an unclean deployment, or a change in the cue files. In this case, the clean_target
# function will be used to clean the target of the unclean imageid, and then normal deployment will occur.
if [[ ${TARGET_TOTALTRACKS} -ne ${DEPLOYED_TOTALTRACKS} ]] && [[ ${DEPLOYED_TOTALTRACKS} -ne 0 ]]; then
clean_target ${TARGET} ${IMAGEID} ${DB_FILE}
fi
if [[ -z "${deployedpathList[@]}" ]]; then
## FLAC Splitter
# Splits the provided IMAGEID's FLAC file into multiple tracks using the provided CUE file.
# A hack is sort of applies here by populating the deployedpathList with the output files of split_flac.
split_flac "${GITDIR}" "${IMAGEID}"
local COUNT=1
while ${COUNT} -le ${TARGET_TOTALTRACKS}; do
local deployedpathList[${COUNT}]="${GITDIR}/$(printf 'split-track%02d.flac' ${COUNT})"
local deployedmetaList[${COUNT}]=""
local COUNT=$(( ${COUNT} + 1 ))
done
fi
## Metadata application
# Applies metadata to flac files that are either newly generated by split_flac to already deployed files that require it.
local COUNT=1
while [[ ${COUNT} -le ${TARGET_TOTALTRACKS} ]]; do
# If ${targetmetaList[${COUNT}]} in ${deployedmetaList[${COUNT}]} are not equal, it means that either the already deployed
# flac file needs metadata updating, or a newly generated split flac needs metadata application
if [[ "${targetmetaList[${COUNT}]}" != "${deployedmetaList[${COUNT}]}" ]]; then
[[ ! -z "${deployedmetaList[${COUNT}]}" ]] && metaflac --remove-all ${deployedpathList[${COUNT}]}
awk 'BEGIN {RS=";"}{print $0}' <<< ${targetmetaList[${COUNT}]} | head -n -1 | metaflac --import-tags-from=- --import-picture-from="${IMAGEID}.jpg" ${deployedpathList[${COUNT}]}
fi
local COUNT=$(( ${COUNT} + 1 ))
done
## Track deployment
# Deploys files to appropriate targetpath, of course only if necessary.
local COUNT=1
while [[ ${COUNT} -le ${TARGET_TOTALTRACKS} ]]; do
if [[ "${targetpathList[${COUNT}]}" != "${deployedpathList[${COUNT}]}" ]]; then
mkdir -p "$(dirname "${targetpathList[${COUNT}]}")"
mv "${deployedpathList[${COUNT}]}" "${targetpathList[${COUNT}]}"
local ROW_NO=$(_cfg query '$2=='${IMAGEID}' && $3=='${COUNT}'' ${DB_FILE})
_cfg drop row ${ROW_NO} ${DB_FILE}
_cfg insert row "true" ${IMAGEID} ${COUNT} "${targetpathList[${COUNT}]}" ${DB_FILE}
fi
local COUNT=$(( ${COUNT} + 1 ))
done
}

35
src/deploy_gen Normal file
View file

@ -0,0 +1,35 @@
#!/bin/bash
# doc deploy_gen {
#
# DESCRIPTION
# deploy_gen - Splits specified image into multiple files using a text file
# formatted under the CUE specification
#
# USAGE
# deploy_gen </path/to/git/dir> </path/to/db_file> <image-id> <track-id>
# }
deploy_gen() {
local GITDIR="${1}"
local DB_FILE="${2}"
local IMAGEID="${3}"
local TRACKID="${4}"
local TRACKNO=$(print_track_no ${DB_FILE} ${IMAGEID} ${TRACKID})
## breakpointList generator
# Generates list with cuebreakpoints utility
local breakpointList=($(cuebreakpoints "${GITDIR}/${IMAGEID}.cue" 2>/dev/null))
# In the event that breakpointList is empty because image represents only one track,
# no split occurs, and returns a 0.
[[ -z "${breakpointList[@]}" ]] && { cat "${GITDIR}/${IMAGEID}.flac" > ${GITDIR}/${TRACKID}.flac; return 0; }
# Attempts first split. If fails because of lack of CD quality file, retries with modified breakpointList
if ! printf '%s\n' ${breakpointList[@]} | shntool split "${GITDIR}/${IMAGEID}.flac" -o flac -O always -x ${TRACKNO} -d "${GITDIR}" -f ${TRACKID}.flac; then
printf '%s\n' ${breakpointList[@]} | sed s/$/0/ | shntool split "${GITDIR}/${IMAGEID}.flac" -o flac -O always -x ${TRACKNO} -d "${GITDIR}" -f ${TRACKID}.flac
[[ $? -eq 0 ]] && return 2 || return 1
fi
return 0
}

View file

@ -1,60 +0,0 @@
#!/bin/bash
###
# Deploys IMAGEID to TARGET and then updates MANIFEST file
##
deploy_imageid() {
local IMAGEID="${1}"
local TARGET="${2}"
local MANIFEST="${3}"
local TOTALTRACKS="$(grep -e TITLE ${IMAGEID}.tags | wc -l)"
if [[ ! -e "${IMAGEID}.flac" ]]; then
_msg EXEC "Downloading data for ${IMAGEID}"
git annex get ${IMAGEID}.* >${STDERR} 2>&1
[[ $? -ne 0 ]] && _msg WARN || _msg OK
local DELETE=true
fi
_msg EXEC "Generating data for ${IMAGEID}"
gen_flac ${IMAGEID}.flac ${IMAGEID}.cue >${STDERR} 2>&1
local EXIT_CODE=$?
[[ ${EXIT_CODE} -eq 0 ]] && _msg OK || _msg WARN
_msg EXEC "Parsing metadata for ${IMAGEID}"
local COUNT=1
while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do
metaList[${COUNT}]="$(gen_meta ${IMAGEID}.tags ${COUNT})"
local COUNT=$(( ${COUNT} + 1 ))
done
_msg OK
_msg EXEC "Applying metadata for ${IMAGEID}"
local COUNT=1
while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do
awk 'BEGIN {RS=";"}{print $0}' <<< ${metaList[${COUNT}]} | head -n -1 | metaflac --import-tags-from=- --import-picture-from="${IMAGEID}.jpg" $(printf 'split-track%02d.flac' ${COUNT})
[[ $? -ne 0 ]] && local GEN_META_ERR=true
local COUNT=$(( ${COUNT} + 1 ))
done
[[ ${GEN_META_ERR} ]] && _msg WARN || _msg OK
_msg EXEC "Deploying ${IMAGEID} to ${TARGET}"
local COUNT=1
while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do
local DIR=$(gen_dir ${metaList[${COUNT}]} | sed s'|:|_|g')
mkdir -p "${TARGET}/$(dirname "${DIR}")"
mv $(printf 'split-track%02d.flac' ${COUNT}) "${TARGET}/${DIR}.flac"
echo "${DIR}.flac=${IMAGEID};" >> ${MANIFEST}
local COUNT=$(( ${COUNT} + 1 ))
done
_msg OK
if [[ ${DELETE} ]]; then
_msg EXEC "Dropping data for ${IMAGEID}"
git annex drop ${IMAGEID}.* >${STDERR} 2>&1
_msg OK
fi
}

56
src/deploy_trackid.sh Normal file
View file

@ -0,0 +1,56 @@
#!/bin/bash
# doc deploy_trackid {
#
# DESCRIPTION
# deploy_trackid - Deploys TRACKID of IMAGEID to TARGET
#
# USAGE
# deploy_trackid <path/to/git/dir> <path/to/target> <path/to/db/file> <IMAGEID> <TRACKID_1> [<TRACKID_2>] [<...>]
#
# }
deploy_trackid() {
## Argument parsing
local GIT_DIR="${1}"
local TARGET="${2}"
local DB_FILE="${3}"
local IMAGEID="${4}"
local TRACKID="${5}"
## Path and metadata parsing
local TRACKNO=$(print_track_no ${DB_FILE} ${IMAGEID} ${TRACKID})
local FUTURE_META="$(print_future_meta ${IMAGEID}.tags ${TRACKNO})"
local FUTURE_PATH="${TARGET}/$(print_future_path$ ${FUTURE_META}})"
local PRESENT_PATH="${TARGET}/$(print_future_path ${IMAGEID} ${TRACKNO} ${DB_FILE})"
local PRESENT_META="$(print_future_meta ${PRESENT_PATH})"
local ROW_NO==$(_cfg query '$2=="'${IMAGEID}'" && $3=="'${TRACKID}'"' ${DB_FILE})
# If the track is selected, we will check if the trackid has already been deployed, if not deploy. If the metadata to be
# applied is different than what's deployed, apply new metadata. If the path has changed, move file to new path.
if is_selected ${DB_FILE} ${IMAGEID} ${TRACKID}; then
if [[ -z "${PRESENT_PATH}" ]]; then
deploy_gen ${GIT_DIR} ${IMAGEID} ${TRACKNO}
local PRESENT_PATH="${GIT_DIR}/output.flac"
fi
if [[ "${PRESENT_META}" != "${FUTURE_META}" ]]; then
[[ -z "${PRESENT_META}" ]] || metaflac --remove-all ${PRESENT_PATH}
awk 'BEGIN {RS=";"}{print $0}' <<< ${FUTURE_META} | head -n -1 | metaflac --import-tags-from=- --import-picture-from="${GIT_DIR}/${IMAGEID}.jpg" "${PRESENT_PATH}"
fi
if [[ "${PRESENT_PATH}" != "${FUTURE_PATH}" ]]; then
mkdir -p "$(dirname "${FUTURE_PATH}")"
mv "${PRESENT_PATH}" "${FUTURE_PATH}"
_cfg change TARGET_PATH ${ROW_NO} "${FUTURE_PATH}" ${DB_FILE}
fi
else
if [[ ! -z "${PRESENT_PATH}" ]]; then
rm "${PRESENT_PATH}"
_cfg change TARGET_PATH ${ROW_NO} ""
fi
fi
return 0
}

View file

@ -7,7 +7,4 @@
source /usr/lib/bash/bunc
IFS='
'
MANIFEST=".manifest"
LAST_COMMIT=".last_commit"
EXCLUDE=".exclude"

View file

@ -1,21 +0,0 @@
#!/bin/bash
###
# Returns true if IMAGEID is not excluded from the OPT_EXCLUDE_FILE.
###
is_not_excluded() {
local IMAGEID="${1}"
local EXCLUDE="${2}"
local excludeList=($(cat ${EXCLUDE}))
[[ -z ${excludeList[@]} ]] && return 0
for exclude in ${excludeList[@]}; do
local FIELD=$(echo ${exclude} | cut -d'=' -f1)
local VALUE=$(echo ${exclude} | cut -d'=' -f2)
[[ -z "$(grep -i -l "\"${FIELD}\" : \"${VALUE}\"" ${IMAGEID}.tags)" ]] || return 1
done
return 0
}

21
src/is_selected Normal file
View file

@ -0,0 +1,21 @@
#!/bin/bash
# doc is_selected {
#
# DESCRIPTION
# is selected - Returns true if IMAGEID is defined as selected in DB_FILE.
#
# USAGE
# is_selected </path/to/db/file> <IMAGEID> <TRACKID>
#
# }
is_selected() {
local DB_FILE="${1}"
local IMAGEID="${2}"
local TRACKID="${3}"
ROWNO=$(_cfg query '$2=="'${IMAGEID}'" && $3=="'${TRACKID}'"' ${DB_FILE})
[[ "$(_cfg print SELECTED ${ROWNO} ${DB_FILE})" == "true" ]] && return 0 || return 1
}

View file

@ -1,4 +1,4 @@
#!
#!/bin/bash
###
# Parses arguments and commands from shell

View file

@ -1,11 +1,19 @@
#!/bin/bash
###
# Prints metadata of specified TRACKNUMBER, from textfile following the MTAG specification. Outputs as standard FIELD=VALUE that can then be pipped into metaflac.
###
# doc print_future_meta {
#
# DESCRIPTION
# print_future_meta - Prints metadata of specified TRACKNUMBER, from textfile following
# the MTAG specification. Outputs as standard FIELD=VALUE that can then be pipped into
# metaflac.
#
# USAGE
# print_future_meta </path/to/mtag/file> <track no>
#
# }
print_meta() {
print_future_meta() {
local MTAG=${1}
local TRACKNUMBER="${2}"
local TAGS=$(cat ${MTAG} | sed -e 's/^[[:space:]]*\"//')
@ -14,7 +22,7 @@ print_meta() {
for tag in ${tagList[@]}; do
local COUNT=1
while [[ ${COUNT} -le ${TRACKNUMBER} ]]; do
local PRTVAR=$(print_meta_track ${COUNT} ${tag} ${TAGS})
local PRTVAR=$(print_meta_field ${COUNT} ${tag} ${TAGS})
[[ ! -z ${PRTVAR} ]] && local CURVAR=${PRTVAR}
local COUNT=$(( ${COUNT} + 1 ))
done

42
src/print_future_path Normal file
View file

@ -0,0 +1,42 @@
#!/bin/bash
# doc print_target_path {
#
# DESCRIPTION
# Prints relative output path based on provided metadata TAGS. Expects input from print_meta
# function.
#
# USAGE
# print_target_path <output from print_meta>
#
# }
print_target_path() {
local TAGS="${@}"
local tagList=(GENRE COMPOSER ALBUM ARTIST PERFORMER DATE DISCNUMBER TOTALDISCS TRACKNUMBER TITLE)
for tag in ${tagList[@]}; do
CURTAG="$(awk -v field=${tag} 'BEGIN{RS=";";FS="="}{if($1==field){printf $2}}' <<< ${TAGS})"
eval local ${tag}='${CURTAG}'
done
[[ -z "${GENRE+x}" ]] && echo -n "${GENRE}/"
if [[ -z "${CONDUCTOR+x}" ]]; then
[[ -z "${COMPOSER+x}" ]] && echo -n "${COMPOSER}/"
[[ -z "${ALBUM+x}" ]] && echo -n "${ALBUM}/"
[[ -z "${ARTIST+x}" ]] && echo -n "${ARTIST}/"
[[ -z "${DATE+x}" ]] && echo -n "${DATE}"
if [[ -z "${DATE+x}" ]] && [[ -z "${PERFORMER+x}" ]]; then echo -n "-"; fi
[[ -z "${PERFORMER+x}" ]] && echo -n "${PERFORMER}/"
else
[[ -z "${ARTIST+x}" ]] && echo -n "${ARTIST}/"
[[ -z "${DATE+x}" ]] && echo -n "${DATE}"
if [[ -z "${DATE+x}" ]] && [[ -z "${ARTIST+x}" ]]; then echo -n "-"; fi
[[ -z "${ALBUM+x}" ]] && echo -n "${ALBUM}/\t"
fi
[[ ${TOTALDISCS} -gt 1 ]] && echo -n "${DISCNUMBER}."
printf "%02d" ${TRACKNUMBER}
echo -n " - $(sed 's|/|-|g' <<< ${TITLE} | sed 's|?||g')"
}

17
src/print_present_meta Normal file
View file

@ -0,0 +1,17 @@
#!/bin/bash
# doc print_present_meta {
#
# DESCRIPTION
# print_present_meta - Prints metadata of remote flac in FIELD=VALUE; format.
#
# USAGE
# print_present_meta </path/to/flac
#
# }
print_present_meta() {
local FLAC="${1}"
metaflac --list --block-type=VORBIS_COMMENT "${FLAC}" | grep "comment\[" | awk 'BEGIN{FS=": ";ORS=";"}{print $2}'
}

20
src/print_present_path Normal file
View file

@ -0,0 +1,20 @@
#!/bin/bash
# doc print_present_path {
#
# DESCRIPTION
# Prints path of exiting TRACKID for IMAGEID.
#
# USAGE
# print_present_path <imageid> <track number> </path/to/db/file>
#
# }
print_present_path() {
local IMAGEID="${1}"
local TRACKID="${2}"
local DB_FILE="${3}"
local ROW=$(_cfg query '$2=="'${IMAGEID}'" && $3=="'${TRACKNO}'"' ${DB_FILE})
_cfg print TARGET_PATH ${ROW} ${DB_FILE}
}

View file

@ -1,25 +0,0 @@
#!/bin/bash
###
# Prints output dir based on provided metadata TAGS. Expects input from print_meta function.
###
print_target_dir() {
local TAGS="${@}"
local tagList=(GENRE COMPOSER ALBUM ARTIST PERFORMER DATE DISCNUMBER TOTALDISCS TRACKNUMBER TITLE)
for tag in ${tagList[@]}; do
CURTAG="$(awk -v field=${tag} 'BEGIN{RS=";";FS="="}{if($1==field){printf $2}}' <<< ${TAGS})"
eval local ${tag}='${CURTAG}'
done
if [[ "${GENRE}" == "Classical" ]]; then
echo -n "${GENRE}/${COMPOSER}/${ALBUM}/${ARTIST}/${DATE} - ${PERFORMER}/"
else
echo -n "${GENRE}/${ARTIST}/${DATE} - ${ALBUM}/"
fi
[[ ${TOTALDISCS} -gt 1 ]] && echo -n "${DISCNUMBER}."
printf "%02d" ${TRACKNUMBER}
echo -n " - $(sed 's|/|-|g' <<< ${TITLE} | sed 's|?||g')"
}

View file

@ -1,16 +0,0 @@
#!/bin/bash
###
# Splits specified FLAC image into multiple flac files using a text file formatted under the
# CUE specification
###
split_flac() {
local FLAC="${1}"
local CUE="${2}"
[[ -z "$(cuebreakpoints ${CUE} 2>/dev/null)" ]] && { cat ${FLAC} > split-track01.flac; return 0; }
cuebreakpoints ${CUE} 2>/dev/null | shntool split ${FLAC} -o flac -O always
[[ $? -ne 0 ]] && { cuebreakpoints ${CUE} 2>/dev/null | sed s/$/0/ | shntool split ${FLAC} -o flac -O always; return 2; } || return 0
}

25
src/update_db.sh Normal file
View file

@ -0,0 +1,25 @@
#!/bin/bash
# doc update_db {
#
# DESCRIPTION
# update_db - Updates TARGET's DB_FILE with defined IMAGEIDs
#
# USAGE
# update_db <path/to/git/dir> <path/to/target/dir> <path/to/db/file> <IMAGEID> <...>
#
# }
update_db() {
local GITDIR="${1}"
local TARGET="${2}"
local DB_FILE="${3}"; shift 3
local imageidList=(${@})
for imageid in ${imageidList[@]}; do
trackidList=($(awk 'BEGIN{FS="\" : \"";RS="\",\n * \""}{if($1=="TRACKID"){print $2}}'))
for trackid in ${trackidList[@]}; do
[[ -z $(_cfg query '$2=="'${imageid}'" && $3=="'${trackid}'"' ${DB_FILE}) ]] && _cfg insert true ${imageid} ${trackid}
done
done
}