#!/bin/sh

### [ modify fstab ] ###
## as long as you don't need al lot of space for /tmp and /var/tmp
## it is ok to use the following settings in /etc/fstab
## the available space depends on the system memory
## it is ever the half of the system memory
#cat >> /etc/fstab <<EOF
##
#tmpfs                                     /tmp      tmpfs defaults            0 0
##
#tmpfs                                     /var/tmp  tmpfs defaults            0 0
#EOF


### [ set host and domain name information ] ###
# read install info and split - one parameter per line
INSTALL_INFO="$(sed 's/\s\+/\n/g' /root/.install-info)"

# get hostname from opsi parameters
HOSTNAME=$(echo "${INSTALL_INFO}" | grep -o '^hn=[^ ]\+')
# use last value for hostname
HOSTNAME=${HOSTNAME##*=}

# if hostname is not defined by opsi
if [ -z "${HOSTNAME}" ] ; then
  # read hostname parameter
  HOSTNAME=$(echo "${INSTALL_INFO}" | grep -o '^hostname=[^ ]\+')
  # use last value for hostname
  HOSTNAME=${HOSTNAME##*=}
fi

# get domain from opsi parameters
DOMAIN=$(echo "${INSTALL_INFO}" | grep -o '^dn=[^ ]\+')
# use last value for domain
DOMAIN=${DOMAIN##*=}

# if domain is not defined by opsi
if [ -z "${DOMAIN}" ] ; then
  # read domain parameter
  DOMAIN=$(echo "${INSTALL_INFO}" | grep -o '^domain=[^ ]\+')
  # use last value for domain
  DOMAIN=${DOMAIN##*=}
fi

# if hostname and domain not empty
if [ "${HOSTNAME}" != "" -a "${DOMAIN}" != "" ] ; then
  # set system fqdn
  sed "s/^\(127.0.1.1\s\+\).*$/\1${HOSTNAME}.${DOMAIN} ${HOSTNAME}/" -i /etc/hosts
  # set system hostname
  echo "${HOSTNAME}" > /etc/hostname
fi


### [ setup root home directory ] ###
# because the /root directory can contain very sensitive data so we set this permissions
chmod 0700 /root

# create some usefull directories in /root
mkdir -m 0700 /root/.ssh
mkdir -m 0700 /root/bin


### [ setup ssh public keys for root access ] ###
# add ssh public keys
wget -O /root/.ssh/authorized_keys http://download.bmsoft.de/pub/deploy.pub

# this line can be remove, if it is not longer needed
cp /root/.ssh/authorized_keys /deploy.pub


### [setup salt minion ] ###
# configure the salt master
echo 'master: salt.bmsoft.de' > /etc/salt/minion.d/99-master-address.conf


### [ modify apt configuration ] ###
# ensure jessie is always the default for packages, even on mixed systems
echo 'APT::Default-Release "jessie";' > /etc/apt/apt.conf.d/000release

# add apt repository for saltstack
cat > /etc/apt/sources.list.d/saltstack.list <<EOF
### [ packages ] ###
deb   http://debian.saltstack.com/debian jessie-saltstack main
EOF

# add apt repositories for debian
cat > /etc/apt/sources.list.d/jessie.list <<EOF
### [ packages ] ###
deb     http://ftp.de.debian.org/debian/ jessie main contrib non-free
deb-src http://ftp.de.debian.org/debian/ jessie main contrib non-free

### [ security ] ###
deb     http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

### [ volatile ] ###
#deb     http://ftp.de.debian.org/debian/ jessie-updates main contrib non-free
#deb-src http://ftp.de.debian.org/debian/ jessie-updates main contrib non-free

# enable the following lines, if you want
# warning the are proposals, some times they are not as stable as normal packages
#### [ proposed ] ###
#deb     http://ftp.de.debian.org/debian/ jessie-proposed-updates main contrib non-free
#deb-src http://ftp.de.debian.org/debian/ jessie-proposed-updates main contrib non-free
EOF

# disable apt installation repositories, to avoid duplicate entries
sed 's/^/#/' -i /etc/apt/sources.list

# update repository informations
aptitude update

