Skip to content

Instantly share code, notes, and snippets.

@agryson
Last active December 18, 2016 13:26
Show Gist options
  • Save agryson/63c8ded9fbcbd44b485b0473a0263e3b to your computer and use it in GitHub Desktop.
Save agryson/63c8ded9fbcbd44b485b0473a0263e3b to your computer and use it in GitHub Desktop.
Updates the metadata of all svgs in a folder to sensible defaults if empty, doesn't change the field if not
#!/bin/bash
# Loops through all svg files in a directory
# Fields with an existing value are left alone
# Empty fields are filled with sensible defaults
# Licensing or Rights information is flagged for manual review
for file in *.svg
do
me="[agryson] Alexander Gryson"
file_w_type=$(basename "$file")
filename="${file_w_type%.*}"
filelocation=$(readlink -f $file)
IFS='F' read -ra PATHS <<< "$filelocation"
firstcommitdate=$(git log --diff-filter=A --date=short --format='%ad' $file_w_type)
filelocation='F'${PATHS[1]}
committer=$(git log --diff-filter=A --format='[%an]' $file_w_type)
echo ">"$file
#__CREATOR
creator_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:creator/cc:Agent/dc:title" $file)
if [[ ! $creator_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:creator $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:creator" -t elem -n cc:Agent $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:creator/cc:Agent" -t elem -n dc:title -v $committer $file
fi
#__Title
title_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:title" $file)
if [[ ! $title_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:title -v $filename $file
fi
#__Creation_Date
creationDate_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:date" $file)
if [[ ! $creationDate_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:date -v $firstcommitdate $file
fi
#__Relation
relation_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:relation" $file)
relation_url="http://www.freecadweb.org/wiki/index.php?title=Artwork"
if [[ ! $relation_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:relation -v $relation_url $file
fi
#__Publisher
publisher_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:publisher/cc:Agent/dc:title" $file)
if [[ ! $publisher_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:publisher $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:publisher" -t elem -n cc:Agent $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:publisher/cc:Agent" -t elem -n dc:title -v "FreeCAD" $file
fi
#__Identifer
identifier_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:identifier" $file)
if [[ ! $identifier_m ]]; then
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:identifier -v $filelocation $file
fi
#__Rights
rights_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:rights/cc:Agent/dc:title" $file)
if [[ $rights_m ]]; then
echo ""
echo "*** RIGHTS EXIST: "$rights_m" : " $file
echo ""
else
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:rights $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:rights" -t elem -n cc:Agent $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:rights/cc:Agent" -t elem -n dc:title -v "FreeCAD LGPL2+" $file
fi
#__License
license_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/cc:license" $file)
license_tango=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -c "//ns:metadata/rdf:RDF/cc:License" $file)
if [[ $license_m ]]; then
echo ""
echo "*** LICENSE EXISTS, NOT OVERWRITING: "$file
echo ""
/usr/bin/inkscape $file
elif [[ $license_tango ]]; then
echo ""
echo "*** LICENSE EXISTS, NOT OVERWRITING: "$file
echo ""
/usr/bin/inkscape $file
else
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n cc:license -v "https://www.gnu.org/copyleft/lesser.html" $file
fi
#__Contributor
contributor_m=$(xmlstarlet sel -N ns="http://www.w3.org/2000/svg" -t -v "//ns:metadata/rdf:RDF/cc:Work/dc:contributor/cc:Agent/dc:title" $file)
if [[ $contributor_m ]]; then
newContrib=$contributor_m", "$me
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -u "//ns:metadata/rdf:RDF/cc:Work/dc:contributor/cc:Agent/dc:title" -v "$newContrib" $file
else
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work" -t elem -n dc:contributor $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:contributor" -t elem -n cc:Agent $file
xmlstarlet ed -L -N ns="http://www.w3.org/2000/svg" -s "//ns:metadata/rdf:RDF/cc:Work/dc:contributor/cc:Agent" -t elem -n dc:title -v "$me" $file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment