#!/bin/bash

ID="`id -u`"
ID2="`id -ur`"
if [ "$ID" != "0" ] && [ "$ID2" != "0" ]; then
  echo "installation have to be done by root!"
  exit 1
fi

BASEDIR="/usr/share/winetools"
DIR="/usr/share/"
VERSION="winetools-0.9"
RELEASE="`echo $VERSION | sed -e "s/winetools-\(.*\)/\1/"`"
WTFILE="`echo $VERSION | sed -e "s/winetools-\(.*\)/wt\1jo/"`"
echo "Version: $VERSION"
echo "Release: $RELEASE"

#for Debian:
#BASEDIR="/usr/share/winetools"
#DIR="/usr/share"

# testing envioronment
# . gettext.sh
which msgfmt &>/dev/null
if [ "$?" != "0" ]; then
  echo "You do not have gettext installed on your system. WineTools will not run in your native language. Rather it will use english."
#  ln -s "$BASEDIR/gettext.sh.dummy" "$BASEDIR/gettext.sh"
#  exit 0
fi

# installation
echo "Installing WineTools to $BASEDIR..."
if [ -e "$BASEDIR" ]; then
  echo "You already installed WineTools. Uninstall the old installation first and come back."
  exit 1
fi
mkdir -p $BASEDIR
cp -R * $BASEDIR
ln -sf $BASEDIR/$WTFILE /usr/bin/wt
ln -sf $BASEDIR/$WTFILE /usr/bin/winetools
ln -sf $BASEDIR/findwine /usr/bin/findwine

if [ ! -e $BASEDIR/Xdialog ] ;then
  # remove probably existing unresolved symbolic link
  rm $BASEDIR/Xdialog
  DIALOG="`which Xdialog`"
  if [ "$?" = "0" ]; then
    ln -s $DIALOG $BASEDIR/Xdialog || echo "Could not link Xdialog to the WineTools directory. You have to do that manually for WineTools to work."
  else
    ln -s $BASEDIR/Xdialog.builtin $BASEDIR/Xdialog || echo "Could not link Xdialog to the WineTools directory. You have to do that manually for WineTools to work."
  fi
fi
	
echo "Installing translations..."
cd "$BASEDIR/po"
for i in $( ls  *.po|cut -f1 -d.); do
	mkdir -p $DIR/locale/$i/LC_MESSAGES
	msgfmt $i.po -o $DIR/locale/$i/LC_MESSAGES/wt0.9.mo
        echo "Installed translations for $i to $DIR/locale/$i/LC_MESSAGES/wt0.9.mo"
done

echo "Start WineTools as *normal* user with \"wt\". Don't use as root!"
