#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Sep. 2001
#
# 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
# =================================================

if [ $# -lt 2 ] || [ $# -gt 3 ]; then
  echo "Usage: $0 <saved config filename> <mountpoint> [<maximum ramdisk size>]"
  exit 1
fi

. /usr/lib/gibraltar-bootsupport/common-definitions.sh

umask 077
set -e

image=$1
mountpoint=$2

if [ "$ETCDISK_SIZE" != "0" ]; then
  # check the syntax of the size string
  check_disk_size_string $ETCDISK_SIZE ETCDISK_SIZE

  if [ -n "$3" ]; then
    check_disk_size_string $3 ""
    ETCDISK_SIZE="$3"
  fi

  echo -n "Creating configuration RAM disk (maximum ${ETCDISK_SIZE}b)... "
  mount_tmpfs $ETCDISK_SIZE $mountpoint
  pushd $mountpoint > /dev/null
  tar xz --preserve --same-owner -f $image
  popd > /dev/null
  echo "done"
else
  echo "Not creating a RAM disk for $2, disabled in /etc/gibraltar/config"
  echo -n "Unpacking saved data to /etc ... "
  pushd $mountpoint > /dev/null
  tar xz --preserve --same-owner -f $image
  popd > /dev/null
  echo "done"
fi

# now remount the ramdisk, applying the maximum size setting from the newly
# loaded configuration data
. /usr/lib/gibraltar-bootsupport/common-definitions.sh
if [ "$ETCDISK_SIZE" != "0" ]; then
  remount_tmpfs $ETCDISK_SIZE $mountpoint
  echo "Successfully resized RAM disk to ${ETCDISK_SIZE}b."
else
  echo "Not resizing RAM disk for $2, disabled in /etc/gibraltar/config"
fi

exit 0
