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

output=$1

if [[ ! "$output" =~ ^[a-z.A-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"
    if [ -d source-files ]; then
      echo "source-files/ directory found"
    fi
  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"

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