#!/bin/sh# Author: Andi Clemens, <andi dot clemens at gmx dot net>## Description:## This script will read in the digiKam configuration file to determine the# database location. It will then make sure that digiKam is not running and# call "sqlite3 DATABASE 'VACUUM;" on the database, to clean up and optimize# the tables.# This will often lead to great performance gain and a smaller database file# size.# -----------------------------------------------------------------------------# VARIABLES# -----------------------------------------------------------------------------NAME="Cleanup Databases"VERSION="1.0"USERNAME=$(whoami)DIGIKAM_NAME="digiKam"DIGIKAM_VERSION=$(digikam -v | grep -i digikam | cut -d: -f2 | sed 's/ //g')CONFIGGROUP="Album Settings"DATABASEKEY="Database File Path"CONFIGFILE="digikamrc"DATABASEDIR=$(kreadconfig --file "$CONFIGFILE" --group "$CONFIGGROUP" --key "$DATABASEKEY")HEADER_OFFSET=20OUTPUT_LOCATION="Location"OUTPUT_DATABASE="Database"DB_DIGIKAM="digikam*.db"DB_THUMBS="thumbnails-digikam.db"db_find_cmd="-type f -name '$DB_DIGIKAM'"# -----------------------------------------------------------------------------# FUNCTIONS# -----------------------------------------------------------------------------# $1: char# $2: count (optional)repeat_char(){ for i in $(seq ${2:-$HEADER_OFFSET}); do echo -n "$1" done}# $*: stringpretty_header(){ # use an offset to center text (default = $HEADER_OFFSET) offset=$(repeat_char " ") offset_length=$(expr $(expr length "$offset") \* 2) # calculate length and print header str_length=$(expr length "$*") length=$(expr $str_length + $offset_length) # delimiter line delimiter=$(repeat_char "-" $length) echo $delimiter echo "${offset}$*${offset}" echo $delimiter}checkForRunningProgram(){ proc="$(ps aux | grep $USERNAME | grep -v $0 | grep -w digikam | grep -v grep)" if [ "$proc" != "" ] then echo "Please shutdown $DIGIKAM_NAME before running this script." return 1 fi return 0}help(){ cat <<EOFUsage: cleanup_digikamdb <options>options: -t cleanup thumbnail databases -h show helpEOF exit 1}# -----------------------------------------------------------------------------# MAIN# -----------------------------------------------------------------------------while getopts "th" Optiondo case $Option in t) db_find_cmd="$db_find_cmd -or -name '$DB_THUMBS'";; h) help;; *) help;; esacdonecheckForRunningProgramif [ $? = 1 ]; then exit 1fipretty_header "$NAME v$VERSION ($DIGIKAM_NAME $DIGIKAM_VERSION)"# backup current working dircurdir=$(pwd)# try to enter the database directorycd $DATABASEDIR 2&> /dev/nullif [ $? = 0 ]then db_out="$OUTPUT_DATABASE:" echo -e "$OUTPUT_LOCATION:\t$(pwd)" # --------------------------------------------------------------------- # find all database files files= i=0 while IFS= read -r -d '' file; do files[i++]="$file" done < <(eval "find $db_find_cmd -print0") echo $files # --------------------------------------------------------------------- for db in $files do echo -ne "${db_out:-"\\t"}\t$(basename $db) ... " sqlite3 "$db" "VACUUM;" if [ $? = 0 ] then echo "done!" else echo "failed!" echo "sqlite3 was not able to cleanup the database." fi unset db_out done echo -e "\n=> Finished";else echo -e "\nI was not able to enter the database folder.\n" echo "Make sure that the variable '$DATABASEKEY' in your 'digikamrc' config file" echo "is set correctly and that you have permissions to access the database folder."ficd $curdir