#!/bin/sh
#
dir="$1"

errexit () {
    echo "$1"
    exit 1
}

if [ -z "$dir" ]; then
    errexit "Usage: $0 <directory>"
fi

if [ ! -d "$dir" ]; then
    errexit "Directory $dir not found"
fi

case "$dir" in
    /*) errexit "Directory $dir should not be an absolute path" ;;
esac

dir=${dir%/}

echo "Creating mkvsix package for $dir" ...
backup="$dir.tar"
echo -n "backup to $backup ... "
tar cf "$backup" "$dir" || errexit "Failed to create backup"
echo "done"
echo -n "renaming dir to 'extension' ... "
mv "$dir" extension || errexit "Failed to rename dir"
echo "done"
echo -n "creating vsix package ... "
zip -r "$dir.vsix" extension || errexit "Failed to create vsix package"
echo "done"
echo -n "restoring backup ... "
mv extension "$dir" || errexit "Failed to restore backup"
echo "done"
rm -f "$backup"



