#!/bin/bash
# preinst script for core-agent-linux
#
# see: dh_installdeb(1)

set -e

# The preinst script may be called in the following ways:
#   * <new-preinst> 'install'
#   * <new-preinst> 'install' <old-version>
#   * <new-preinst> 'upgrade' <old-version>
#
#     The package will not yet be unpacked, so the preinst script cannot rely 
# on any files included in its package. Only essential packages and 
# pre-dependencies (Pre-Depends) may be assumed to be available. 
# Pre-dependencies will have been configured at least once, but at the time the 
# preinst is called they may only be in an "Unpacked" or "Half-Configured" state
# if a previous version of the pre-dependency was completely configured and has
# not been removed since then.
#
#
#  * <old-preinst> 'abort-upgrade' <new-version>
#
#    Called during error handling of an upgrade that failed after unpacking the
# new package because the postrm upgrade action failed. The unpacked files may
# be partly from the new version or partly missing, so the script cannot rely
# on files included in the package. Package dependencies may not be available.
# Pre-dependencies will be at least "Unpacked" following the same rules as
# above, except they may be only "Half-Installed" if an upgrade of the 
# pre-dependency failed.[46]
#
#    For details, see http://www.debian.org/doc/debian-policy/ or
# https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html or
# the debian-policy package

if [ "$1" = "install" ] ; then
    # --------------------------------------------------------------------------
    # Create required directories
    # --------------------------------------------------------------------------
    mkdir -p /var/lib/qubes
    mkdir -p /lib/modules
    #mkdir -p -m 0700 /var/log/xen  # xen-utils-common should do this

    if [ -e /etc/fstab ] ; then 
        mv /etc/fstab /var/lib/qubes/fstab.orig
    fi

    # --------------------------------------------------------------------------
    # Many Qubes scripts reference /bin/sh expecting the shell to be bash but
    # in Debian it is dash so some scripts will fail so force an alternate for
    # /bin/sh to be /bin/bash
    # --------------------------------------------------------------------------
    update-alternatives --force --install /bin/sh sh /bin/bash 999

    # --------------------------------------------------------------------------
    # Modules setup
    # --------------------------------------------------------------------------
    echo "xen_netfront" >> /etc/modules

    # --------------------------------------------------------------------------
    # Remove `mesg` from root/.profile?
    # --------------------------------------------------------------------------
    sed -i -e '/^mesg n/d' /root/.profile

    # --------------------------------------------------------------------------
    # Update /etc/fstab
    # --------------------------------------------------------------------------
    cat > /etc/fstab <<EOF
/dev/mapper/dmroot /         ext4 defaults,noatime 1 1
/dev/xvdc1 swap              swap    defaults 0 0

/dev/xvdb /rw                ext4    noauto,defaults,discard 1 2
/rw/home /home               none    noauto,bind,defaults 0 0

tmpfs /dev/shm               tmpfs   defaults 0 0
devpts /dev/pts              devpts  gid=5,mode=620 0 0
proc /proc                   proc    defaults 0 0
sysfs /sys                   sysfs   defaults 0 0
xen /proc/xen                xenfs   defaults 0 0

/dev/xvdi /mnt/removable     auto    noauto,user,rw 0 0
/dev/xvdd /lib/modules       ext3    defaults 0 0
EOF

    # --------------------------------------------------------------------------
    # User add / modifications
    # --------------------------------------------------------------------------
    id -u 'user' || {
        groupadd -f user
        useradd -g user -G dialout,cdrom,floppy,sudo,audio,dip,video,plugdev -m -s /bin/bash user
    }
    id -u 'tinyproxy' || {
        groupadd -f tinyproxy
        useradd -g tinyproxy -r -M --home /run/tinyproxy --shell /bin/false tinyproxy
    }
    usermod -p '' root
    usermod -L user
    exit 0
fi

if [ "$1" = "upgrade" ] ; then
    exit 0
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

# vim: set ts=4 sw=4 sts=4 et :