#!/bin/bash
# Works around the following bug by overriding internal Evince defaults:
#   Bug 639003 - Save Settings as default doesn't work
#   https://bugzilla.gnome.org/show_bug.cgi?id=639003
evince="/usr/bin/evince"
setattr="/usr/bin/gvfs-set-attribute --type string"
sleep="/bin/sleep 1"
sync="/bin/sync"

if [ -z "$1" ]; then
    echo "Usage: $0 file"
    exit 1
fi
if [ ! -f "$1" ]; then
    echo "File not found: $1"
    exit 1
fi

# boolean: 0 = false, 1 = true
$setattr "$1" "metadata::evince::continuous" 1
$setattr "$1" "metadata::evince::dual-page" 0
$setattr "$1" "metadata::evince::fullscreen" 0
$setattr "$1" "metadata::evince::inverted-colors" 0
$setattr "$1" "metadata::evince::sidebar_visibility" 1
$setattr "$1" "metadata::evince::show_toolbar" 1

# string: links, thumbnails, attachments, layers, annotations, bookmarks
$setattr "$1" "metadata::evince::sidebar_page" links

# int
$setattr "$1" "metadata::evince::sidebar_size" 132

# string: best-fit, fit-width, free
$setattr "$1" "metadata::evince::sizing_mode" fit-width

# double: 1.0 = 100%
$setattr "$1" "metadata::evince::zoom" 1.0

# Force write of metadata; add sleep if getting segmentation faults.
$sync
# $sleep
$evince "$1"
