#!/bin/sh
#       dai-installer.sh
#       
#       Copyright 2010 elwin013 <kontakt AT elwin013.com>
#
#       This program 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. 
#
#       Name: Daimonin Installer
#       Author: elwin013 < kontakt at elwin013.com >
#       Version: 0.3
#       Webpage: http://daimonin.elwin013.com/

ACTION=$1
MODE=$2
DISTRO=$3

print_help() {
 echo " Daimonin Installer v0.3 for main version."
 echo " Install Daimonin MMORPG (http://www.daimonin.org/) with one command!
 Script webpage: http://daimonin.elwin013.com/
 Usage: 
  sh dai-installer.sh action mode distribution(optional)
  
  action 
    what you want to do - install, remove, or update to latest version
  mode 
    single user (install in home directory) or multi user 
    (install in /usr) mode
  distribution 
    (optional) distribution which you're using - debian, ubuntu, fedora, 
    arch, opensuse - installs needed dependencies
  
  Examples:
   - multi user install mode on Ubuntu:
      sh dai-installer.sh install multi ubuntu
   - single user remove mode on Fedora:
      sh dai-installer.sh remove single fedora
   - multi user update:
      sh dai-installer update multi"
 break
}
install_dependencies() {
 echo "Script need password to install dependencies (root or user if sudo (e.g. in Ubuntu) used):
libsdl, libsdl-mixer, libsdl-image, physfs and curl."
 case $DISTRO in
  debian)
  su -c "apt-get update && apt-get install libsdl1.2-dev libsdl-mixer1.2-dev libsdl-image1.2-dev libcurl4-gnutls-dev libphysfs-dev autoconf automake"
  ;;
  ubuntu)
  sudo apt-get update
  sudo apt-get install libsdl1.2-dev libsdl-mixer1.2-dev libsdl-image1.2-dev libcurl4-gnutls-dev libphysfs-dev autoconf automake
  ;;
  arch)
  su -c "pacman -Sy sdl_mixer sdl_image physfs curl automake autoconf"
  ;;
  fedora)
  su -c "yum install physfs sdl curl autoconf automake"
  ;;
  opensuse)
  su -c "zypper install curl sdl sdl_image sdl_mixer physfs autoconf automake"
  ;;
  *)
  echo "Not found or not supported at this time"
  ;;
 esac
 prepare_main;
}
clean() {
 rm -rf ~/.daimonin/installer/client
 rm -rf ~/.daimonin/installer/main
}
upgrade() {
 if [ "$MODE" = "" ]; then print_help;
 else upclean;
 fi
 prepare_main;
}
upclean() {
 clean;
 rm -rf ~/.daimonin/installer/client.tar.gz
 echo "Before installation new version script need to delete old files"
 if [ "$MODE" = "single" ]; then remove_single;
 elif [ "$MODE" = "multi" ]; then remove_multi $DISTRO;
 else print_help;
 fi
}
prepare_main() {
 echo "==> Creating directories"
 mkdir -p ~/.daimonin/installer/main
 cd ~/.daimonin/installer
 echo "==> Downloading base source"
 wget -c http://daimonin.svn.sourceforge.net/viewvc/daimonin/main/client.tar.gz -O client.tar.gz
 echo "==> Unpacking"
 tar xzf client.tar.gz
 mv -f client/* main/
 cd main/make/linux
 if [ "$MODE" = "single" ]; then install_single;
  elif [ "$MODE" = "multi" ]; then install_multi;
  else print_help;
 fi
}
install_single() {
 ./bootstrap
 ./configure
 make
 make install
 create_desktop_shortcut;
 echo "=> GAME WAS SUCESFULLY INSTALLED!
=> Enter ~/.daimonin folder and run daimonin! Or run shortcut placed on desktop"
 clean;
}
install_multi() {
 create_shortcut;
 create_starter;
 ./bootstrap
 ./configure
 make
 make install
 DIRECTORY="`cd ~/ && pwd`"
 if [ "$DISTRO" = "" ]; then
  echo "Please write your super user password - it's needed to place binary and files in /usr folder"
  su -c "mv $DIRECTORY/daimonin /usr/share && mv daimonin.sh /usr/bin/daimonin && mv daimonin.desktop /usr/share/applications"
 elif [ "$DISTRO" = "ubuntu" ]; then
  echo "Please write your user password - it's needed to place binary and files in /usr folder"
  sudo mv $DIRECTORY/daimonin /usr/share
  sudo mv daimonin.sh /usr/bin/daimonin
  sudo mv daimonin.desktop /usr/share/applications
 else print_help;
 fi
 echo "=> GAME WAS SUCESFULLY INSTALLED!"
 clean;
}
create_desktop_shortcut() {
 touch ~/Desktop/daimonin.desktop
 echo "[Desktop Entry]
Type=Application
Version=1.0
Name=Daimonin
GenericName=MMORPG game
Comment=Free, open source, Massively Multiplayer Online RPG game
Icon="`cd ~/ && pwd`"/daimonin/bitmaps/icon.png
Categories=Game
TryExec=~/daimonin/
Exec=daimonin" >> ~/Desktop/daimonin.desktop
}
create_shortcut() {
 touch daimonin.desktop
 echo "[Desktop Entry]
Type=Application
Version=1.0
Name=Daimonin
GenericName=MMORPG game
Comment=Free, open source, Massively Multiplayer Online RPG game
Icon=/usr/share/daimonin/bitmaps/icon.png
Categories=Game
Exec=daimonin" >> daimonin.desktop
}
create_starter() {
 touch daimonin.sh
 echo "#!/bin/sh" >> daimonin.sh
 echo "cd /usr/share/daimonin" >> daimonin.sh
 echo "./daimonin" >> daimonin.sh
 chmod +x daimonin.sh
}
remove_single() {
 rm -rf ~/daimonin
 rm -rf ~/daimonin-0.10.0
 rm ~/Desktop/daimonin.desktop
 echo "GAME REMOVED!"
}
remove_multi() {
 if [ "$DISTRO" = "ubuntu" ]; then
  sudo rm -rf /usr/share/daimonin &&
  sudo rm -rf /usr/share/daimonin-0.10.0 &&
  sudo rm /usr/share/applications/daimonin.desktop &&
  sudo rm /usr/bin/daimonin &&
  echo "GAME SUCCESFULLY REMOVED"
 else
  su -c "rm -rf /usr/share/daimonin && rm -rf /usr/share/daimonin-0.10.0 && rm /usr/share/applications/daimonin.desktop && rm /usr/bin/daimonin" && 
  echo "GAME SUCCESFULLY REMOVED"
 fi
}

case $ACTION in
 install)
  if [ "$DISTRO" = "" ]; then prepare_main;
  else install_dependencies;
  fi
 ;;
 update)
  upgrade;
 ;;
 remove)
  if [ "$MODE" = "single" ]; then remove_single;
  elif [ "$MODE" = "multi" ]; then remove_multi;
  else print_help;
  fi
 ;;
 *)
  print_help;
 ;;
esac

