#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2000
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
#

# do not edit below this line
# =================================================

. /etc/default/common-definitions.sh

umask 077
set -e

image=/system/var-defaults.tar.gz
mountpoint=/var

# at first, check if /var is already mounted (e.g. a harddisk partition)
if (mount | grep "/var" | egrep -v "/var/\w+" >/dev/null) ; then
  echo "/var is already mounted - skipping creation of RAM disk"
else
  # not mounted - we have to do something
  # but check if the admin has specified a /var partition on harddisk
  if (cat /etc/fstab | grep -v "^#" | grep "/var" | egrep -v "/var/\w+" >/dev/null) ; then
    # /var partition listed in fstab - there is something wrong
    echo "Warning: /var filesystem is listed in /etc/fstab, but was not mounted."
    echo "There might be something wrong with your setup, creating a ramdisk now."
  fi

  # check the syntax of the size string
  check_disk_size_string $VARDISK_SIZE VARDISK_SIZE

  echo -n "Creating ramdisk for unpacking the $mountpoint image (maximum ${VARDISK_SIZE}b)... "
  mount_tmpfs $VARDISK_SIZE $mountpoint
  echo "done"

  echo -n "Unpacking default $mountpoint tree ... "
  pushd $mountpoint > /dev/null
  tar xz --preserve --same-owner -f $image
  popd > /dev/null
  echo "done"
fi

# now /var should be mounted in any case - create a /var/tmp ramdisk now if
# the admin has not disabled it
if [ "$TMPDISK_SIZE" != "0" ]; then
  check_disk_size_string $TMPDISK_SIZE TMPDISK_SIZE

  echo -n "Creating ramdisk for temporary files (maximum ${TMPDISK_SIZE}b)... "
  mount_tmpfs $TMPDISK_SIZE /var/tmp
  echo "done"
else
  echo "Not creating a ramdisk for temporary files, disabled in /etc/gibraltar_config"
fi

exit 0
