#!/bin/bash
########################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
########################################################################
# Copyright (C) 2013 Mithat Konar
# DESTRUCTIVELY convert all svg files in working directory to pdf.
# Source file extension is case sensitive!
# Requires inkscape
EXT=.svg
OPTIONS=""
SCRIPTNAME="$(basename $0)"
# Make list of files to be processed.
FILES=$(ls -1 *${EXT} 2>/dev/null) || { echo "Nothing to convert. No *${EXT} files found."; exit 1; }
# Get confirmation from user to continue.
echo "Preparing to process the following files:
$FILES"
echo
read -p "Destructively convert these to PDF (y/N)? " yn
if [[ "x$yn" != "xy" && "x$yn" != "xY" ]] ; then exit 1; fi
# Make a temp file to store batch commands.
CMDFILE=$(mktemp) || { echo "Could not create temp file." 1>&2; exit 1; }
# Build up the list of commands in temp file.
for file in $FILES
do
base=$(basename $file ${EXT})
echo "${file} --export-pdf=${base}.pdf" >> $CMDFILE
done
# Process commands in a batch.
echo "Hold on ..."
DISPLAY= inkscape ${OPTIONS} --shell < $CMDFILE
rm -f $CMDFILE
echo " ... done."
# Delete old files.
# Since inkscape exits with 0 even with errors, we need to explicitly check
# for conversion before deleting originals.
rv=0
not_deleted=""
for file in $FILES
do
base=$(basename $file ${EXT})
if [[ -f ${base}.pdf ]]; then
rm $file
else
not_deleted="$not_deleted
$file"
rv=1
fi
done
if [[ "x$not_deleted" != "x" ]] ; then
echo
echo "The following files were not deleted for some reason:
$not_deleted"
fi
exit $rv
#-----------------------------------------------------------------------
# Below is an alternative to the approach used above that does not use a
# temp file. It isn't optimal because it restarts inkscape for each file.
# However the simplicity lends itself to porting (e.g., to windows BAT.).
#~ for file in $FILES
#~ do
#~ base=$(basename $file ${EXT})
#~ inkscape --without-gui ${OPTIONS} ${file} --export-pdf=${base}.pdf
#~ done