#!/bin/bash
####################################################################
#                                                                  #
# unpack-xz.plugin - generic plugin that unpacks:                  #
# tar.xz, tar.lzma, xz and lzma files                              #
#                                                                  #
####################################################################
#                                                                  #
# Copyright 2009 by Zbigniew Luszpinski zbiggy(a)o2,pl under GPLv2 #
#                                                                  #
# Parts of code of this plugin are "borrowed" from                 #
# unpack-*.plugin by Auke Kok                                      #
####################################################################


plugin_unpack_xz() {
  case $1 in
    *.tar.xz)
      if [ ! -x /usr/bin/tar ]; then
        message "${PROBLEM_COLOR}! Cannot unpack tar.xz file without ${MODULE_COLOR}tar${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      if [ ! -x /usr/bin/xz ]; then
        message "${PROBLEM_COLOR}! Cannot unpack tar.xz file without ${MODULE_COLOR}xz${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      debug_msg "Unpacking xz file \"$1\""
      tar Jxf $1 --no-same-owner --no-same-permissions || return 1
      ;;
    *.tar.lzma)
      if [ ! -x /usr/bin/tar ]; then
        message "${PROBLEM_COLOR}! Cannot unpack tar.lzma file without ${MODULE_COLOR}tar${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      if [ ! -x /usr/bin/xz ]; then
        message "${PROBLEM_COLOR}! Cannot unpack tar.lzma file without ${MODULE_COLOR}xz${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      debug_msg "Unpacking xz file \"$1\""
      tar Jxf $1 --no-same-owner --no-same-permissions || return 1
      ;;
    *.xz)
      if [ ! -x /usr/bin/xz ]; then
        message "${PROBLEM_COLOR}! Cannot unpack xz file without ${MODULE_COLOR}xz${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      debug_msg "Unpacking xz file \"$1\""
      cp $1 . || return 1
      xz $1   || return 1
      ;;
    *.lzma)
      if [ ! -x /usr/bin/xz ]; then
        message "${PROBLEM_COLOR}! Cannot unpack lzma file without ${MODULE_COLOR}xz${DEFAULT_COLOR}${PROBLEM_COLOR} installed${DEFAULT_COLOR}";
        exit 1
      fi
      debug_msg "Unpacking xz file \"$1\""
      cp $1 . || return 1
      xz $1   || return 1
      ;;
    *)
      # fallback: we don't know what to do!
      return 2
      ;;
  esac
  # return success!
  return 0
}


plugin_register SOURCE_UNPACK plugin_unpack_xz
