#!/bin/bash
#
# garnewpkg
#
# Create a new gar package directory
#
#

VERSION=1.0.0
DATE=20201203

set -e

[[ "$GARDIR" ]] || GARDIR=/usr/gar

display_usage () {
   echo
   echo "garnewpkg $VERSION ($DATE)"
   echo
   echo "Usage: $(basename $0) <category>/<name> <version> [source_url]"
   echo
   echo "$(basename $0) creates a new package directory <name> inside the"
   echo "category directory <category> under the GAR directory pointed to"
   echo "by the GARDIR environment variable (default: /usr/gar)."
   echo
   echo "If <category> does not exist this program will create it."
   echo
   echo "Examples:"
   echo
   echo "$(basename $0) devel/gcc"
   echo "$(basename $0) libs/compression/lz4"
   echo
}



#######################################################
#                 Options processing                  #
#######################################################

#while getopts ":Vkp" opt; do
#  case $opt in
#    V)
#      echo "$(basename $0) $VERSION, $DATE"
#      exit 0
#      ;;
#    k)
#      catsearch=1
#      ;;
#    p)
#      fullpath=1
#      ;;
#    \?)
#      echo "$(basename $0): Invalid option: -$OPTARG" >&2
#      display_usage
#      exit 1
#      ;;
#    :)
#      echo "$(basename $0): Option -$OPTARG requires an argument" >&2
#      exit 1
#      ;;
#  esac
#done
#
#shift $(($OPTIND-1))

##### Positional arguments processing #####

if [[ -z "$2" ]]; then
   display_usage
   echo "*** Error: <version> must be specified." >&2
   echo
   exit 111
fi

cat_pkgname="$1"
pkgversion="$2"
pkgsource="$3"

if ! echo "$cat_pkgname" | grep -q "/"; then
   display_usage
   echo "*** Error: Both <category> and <name> must be specified." >&2
   echo
   exit 111
fi

# Canonicalize the category path.
pkgcat=$(dirname "$1" | sed -r 's|/+|/|g; s|^/||g; s|/$||g')

pkgname=$(basename "$1")
pkgversion="$2"
pkgurl=$(echo "$3" | sed "s|${pkgversion}|\$\(GARVERSION\)|g")
pkgurl_base=$(dirname "$pkgurl")
pkgurl_file=$(basename "$pkgurl")



###########################################

main () {
   catdir="${GARDIR}/${pkgcat}"
   pkgdir="${catdir}/${pkgname}"

   if [[ -d "$pkgdir" ]]; then
      echo
      echo "*** Error: '$pkgdir' exists already." >&2
      echo
      exit 1
   fi

   # Create the category dir if necessary

   if ! [[ -d $catdir ]]; then
      mkdir -pv "$catdir"
      # The level.mk and Makefile symlinks should be
      # relative to the catdir and point to GARDIR
      ln -sv $(echo "$pkgcat" | sed -r "s|[^/]+|..|g")/category.mk "${catdir}/Makefile"
      ln -sv $(echo "$pkgcat" | sed -r "s|[^/]+|..|g")/level.mk "${catdir}/level.mk"
   fi

   # Build the package dir's Makefile and config.mk files

   mkdir -pv "${pkgdir}"
   echo "Creating '${pkgdir}/Makefile'..." 
   sed -r "s|GARNAME =.*$|GARNAME = ${pkgname}|g;
           s|CATEGORIES =.*$|CATEGORIES += ${pkgcat}|g;
           s|MASTER_SITES \+=.*$|MASTER_SITES += ${pkgurl_base}/|g;
           s|DISTFILES =.*$|DISTFILES = ${pkgurl_file}|g;
           s|GARVERSION =.*$|GARVERSION = ${pkgversion}|g" "${GARDIR}/pkg-template.mk" > "${pkgdir}/Makefile"

   echo "Creating '${pkgdir}/config.mk'..." 
   sed "s/options for xxxxx/options for ${pkgname}/g" "${GARDIR}/config-template.mk" > "${pkgdir}/config.mk"

   echo "Creating '${pkgdir}/manifest'..."
   > "${pkgdir}/manifest"
}

main
