#!/bin/bash
if [ $# -eq 0 ]
then
  echo "No output package name given!"
  exit 1
fi

output=$1

if [[ ! "$output" =~ ^[a-zA-Z0-9_-]{1,}$ ]]; then
  echo "Invalid output name! The name must only contain letters, numbers, hyphens or underscores!"
  exit 1
fi

type="binary"

echo "Creating package with the name $output..."

if [ -d files ]; then
  echo "files/ directory found"
else
  if [ -f source.sh ]; then
    type="source"
    echo "source.sh file found"
  else
    echo "files/ directory or source.sh file not found in $PWD"
    exit 1
  fi
fi

if [ -f pkg.info ]; then
  echo "pkg.info file found"
else
  echo "pkg.info file not found in $PWD"
  exit 1
fi

echo "Creating $type package as $output.bpm"

if [[ "$type" == "binary" ]]; then
  tar -czf "$output".bpm files/ pkg.info
else
  tar -czf "$output".bpm source.sh pkg.info
fi
