#!/bin/sh
set -x
set -e
print_help_and_exit() {
	echo "Usage: ./create-deb.sh
create deb for debian. requires fpm tool."
	exit 1
}

# main()
if [ "$1" = "--help" ]; then
	print_help_and_exit
fi

DEST=${DEST:-DEST}
VERSION=${VERSION:-`grep -E "^ *version: '.*'" meson.build|cut -d"'" -f2`}

if [ ! -d release/ ]; then
	meson setup --buildtype release release/
fi
ninja -C release/
mkdir -p \
      "$DEST"/usr/share/dbus-1/interfaces/ \
      "$DEST"/usr/share/dbus-1/services/ \
      "$DEST"/usr/bin/ \
      "$DEST"/usr/share/zero-panel/ \
      "$DEST"/usr/share/man/man1/ \
      "$DEST"/usr/share/doc/zero-panel/

cp 'com.emacsos.zero.Panel1.PanelInterface.xml' "$DEST"/usr/share/dbus-1/interfaces/
cp 'com.emacsos.zero.Panel1.service' "$DEST"/usr/share/dbus-1/services/
sed -i -E 's:/home/sylecn/d/zero-panel:/usr/bin/zero-panel:' "$DEST"/usr/share/dbus-1/services/com.emacsos.zero.Panel1.service
cp release/zero-panel "$DEST"/usr/bin/
rsync -air themes/ "$DEST"/usr/share/zero-panel/
cp zero-panel.1 "$DEST"/usr/share/man/man1/
cp README* "$DEST"/usr/share/doc/zero-panel/

fpm -f -t deb -s dir -n zero-panel -v "$VERSION" \
    -d libgtk-3-0 \
    -d libglib2.0-0 \
    --deb-recommends fonts-noto-cjk \
    --description "panel used to show candidates for zero-el" \
    --vendor sylecn \
    --maintainer "Yuanle Song <sylecn@gmail.com>" \
    --deb-priority optional \
    --url "https://gitlab.emacsos.com/sylecn/zero-panel" \
    --license Apache-2.0 \
    -C "$DEST" .
