#!/bin/sh

if [ -e Makefile ]; then
	rm Makefile;
fi

#set default options
PYTHON="--python"
FUSE_GROUP="--fuse-group"

USR_BIN_FILES="backintime backintime-askpass"
FUSE_FILES="sshtools.py encfstools.py"

#get commandline arguments
for arg in $*; do
	case $arg in
		--python | --python2) PYTHON=$arg;;
		--fuse-group | --no-fuse-group) FUSE_GROUP=$arg;;
	esac
done

#patch python command
#use 'python' or 'python2' to start Python Version 2.x
case $PYTHON in
	--python)   sed -e "s/^python2\? /python /g" \
			-e "s/^ssh-agent python2\? /ssh-agent python /g" \
			-i $USR_BIN_FILES
			;;
	--python2)  sed -e "s/^python2\? /python2 /g" \
			-e "s/^ssh-agent python2\? /ssh-agent python2 /g" \
			-i $USR_BIN_FILES
			;;
esac

#patch check for 'fuse' group
#Some distributions require user to be in group 'fuse' to use sshfs and encfs
case $FUSE_GROUP in
	--fuse-group)    sed -e "s/CHECK_FUSE_GROUP = \(True\|False\)/CHECK_FUSE_GROUP = True/"  -i $FUSE_FILES;;
	--no-fuse-group) sed -e "s/CHECK_FUSE_GROUP = \(True\|False\)/CHECK_FUSE_GROUP = False/" -i $FUSE_FILES;;
esac

#check languages
mos=""
langs=""
for langfile in `ls po/*.po`; do
	lang=`echo $langfile | cut -d/ -f2 | cut -d. -f1`
	mos="po/$lang.mo $mos"
	langs="$lang $langs"
done

#Start Makefile
printf "LANGS=$langs\n\n" >> Makefile

#add template
cat Makefile.template >> Makefile

#translate
printf "translate: $mos\n\n" >> Makefile

for lang in $langs; do
	printf "po/$lang.mo: po/$lang.po\n" >> Makefile
	printf "\tmsgfmt -o po/$lang.mo po/$lang.po\n\n" >> Makefile
done

#common langs
printf "install_translations:\n" >> Makefile
for lang in $langs; do
	printf "\tinstall -d \$(DEST)/share/locale/$lang/LC_MESSAGES\n" >> Makefile
	printf "\tinstall --mode=644 po/$lang.mo \$(DEST)/share/locale/$lang/LC_MESSAGES/backintime.mo\n" >> Makefile
done
printf "\n" >> Makefile

printf "All OK. Now run:\n"
printf "    make\n"
printf "    sudo make install\n"

