#!/bin/bash # Template CHanger alternative # This script uses xmlstarlet, zip and unzip. echo echo ./cht.sh, CHange Template... echo This script inserts or replaces, a template XML node in a echo LibreOffice Writer file, by modifying meta.xml. It accepts both echo document and master document files and template files. echo xmlstarlet is expected to be installed. echo This is for documentation projects where Writer files and Templates echo reside in the same directory! echo USE AT YOUR OWN RISK, HAVE A BACK-UP READY, wiebe-nedcad-nl echo Run script always in directory with documents, cd your_project echo "Usage: cht " echo Example: ./cht.sh template.ott writer.odt echo ## Variables # General TEMPL=$1 DOC=$2 WORKDIR="temp_extracted" # XML node "meta:template" values echo Node meta:template will contain the following bare attributes: # xlink:type xlink:actuate xlink:title xlink:href meta:date # Static N_XLINK_TYPE="xlink:type" && V_XLINK_TYPE="simple" echo $N_XLINK_TYPE -- $V_XLINK_TYPE # Static N_XLINK_ACTUATE="xlink:actuate" && V_XLINK_ACTUATE="onRequest" echo $N_XLINK_ACTUATE -- $V_XLINK_ACTUATE # Template name N_XLINK_TITLE="xlink:title" NO_EXT="${TEMPL%.*}" V_XLINK_TITLE=${NO_EXT##*/} echo $N_XLINK_TITLE -- $V_XLINK_TITLE # Absolute template location N_XLINK_HREF="xlink:href" V_XLINK_HREF="$PWD/$TEMPL" echo $N_XLINK_HREF -- $V_XLINK_HREF # Template time, Epoch time is lowered a bit to force updating document styles N_META_DATE="meta:date" BELLE_EPOCH_COR=-1 BELLE_EPOCH_X=$(stat -c %Y $TEMPL) BELLE_EPOCH=$((BELLE_EPOCH_X+BELLE_EPOCH_COR)) V_META_DATE=$(date -d @$BELLE_EPOCH +%Y-%m-%dT%T.%9N) echo $N_META_DATE -- $V_META_DATE echo ## Checks # Do files exist with right extension? if [ -f $TEMPL ] && [ "${TEMPL: -4}" == ".ott" ]; then echo "File $TEMPL found." else echo "ERROR: Writer template $TEMPL does not exist or is not valid. Aborting." exit 1 fi if [ -f $DOC ] && ([ "${DOC: -4}" == ".odt" ] || [ "${DOC: -4}" == ".odm" ]); then echo "File $DOC found." else echo "ERROR: Writer document $DOC does not exist or is not valid. Aborting." exit 1 fi # Do we have xmlstarlet? hash xmlstarlet 2>/dev/null || { echo >&2 "ERROR: xmlstarlet is required. Please install this first. Aborting."; exit 1; } ## The proces # Unzip the document file mkdir -p $WORKDIR if [ -z "$(ls -A ./$WORKDIR)" ]; then echo "Ready for processing meta.xml" else echo "ERROR: ./$WORKDIR is not empty and may contain an unzipped document file. Aborting." exit 1 fi unzip -q $DOC -d $WORKDIR cd $WORKDIR # If exists, delete node meta:template, add node and subnodes via pipes xmlstarlet ed -d "/office:document-meta/office:meta/meta:template" meta.xml | \ xmlstarlet ed -s "/office:document-meta/office:meta" -t elem -n meta:template | \ xmlstarlet ed -i "/office:document-meta/office:meta/meta:template" -t attr -n $N_XLINK_TYPE -v $V_XLINK_TYPE | \ xmlstarlet ed -i "/office:document-meta/office:meta/meta:template" -t attr -n $N_XLINK_ACTUATE -v $V_XLINK_ACTUATE | \ xmlstarlet ed -i "/office:document-meta/office:meta/meta:template" -t attr -n $N_XLINK_TITLE -v $V_XLINK_TITLE | \ xmlstarlet ed -i "/office:document-meta/office:meta/meta:template" -t attr -n $N_XLINK_HREF -v $V_XLINK_HREF | \ xmlstarlet ed -i "/office:document-meta/office:meta/meta:template" -t attr -n $N_META_DATE -v $V_META_DATE \ > temp.xml && mv temp.xml meta.xml # What is the result? echo meta.xml is now: && echo cat meta.xml && echo # Back-up original file mv ../$DOC "../${DOC%.*}.bak" echo A .bak file is created && echo # Create modified Writer file, mimetype first zip -rq ../$DOC mimetype . # Cleaning up cd .. rm -rf $WORKDIR echo Finished && echo