#!/bin/sh

# Palringo client Linux install script
# Copyright (c) 2008, Palringo Ltd
#
# Author: Xabier Eizmendi
#
# You can freely distribute this script.
#
# The purpose of this script is to automate the process of installing Palringo
# in a Linux system

PALRINGO_TEMP_DIR=/tmp/Palringo
PALRINGO_NAME="Palringo"
PALRINGO_URL=http://download.palringo.com/Palringo-x86-SVN.exe
PALRINGO_EXE=Palringo-x86-SVN.exe
FONTS_NAME="Tahoma fonts"
FONTS_URL=http://download.palringo.com/tahoma.tar.gz
FONTS_FILE=tahoma.tar.gz
FONTS_OPTION=1
FONT_INSTALL_DIR=~/.fonts
IMAGE_VIEW_NAME="Fast Stone Maxview 2.1"
IMAGE_VIEW_URL=http://www.faststone.org/DN/FSMaxViewSetup21.exe
IMAGE_VIEW_EXE=FSMaxViewSetup21.exe
IMAGE_VIEW_OPTION=1
DOWNLOAD_URL=""
DOWNLOAD_NAME=""
OPTION=1

create_temp_dir()
{
  if [ ! -d $PALRINGO_TEMP_DIR ]; then 
    echo "Creating temporary directory $PALRINGO_TEMP_DIR to download Palringo..."
    mkdir $PALRINGO_TEMP_DIR 
  fi
}

clean_temp_files()
{
  echo "Cleaning temporary files..."
  rm -fR $PALRINGO_TEMP_DIR
}

ask_option()
{
  echo "If you want to install $DOWNLOAD_NAME type 1, if not type 0, and then [ENTER]:"
  read OPTION
}

download_file()
{
  echo "Downloading $DOWNLOAD_NAME..."
  if [ `which wget` ]; then
    wget -N -P /tmp/Palringo $DOWNLOAD_URL
    if [ $? -ne 0 ]; then
      echo "Error while downloading $DOWNLOAD_NAME. Exiting..."
      exit 1
    fi
  else
    echo "Error: This script requires wget to download all the files"
    clean_temp_files
    exit 1
  fi
}

install_palringo()
{
  if [ `which wine` ]; then
    if [ $FONTS_OPTION -ne 0 ]; then
      echo "Installing $FONTS_NAME..."
      if [ ! -d ~/.fonts ]; then 
	mkdir ~/.fonts
      fi
      cd ~/.fonts
      tar xvfz /tmp/Palringo/$FONTS_FILE
      if [ $? -ne 0 ]; then
	echo "Error while decompressing fonts"
	exit 1
      fi
      if [ `which fc-cache` ]; then
	fc-cache
      else
	echo "Couldn't install Tahoma font in your system. This is optional"
	echo "but Palringo does look better with it. You can download it from $FONTS_URL"
      fi
    fi
    if [ $IMAGE_VIEW_OPTION -ne 0 ]; then
      echo "Installing $IMAGE_VIEW_NAME. Make sure you associate JPG files with it."
      wineconsole /tmp/Palringo/$IMAGE_VIEW_EXE
    fi
    echo "Installing $PALRINGO_NAME..."
    wineconsole /tmp/Palringo/$PALRINGO_EXE
    clean_temp_files
    echo "Palringo Client succesfully installed in your system"
  else
    if [ `which apt-get` ]; then
      echo "Run sudo 'apt-get install wine' to install wine and then run this script again"
    elif [ `which yum` ]; then
      echo "Run 'sudo yum install wine' to install wine and then run this script again"
    else
      echo "Install wine and then run this script again"
    fi
    echo "All the downloaded files will be kept for the next time you run this script"
    exit 1
  fi
}

cat <<EOF
*****************************************************************************
*          Palringo client Linux install script                             *
*                                                                           *
* Installing the fonts and the image  viewer is optional. But if you don't  *
* have any image viewer installed in wine you won't be able to see photo    *
* messages                                                                  *
*****************************************************************************
EOF

create_temp_dir

DOWNLOAD_URL=$PALRINGO_URL
DOWNLOAD_NAME=$PALRINGO_NAME
download_file

DOWNLOAD_URL=$FONTS_URL
DOWNLOAD_NAME=$FONTS_NAME
ask_option
FONTS_OPTION=$OPTION
if [ $FONTS_OPTION -ne 0 ]; then
  download_file
fi

DOWNLOAD_URL=$IMAGE_VIEW_URL
DOWNLOAD_NAME=$IMAGE_VIEW_NAME
ask_option
IMAGE_VIEW_OPTION=$OPTION
if [ $IMAGE_VIEW_OPTION -ne 0 ]; then
  download_file
fi

install_palringo
