#!/bin/bash # doc db_set { # # DESCRIPTION # db_set - Command selects or deslects all TRACKIDs that matches the CONDITION # # USAGE # db_set [<...>] # # CONDITIONS # CONDITIONS are defined using the following format: # = # The metadata field can be anything that can be injected as metadata in a Vorbis or id3tag. # # } db_set() { local GIT_DIR="${1}" local DB_FILE="${2}" local SELECTED="${3}"; shift 3 local CONDITION="${@}" if [[ "${CONDITION}" == "all" ]]; then gawk -i inplace -v selected=${SELECTED} 'BEGIN{FS="\t";OFS="\t";}{if($1!="LAST_DEPLOY" && $1!="LAST_UPDATE" && $1!="REPO_ID" && $1!="null"){$1=selected}{print $0}}' "${DB_FILE}" cat "${DB_FILE}" return 0 fi # Defines what imageids are imported into database, as we're only going to act on those local FIELD="$(echo ${CONDITION} | cut -d'=' -f1 | tr '[:lower:]' '[:upper:]' )" local VALUE="$(echo ${CONDITION} | cut -d'=' -f2 | tr '[:lower:]' '[:upper:]' )" # Determines which IMAGEIDs present in the DB_FILE match the CONDITION local COUNT=1 local dbimageidList=$(awk 'BEGIN{FS="\t"}{if($1!="LAST_DEPLOY" && $1!="LAST_UPDATE" && $1!="REPO_ID" && $1!="null"){print $2}}' ${DB_FILE} | awk '!seen[$0]++') for dbimageid in ${dbimageidList[@]}; do local MTAG="$(printf "${GIT_DIR}/%s.tags" ${dbimageid})" local MATCH="$(grep -i -l "\"${FIELD}\" : \"${VALUE}\"" "${MTAG}")" local imageidList[${COUNT}]="$(basename -s .tags ${MATCH} 2>/dev/null)" local COUNT=$(( ${COUNT} + 1 )) done # Goes through imageidList to determine which TRACKID matches the CONDITION for imageid in ${imageidList[@]}; do # Defines TOTALTRACKS of IMAGEID local TOTALTRACKS=$(grep "@" ${GIT_DIR}/${imageid}.tags | wc -l) # Loop goes through all of the tracks in IMAGEID, and determines which of the TRACKIDs has a FIELD that matches the VALUE local COUNT=1 while [[ ${COUNT} -le ${TOTALTRACKS} ]]; do local TRACK_VALUE=$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} ${FIELD} | cut -d'=' -f2 | sed 's|.$||' | tr '[:lower:]' '[:upper:]') # If print_meta_mtag extract the same VALUE from FIELD as the CONDITION, the TRACKID of this TRACK is added into trackidList if [[ "${TRACK_VALUE}" == "${VALUE}" ]]; then local TRACKID=$(print_meta_mtag "${GIT_DIR}/${imageid}.tags" ${COUNT} TRACKID | cut -d'=' -f2 | sed 's|.$||') _msg ECHO "Track ${COUNT} of ${imageid} matches condition. Adding to trackidList" local trackidList[${COUNT}]="${TRACKID}" fi local COUNT=$(( ${COUNT} + 1 )) done # Changes state of trackids that matched for trackid in ${trackidList[@]}; do awk -v imageid=${imageid} -v selected=${SELECTED} -v trackid=${trackid} 'BEGIN{FS="\t";OFS="\t"}{if($2==imageid && $3==trackid){$1=selected}{print $0}}' ${DB_FILE} > ${DB_FILE}.tmp; mv ${DB_FILE}.tmp ${DB_FILE} done done }