dam/src/chk_nonexistent_ids

40 lines
1.3 KiB
Bash

#!/bin/bash
# doc chk_nonexistent_ids {
#
# DESCRIPTION
# chk_nonexistent_ids - Checks if all trackids in DB_FILE points to an existing file. If not, deletes.
#
# USAGE
# chk_nonexistent_ids </path/to/target/dir> </path/to/db/file/>
#
# }
chk_nonexistent_ids() {
local TARGET="${1}"
local DB_FILE="${2}"
local ERR=false
local rowList=($(awk 'BEGIN{RS="\n";FS="\t";OFS="="}{if($1!="LAST_UPDATE" && $1!="LAST_DEPLOY"){print $2,$3,$4}}' "${DB_FILE}"))
local COUNT=1
for row in ${rowList[@]}; do
local IMAGEID="$(cut -d"=" -f1 <<< ${row})"
local TRACKID="$(cut -d"=" -f2 <<< ${row})"
local FILE="$(cut -d"=" -f3 <<< ${row})"
local NONEXISTENT=false
echo "[ ${COUNT} / ${#rowList[@]} ] Checking path of ${TRACKID}"
if [[ ! -f "${TARGET}/${FILE}" ]] && [[ ${FILE} != "null" ]]; then
gawk -i inplace -v imageid=${IMAGEID} -v trackid=${TRACKID} 'BEGIN{FS="\t";OFS="\t"}{if($2==imageid && $3==trackid){$4="null"}{print $0}}' ${DB_FILE}
local NONEXISTENT=true
fi
[[ "${NONEXISTENT}" == "true" ]] && { echo "[>>>>>>] Error reported"; echo "${TRACKID} of ${IMAGEID} points to non-existent file"}; local ERR=true; }
local COUNT=$(( ${COUNT} + 1 ))
done
[[ "${ERR}" == "true" ]] && return 1 || return 0
}