#!/bin/sh
set -e

# Backport configuration
# Note: Some packages break the base system so we cannot just get
# everything from backports...
apt-get update
apt-get -y --force-yes -t etch-backports install \
    fluxbox \
    geany \
    smbfs \
    ca-certificates \
    smbclient \
    dhcp3-client \
    vim \
    xpdf \
    openssl-blacklist \
    openssl-blacklist-extra

# todo: remove later
mkdir -p /etc/skel/.idesktop

# Ensuring correct file mode for template user
# Note: can be done in git but this is safer
find /etc/skel -type d -exec chmod u+rwx,go-rwx {} \;
find /etc/skel -type f -exec chmod u+rw,go-rw {} \;
find /etc/skel \( \
    -name '.xinitrc' -o \
    -name '.xsession' -o \
    -path '/etc/skel/.fluxbox/startup' -o \
    -name '*.sh' -o \
    -name '*.run' \
\) -type f -exec chmod u+x {} \;


chmod +xr,ug+xrw,o-w /opt/retrodebian/bin/set-resolution

chmod +r,ug+xw,o-w /opt/retrodebian/fluxbox/watch-menu.sh
chmod +r,ug+xw,o-w /opt/retrodebian/fluxbox/update-menu.sh
chmod +r,ug+xw,o-w /etc/init.d/retrodebian-fluxbox-watch-menu
update-rc.d retrodebian-fluxbox-watch-menu defaults

chmod +r,ug+xw,o-w /opt/retrodebian/xorg/autoconfig.sh
chmod +r,ug+xw,o-w /opt/retrodebian/xorg/video-detect.d/*.sh
chmod +r,ug+xw,o-w /opt/retrodebian/xorg/mouse-detect.d/*.sh
chmod +r,ug+xw,o-w /etc/init.d/retrodebian-xorg-autoconfig
update-rc.d retrodebian-xorg-autoconfig defaults

# Generating and configuring Locale
if [ -x /usr/sbin/locale-gen ]; then
    grep -q '^en_US.UTF-8 UTF-8$' /etc/locale.gen 2>/dev/null || echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen
    locale-gen
fi

cat > /etc/default/locale <<'EOF'
LANG=en_US.UTF-8
LANGUAGE=en_US:en
EOF

# Allow all (new) users to do: nice -n -5
sed -i \
  -e 's/^#\s*EXTRA_GROUPS=/EXTRA_GROUPS=/' \
  -e 's/^#\s*ADD_EXTRA_GROUPS=/ADD_EXTRA_GROUPS=/' \
  /etc/adduser.conf

grep -q '^@users[[:space:]]\+-[[:space:]]\+nice[[:space:]]\+-5$' /etc/security/limits.conf || \
echo '@users   -   nice   -6' >> /etc/security/limits.conf

grep -q '^session[[:space:]]\+required[[:space:]]\+pam_limits\.so$' /etc/pam.d/common-session || \
echo 'session required pam_limits.so' >> /etc/pam.d/common-session

# Compatibility fix link (ut99)
ln -sf /usr/lib/libSDL-1.2.so.0 /usr/lib/libSDL-1.1.so.0

# Enabling ntp client
update-rc.d ntp defaults || true

# set default terminal
update-alternatives --set x-terminal-emulator /usr/bin/urxvt