#!/bin/bash
# Author: Tomas M. <http://www.slax.org/>

export TEXTDOMAIN="minios-tools"

if [ ! -e "$1" ]; then
   echo
   echo "$(gettext "Convert .$BEXT compressed module into directory with the same name")"
   echo "$(gettext "Usage:"): $0 [source_file.sb] [[optional output_directory]]"
   echo "  $(gettext "If the output_directory is specified, it must exist")"
   echo "  $(gettext "If the output_directory is not specified, the name source_file.sb")"
   echo "  $(gettext "is used and the directory is overmounted with tmpfs")"
   exit 1
fi

if [ ! -r "$1" ]; then
   echo "$(gettext "File does not exist:"): $1" >&2
   exit 1
fi

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root."
    exit 1
fi

if [ "$2" = "" ]; then
   SOURCE="$1".x
   while [ -e "$SOURCE" ]; do SOURCE="$SOURCE"x; done
   mv "$1" "$SOURCE" || exit
   mkdir "$1"
   unsquashfs -f -dest "$1" "$SOURCE" >/dev/null || exit
   rm "$SOURCE"
else
   if [ ! -d "$2" ]; then
      echo "$(gettext "Directory does not exist:"): $2" >&2
      exit 1
   fi
   unsquashfs -f -dest "$2" "$1" >/dev/null
fi
