#!/bin/bash
# Filename:      restore-config
# Purpose:       generate grml configuration archive and store it anywhere
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
# Latest change: Son Mai 13 11:12:38 CEST 2007 [mika]
################################################################################

# shellcheck source=/dev/null
{
  . /etc/grml/lsb-functions
}

# set variables  {{{
  DEBUG=0
  LANG=C
  LC_ALL=C
  PROGRAMNAME=${0##*/}
  TMPDIR=/tmp

# functions {{{
debug(){
  if [ "$DEBUG" -gt 0 ] ; then
    echo "debug: $*"
  fi
}

bailout(){
  rm -f "$TMP_FILELIST"
}

trap bailout 1 2 3 15
# }}}

# usage information {{{
usage()
{
  cat >&2 <<EOF
${GREEN}${PROGRAMNAME} - restore configuration of grml system${NORMAL}

${BLUE}Usage:${NORMAL}
  $PROGRAMNAME [-target_options] <configuration_file>

${BLUE}Target options:${NORMAL}
  -home                         extract hidden files from \$HOME (\$HOME/.*)
  -grmlhome                     store hidden files from \$HOME (\$HOME/.*) of user grml [use as user root]
  -etc                          extract modified files from /etc
  -configdir                    extract \$HOME/config

  Default: restore/extract complete archive.

  Notice: it is also possible to use environment variables:
          \$RESTORE_HOME, \$RESTORE_ETC, \$RESTORE_CONFIGDIR and \$RESTORE_ALL

${BLUE}Usage examples:${NORMAL}
  $PROGRAMNAME -home foo_bar_config.tbz  => restore configuration from file foo_bar_config.tbz
  $PROGRAMNAME config.tbz                => restore configuration from file config.tbz

More information on restore-config can be found in the manual page: man restore-config

See also: save-config(1), bootoptions: myconfig=/dev/ice, extract=PATH,
          netconfig=server.tld/path/to/config.tbz

Report bugs, send wishes and feedback to the grml team:
http://grml.org/bugs/ - contact (at) grml.org
EOF
}
# }}}


# extract configuration file {{{
restore_all(){
  echo "Trying to extract $FILENAME"
  ( cd / && unp "$FILENAME" )
}

restore_home(){
  echo "Trying to extract $FILENAME in $HOME"
  ( cd "$HOME" && unp "$FILENAME" -- -x "$HOME" )
}

restore_grmlhome(){
  echo "Trying to extract $FILENAME in /home/grml"
  ( cd /home/grml/ && unp "$FILENAME" -- -x /home/grml )
}

restore_etc(){
  echo "Trying to extract $FILENAME in /etc"
  ( cd /etc && unp "$FILENAME" -- -x /etc )
}

restore_config(){
  echo "Trying to extract $FILENAME in $HOME/config"
  ( cd "$HOME" && unp "$FILENAME" -- -x "$HOME/config" )
}
# }}}


# commandline parsing {{{
parse_options()
{
   if [ "$#" -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
      usage ; exit
   fi

   while [ "$#" -gt 0 ]; do
     case "$1" in
       -h|--help)
         usage ; exit ;;
       -home)
         echo "debug: home is set"
         RESTORE_HOME="yes" ;;
       -grmlhome)
         echo "debug: grmlhome is set"
         RESTORE_GRMLHOME="yes" ;;
       -etc)
         echo "debug: etc is set"
         RESTORE_ETC="yes" ;;
       -configdir)
         echo "debug: configdir is set"
         RESTORE_CONFIGDIR="yes" ;;
       -*)
         echo "Error: Unknown option: $1" >&2
         exit 1 ;;
       *)
         if [ -z "$FILENAME" ]; then
           FILENAME="$(readlink -f "$1")"
         else
           echo "Error: Multiple filenames provided." >&2
           exit 1
         fi ;;
     esac
     shift
   done

   if [ -z "$FILENAME" ]; then
     echo "Error: No filename provided." >&2
     exit 1
   fi
}
parse_options "$@"
# }}}

runit(){
   if [ "$RESTORE_HOME" = "yes" ]; then
     debug "running restore_home"
     restore_home
     RESTORE_SET=1
   fi
   if [ "$RESTORE_GRMLHOME" = "yes" ]; then
     debug "running restore_grmlhome"
     restore_grmlhome
     RESTORE_SET=1
   fi
   if [ "$RESTORE_ETC" = "yes" ] ; then
     debug "running restore_etc"
     restore_etc
     RESTORE_SET=1
   fi
   if [ "$RESTORE_CONFIGDIR" = "yes" ] ; then
     debug "running restore_configdir"
     restore_config
     RESTORE_SET=1
   fi
   debug "FILENAME = $FILENAME"
   if [ -z "$RESTORE_SET" ] ; then
     debug "running restore all"
     restore_all
   fi
}

# now run it
  runit
  bailout

## END OF FILE #################################################################
# vim:foldmethod=marker
