#!/bin/bash
# -*- coding: utf-8 -*-
#
# SERVER SCOL INSTALLATION SCRIPT FOR GNU/LINUX
# ADD STUFF SCRIPT
#
# Copyright (C) 2010 Stéphane Bisaro, aka iri <iri@irizone.net>
# based on a previous work (installation only ) by Patrice Favre, 03/12/99
#
# This program 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.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# More infos availables on : http://www.irizone.net or http://www.scolring.org/
#

URL_BASE=http://www.scolring.org/files/rscs/ # url server

URL_DMS=dms_dist_2.5.2.tar.gz # scs software files needed
URL_EMAGINER=emdms_luxury.tar.gz # e_maginer software files needed
URL_CRYONICS=cryonics_1.5.1.tar.gz # cryonics software files needed

URL_GOODIES=goodies_1.0.tar.gz # tools + worlds in 'partition' directory

URL_LIB=lib_full.tar.gz # textures and 3d objects in 'partition/lib' subdirectory








#
### ### ### ### ###
# 
# FUNCTIONS
#
### ### ### ### ###
#


### Misc.

# Cancel to install
Cancel () {
  Echo "$MCANCEL"
  exit 1
}

# Display all \char if any (option : -e)
Echo () {
  echo -e $*
}

# Display the same character $2 repeated $1 times
MultipleChar () {
  for ((i=0; i < $1; i++))
  do
    echo -n $2
  done
}




#
### ### ### ### ###
# 
# MENU PROCEDURE
#
### ### ### ### ###
#


F_menu () {
  Echo ""
  select menu in "dms" "goodies" "e_maginer" "cryonics" "3d and textures" "size only" "exit"; do
    case $REPLY in
      1)
        F_dms ; break ;;
      2)
        F_goodies ; break ;;
      3)
        F_e_maginer ; break ;;
      4)
        F_cryonics ; break ;;
      5)
        F_lib ; break ;;
      6)
        F_size ; break ;;
      7)
        F_exit ; break;;
      *)
        Echo $MSTUFFBADSELECT ;;
    esac
  done
}




#
### ### ### ### ###
# 
# DOWNLOAD and INSTALL PROCEDURES
#
### ### ### ### ###
#

F_do () {
	wget --progress=dot:mega "$URL_BASE$1"
	cd "$SRCDIR"/"$2"/
	tar -xzf "$SRCDIR"/"$1"
	cd "$SRCDIR"
	rm $1
	F_menu
}

F_dms () {
  F_do "$URL_DMS" "common/scs"
}

F_goodies () {
  F_do "$URL_GOODIES" "partition"
}

F_e_maginer () {
  F_do "$URL_EMAGINER" "common/emaginer"
}

F_cryonics () {
  F_do "$URL_CRYONICS" "common/cryonics"
}

F_lib () {
  F_do "$URL_LIB" "partition"
}


# extract the size from the header (server response)
F_size_echo () {
  Echo "$3 ( ${2%.tar.gz} ) :" 
  wget --spider -S $1$2 2>&1 | sed -n '/^\(Longueur\|Length\)/ s#.*(\(.*\)).*#\1#p'  
}

F_size () {
  Echo $MSTUFFSIZE
  F_size_echo $URL_BASE $URL_DMS "DMS"
  F_size_echo $URL_BASE $URL_GOODIES "GOODIES"
  F_size_echo $URL_BASE $URL_EMAGINER "E_MAGINER"
  F_size_echo $URL_BASE $URL_CRYONICS "CRYONICS"
  F_size_echo $URL_BASE $URL_LIB "3D & TEXTURE"
  Echo $MSTUFFSIZEFINISHED
  F_menu
}


F_exit () {
  Echo "$MSTUFFFINISHED"
  exit 0
}






#
### ### ### ### ###
# 
# MAIN PROCEDURE
#
### ### ### ### ###
#


# Language definition : french or english
if [[ $LANG == *fr* ]]; then
  DEFLANG=french
else
  DEFLANG=english
fi
LangFile=install_$DEFLANG.lang
if [ ! -f $LangFile ]; then
  Echo "Lang file $LangFile not found"
  Cancel
fi
while read k v; do
  eval "$k=\"$v\""
done <$LangFile

SRCDIR=${PWD%/} # remove the last slash, if any
ID=`id -u`

# Title
Echo ""
Echo "\t\t"`MultipleChar ${#MSTUFFTITLE} "="`
Echo "\t\t$MSTUFFTITLE"
Echo "\t\t"`MultipleChar ${#MSTUFFTITLE} "="`
Echo ""

Echo "$MWELCOME"

if [ ! -f "$SRCDIR"/usm.ini ]; then
  Echo "$MUNNOSCOL"
  Cancel
fi

PS3=$MSTUFFPS3
select menu in "dms" "dms_full" "e_maginer" "cryonics" "3d and textures" "size only" "exit"; do
  case $REPLY in
    1)
      F_dms ; break ;;
    2)
      F_goodies ; break ;;
    3)
      F_e_maginer ; break ;;
    4)
      F_cryonics ; break ;;
    5)
      F_lib ; break ;;
    6)
      F_size ; break ;;
    7)
      F_exit ; break;;
    *)
      Echo $MSTUFFBADSELECT ;;
  esac
done

cd "$SRCDIR"
exit 0
