All pastes #1610944 Raw Edit

Someone

public shellscript v1 · immutable
#1610944 ·published 2009-10-10 22:30 UTC
rendered paste body
#!/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"FOO=0db_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    str_length=$(expr length "$*")    length=$(expr $str_length + $offset_length)    # delimiter line    delimiter=$(repeat_char "-" $length)    # print header    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}foo(){    files=    if [ $FOO = 1 ]; then        files=$(find -type f \( -name "$DB_DIGIKAM" -or -name "$DB_THUMBS" \) -exec echo "'{}'" \;)    else        files=$(find -type f \( -name "$DB_DIGIKAM" \) -exec echo "'{}'" \;)    fi    echo $files}# -----------------------------------------------------------------------------# MAIN# -----------------------------------------------------------------------------while getopts "th" Optiondo    case $Option in        t)  db_find_cmd="$db_find_cmd -or -name '$DB_THUMBS'"; FOO=1;;        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)"    # ---------------------------------------------------------------------    for db in $(foo)    do        echo -ne "${db_out:-"\\t"}\t$(basename $db) ... "        sqlite3 "$db" "VACUUM;"        if [ $? = 0 ]        then            echo "done!"        else            echo "failed!"        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