#!/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:-0.6.1}

ninja -C release/
mkdir -p \
      "$DEST"/usr/share/dbus-1/interfaces/ \
      "$DEST"/usr/share/dbus-1/services/ \
      "$DEST"/usr/bin/

cp 'com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface.xml' "$DEST"/usr/share/dbus-1/interfaces/
cp 'com.emacsos.zero.ZeroPinyinService1.service' "$DEST"/usr/share/dbus-1/services/
sed -i -E 's:/home/sylecn/bin/sbin/zero-pinyin-service:/usr/bin/zero-pinyin-service:' "$DEST"/usr/share/dbus-1/services/com.emacsos.zero.ZeroPinyinService1.service
cp release/zero-pinyin-service "$DEST"/usr/bin/

fpm -f -t deb -s dir -n zero-pinyin-service -v "$VERSION" \
    -d libglib2.0-0 \
    -d libsqlite3-0 \
    -d libstdc++6 \
    -d libgcc1 \
    -d libc6 \
    -d libpcre3 \
    -d zlib1g \
    -d libselinux1 \
    -d libmount1 \
    -d libffi6 \
    -d libblkid1 \
    -d libuuid1 \
    --description "provide pinyin input engine for zero-el pinyin" \
    --vendor sylecn \
    --maintainer "Yuanle Song <sylecn@gmail.com>" \
    --deb-priority optional \
    --url "https://gitlab.emacsos.com/sylecn/zero-pinyin-service" \
    -C "$DEST" .
