#!/bin/bash

MARK="\n#This file auto fill with Office@Etersoft settings\n"
CONFIGDIR="./files/"
USESUDO="sudo"
DELUSER="remove_me"

setAcrh()
{
if [ $(arch) = "x86_64" ]
then ARCH=`arch`
else ARCH="i586"
fi
echo "ARCH set to $ARCH"
}

textToFile() #write file $1 to file $2
{
$USESUDO cp "$2" "$2"_old

if [ -e $1 ] && ! grep -q "$MARK" $2 # if file exist and doesnt contain mark do copy
then
cat $1 | sed -e "s|\$ARCH|$ARCH|g" | $USESUDO cat >>$2
$USESUDO echo -e "$MARK" >> $2
fi

if [ -e $1 ] && [ -e $2 ]
then
diff -u "$2"_old "$2"
fi
}

startService()
{ #Power on services
#xfconf-query -c xfce4-session -p /startup/ssh-agent/type -s "ssh-agent" -t string -n #for user
service nfslock start
service rpc.bind start
service sshd start

#Autostart services
chkconfig nfslock on
chkconfig rpc.bind on
chkconfig sshd on
}


setMount()
{ #Automount in fstab
mkdir -p /var/ftp/
mkdir -p /var/ftp/pvt
mkdir -p /var/ftp/pub
mkdir -p /var/ftp/tmp

textToFile ./files/fstab /etc/fstab #TODO with EOF
mount -a >> ./post-install.log
#fstab.d eter.office
}

setRepository()
{
textToFile ./files/etersoft /etc/apt/sources.list.d/etersoft.list

apt-get update >> install.log
apt-get distr-update
apt-get install eepm
epmi --auto chromium eepm etckeeper etersoft-devel-utils etersoft-build-utils git vbox-client nxclient keepassx libnss-mysql libnss-role psi

etckeeper init
}

setHome()
{
#$USESUDO
textToFile ./files/role /etc/role

#Autentification
rm -rf /etc/nsswitch.conf /etc/libnss-mysql.cfg /etc/libnss-mysql-root.cfg
textToFile ./files/nsswitch.conf /etc/nsswitch.conf
textToFile ./files/libnss-mysql.cfg /etc/libnss-mysql.cfg
textToFile ./files/libnss-mysql-root.cfg /etc/libnss-mysql-root.cfg

useradd -u 2000 -d /dev/null -s /dev/null uidrange
#killall -u remove_me
userdel remove_me
}
#supst

testText(){
echo "string with config" >> a
textToFile a b

textToFile c d
}

setAcrh
#testText
startService
setMount
setRepository
setHome