#!/bin/dash
# Convert a directory package to SFS
# Copyright (C) James Budiono 2017
# License: GNU GPL Version 3 or later
#

### configuration
COMPRESSION=${COMPRESSION:--comp xz}
APPTITLE="dir2sfs"

# $1-msg
msg() {
	if [ "$DISPLAY" ]; then
		Xdialog --title "$APPTITLE" --msgbox "$1" 0 0
	else
		dialog --title "$APPTITLE" --msgbox "$1" 0 0
	fi
}

# $1-msg
die() {
	msg "$1"
	exit 1
}

### main
# 1. sanity checks
[ -z "$1" ] && usage
SRC="$1"
TARGET=${SRC%.t?z}.sfs
! [ -e "$SRC" ] && die "$1 does not exist."
! [ -r "$SRC" ] && die "$1 is not readable."
[ -L "$SRC" ] && SRC=$(readlink -f "$1")
[ -e "$TARGET" ] && die "$TARGET already exist."

# 2. do the work
mksquashfs $SRC $TARGET $COMPRESSION
msg "Done. Created $TARGET."

