#!/bin/bash
# Copyright 2006-2017, Matthew Welland.
#
# This file is part of Megatest.
#
# Megatest 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.
#
# Megatest 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 Megatest. If not, see <http://www.gnu.org/licenses/>.
wikiname=$1
FOSSILBIN=fossil
if [ x"$wikiname" == "x" ];then
echo "Usage: viwiki wikipagename"
exit
fi
$FOSSILBIN sync
# wikitmpfile=`mktemp /tmp/${USER}_wikiedit.XXXXXXX`
wikitmpfile=${wikiname}.in
if ! $FOSSILBIN wiki export "$wikiname" 2> /dev/null 1> $wikitmpfile ;then
cat /dev/null > $wikitmpfile
wikipagestate='new'
else
wikipagestate='existing'
fi
# make a backup copy of the extracted file to diff detect if changed
cp $wikitmpfile ${wikitmpfile}.orig
if [[ x"$EDITOR" == "x" ]];then # || [[ x"$VISUAL" == "x" ]];then
EDITOR="gvim -f"
fi
echo $EDITOR | grep -q -e gvim
isGvim=$?
echo $EDITOR | grep -q -e 'gvim.*-f'
hasF=$?
if [[ $isGvim == 0 && $hasF != 0 ]]; then
EDITOR="$EDITOR -f"
fi
$EDITOR $wikitmpfile
if ! diff -q $wikitmpfile ${wikitmpfile}.orig;then
echo "Saving changes to $wikitmpfile to wiki"
if [ $wikipagestate == 'new' ];then
$FOSSILBIN wiki create "$wikiname" $wikitmpfile
else
$FOSSILBIN wiki commit "$wikiname" $wikitmpfile
fi
else
echo "Not saving, no changes to $wikitmpfile."
fi
$FOSSILBIN sync
# NOTE// We *keep* the wikitmpfile but remove the orig copy
rm -f ${wikitmpfile}.orig