#!/bin/bash -ex

PYTHONRELEASE=python2.6
UBUNTURELEASE=10.04

PASSWVAR="tryton"

echo "------------------------------------"
echo "Adding System User"
echo "------------------------------------"

/usr/sbin/adduser --quiet --system --group tryton


echo "------------------------------------"
echo "Adding DB User"
echo "------------------------------------"
/etc/init.d/postgresql-8.4 stop
/etc/init.d/postgresql-8.4 start

su -c - postgres "createuser tryton --no-superuser --createdb --no-createrole" 

#Alter user tryton erp with defined password
cat > /tmp/changepass.sql <<"EOF"
alter user tryton with password '$PASSWVAR';
EOF

su -c - postgres "psql template1 -U postgres -f /tmp/changepass.sql"

/etc/init.d/postgresql-8.4 stop


echo "------------------------------------"
echo "Updating Services Screen"
echo "------------------------------------"
USAGE=/etc/confconsole/services.txt
sed -i -e '6 a Tryton:	$ipaddr:8000' $USAGE
#delete the extra blank line so everything fits on a single screen
sed -i '9 d' $USAGE


echo "------------------------------------"
echo "Updating host name"
echo "------------------------------------"
HOSTNAME=tryton
echo "$HOSTNAME" > /etc/hostname
sed -i "s|127.0.1.1 \(.*\)|127.0.1.1 $HOSTNAME|" /etc/hosts

echo "------------------------------------"
echo "Enabling Ubuntu Multiverse Repository"
echo "------------------------------------"
APTSOURCES=/etc/apt/sources.list.d/sources.list

COMMENT=`grep 'lucid multiverse' $APTSOURCES`
UNCOMMENT=$(echo $COMMENT | sed 's|#||')
sed -i "s|$COMMENT|$UNCOMMENT|" $APTSOURCES

apt-get update
echo "------------------------------------"
echo "Installing Needed Packages"
echo "------------------------------------"

apt-get -y install mercurial python2.6  python-ldap libpq-dev python-dev gcc adduser python-dateutil python-genshi python-lxml python-relatorio python-simplejson python-polib python-pkg-resources python-setuptools python-psycopg2 python-yaml python-cairo python-pycha

easy_install psycopg2

easy_install polib 

easy_install proteus

#Seting up tryton Server


#------Core Tryton Server
echo "------------------------------------"
echo "Downloading Trytond Gource"
echo "------------------------------------"
cd /opt
hg clone http://hg.tryton.org/2.4/trytond/
echo "------------------------------------"
echo "Installing Trytond"
echo "------------------------------------"

chown -R tryton:tryton /opt/trytond
cd /opt/trytond
python setup.py install


echo "------------------------------------"
echo "Downloading Trytond Modules Source"
echo "------------------------------------"

REPO="http://hg.tryton.org/2.4/modules/"

mkdir /opt/tryton_modules

module[0]="account"
module[1]="account_be"
module[2]="account_de_skr03"
module[3]="account_fr"
module[4]="account_invoice"
module[5]="account_invoice_history"
module[6]="account_invoice_line_standalone"
module[7]="account_product"
module[8]="account_statement"
module[9]="account_stock_anglo_saxon"
module[10]="account_stock_continental"
module[11]="analytic_account"
module[12]="analytic_invoice"
module[13]="analytic_purchase"
module[14]="analytic_sale"
module[15]="calendar"
module[16]="calendar_classification"
module[17]="calendar_scheduling"
module[18]="calendar_todo"
module[19]="carrier"
module[20]="carrier_percentage"
module[21]="carrier_weight"
module[22]="company"
module[23]="company_work_time"
module[24]="country"
module[25]="currency"
module[26]="dashboard"
module[27]="google_maps"
module[28]="ldap_authentication"
module[29]="ldap_connection"
module[30]="party"
module[31]="party_siret"
module[32]="party_vcarddav"
module[33]="product"
module[34]="product_cost_fifo"
module[35]="product_cost_history"
module[36]="product_measurements"
module[37]="product_price_list"
module[38]="production"
module[39]="project"
module[40]="project_plan"
module[41]="project_revenue"
module[42]="purchase"
module[43]="purchase_invoice_line_standalone"
module[44]="purchase_shipment_cost"
module[45]="sale"
module[46]="sale_opportunity"
module[47]="sale_price_list"
module[48]="sale_shipment_cost"
module[49]="stock"
module[50]="stock_forecast"
module[51]="stock_inventory_location"
module[52]="stock_location_sequence"
module[53]="stock_lot"
module[54]="stock_product_location"
module[55]="stock_split"
module[56]="stock_supply"
module[57]="stock_supply_day"
module[58]="stock_supply_forecast"
module[59]="stock_supply_production"
module[60]="timesheet"

declare -p module
for  e in ${module[*]}
	do
		cd /opt/tryton_modules		
		echo "------------------------------------"
		echo " - Module: "$e
		echo "------------------------------------"
		hg clone $REPO$e"/"
		echo "------------------------------------"
		echo "   - Downloaded."
		echo "------------------------------------"
		chown -R tryton:tryton $e
		cd $e
		python setup.py install
		echo "------------------------------------"
		echo "   - Installed."
		echo "------------------------------------"
	done

cd /
