#!/bin/bash

if test "$1" = ""; then
	echo "  update_po release archpkg test_"
	exit
fi

cd "`dirname "$0"`"

update_po() {
	cd po
	intltool-update -p
	cd ../dist/doc/po
	make update-po
}

archpkg() {
VERSION=$(head -n 1 debian/changelog | cut -d '(' -f 2 | cut -d ')' -f 1)

cat << eof > PKGBUILD
pkgname=gnome-encfs-manager
pkgver=${VERSION}
pkgrel=0
pkgdesc="An easy to use manager and mounter for encfs stashes"
arch=('i686' 'x86_64')
license=('GPL')
depends=('gtk3' 'libgee06' 'libgnome-keyring' 'encfs' 'gnome-icon-theme' 'libsm' 'libice')
makedepends=('vala' 'intltool' 'gettext')
install=PKGBUILD.install
url="https://launchpad.net/gencfsm"

build() {
  cd "\${startdir}"

  ./autogen.sh
  ./configure --prefix=/usr --disable-appindicator
  make
}

package() {
  cd "\${startdir}"

  make DESTDIR="\${pkgdir}" install
}
eof

cat << eof > PKGBUILD.install
pkgname=gnome-encfs-manager

post_install() {
  glib-compile-schemas /usr/share/glib-2.0/schemas
  for icons in hicolor ubuntu-mono-light ubuntu-mono-dark Mint-X; do
    if [ -e "/usr/share/icons/\$icons/index.theme" ]; then
      gtk-update-icon-cache /usr/share/icons/\$icons 2>/dev/null
    fi
  done
}

post_upgrade() {
  post_install \$1
}
eof

makepkg
}

release_PKGBUILD() {
sha256=$(sha256sum "gnome-encfs-manager_${VERSION}.tar.xz" | cut -d ' ' -f 1)

cat << eof > PKGBUILD
pkgname=gnome-encfs-manager
pkgver=${VERSION}
pkgrel=0
pkgdesc="An easy to use manager and mounter for encfs stashes"
arch=('i686' 'x86_64')
license=('GPL')
depends=('gtk3' 'libgee06' 'libsecret-1' 'encfs' 'gnome-icon-theme' 'libsm' 'libice')
makedepends=('vala' 'intltool' 'gettext')
install=PKGBUILD.install
url="https://launchpad.net/gencfsm"
source=(\${pkgname}_\${pkgver}.tar.xz)
sha256sums=('${sha256}')

build() {
  cd "\${srcdir}/\${pkgname}-\${pkgver}"

  ./autogen.sh
  ./configure --prefix=/usr
  make
}

package() {
  cd "\${srcdir}/\${pkgname}-\${pkgver}"

  make DESTDIR="\$pkgdir" install
}
eof

cat << eof > PKGBUILD.install
post_install() {
  glib-compile-schemas /usr/share/glib-2.0/schemas
  for icons in hicolor ubuntu-mono-light ubuntu-mono-dark Mint-X; do
    if [ -e "/usr/share/icons/\$icons/index.theme" ]; then
      gtk-update-icon-cache /usr/share/icons/\$icons 2>/dev/null
    fi
  done
}

post_upgrade() {
  post_install \$1
}
eof

}

release_SPEC() {

cat << eof > gnome-encfs-manager.spec
Name: gnome-encfs-manager
Version: ${VERSION}
Release: 0
License: GPL-2+
Source: %{name}_%{version}.tar.xz
URL: http://www.libertyzero.com/GEncfsM
Group: System/GUI/GNOME
Summary: An easy to use manager and mounter for encfs stashes

Requires: encfs

BuildRequires: intltool gettext libtool vala
BuildRequires: pkgconfig(gtk+-3.0)
BuildRequires: pkgconfig(gee-1.0)
BuildRequires: pkgconfig(gmodule-2.0)
BuildRequires: pkgconfig(gio-2.0)
BuildRequires: pkgconfig(libsecret-1)
BuildRequires: pkgconfig(sm)
BuildRequires: pkgconfig(ice)
BuildRequires: pkgconfig(appindicator3-0.1)

%description
The Gnome Encfs Manager is a manager and mounter for encfs stashes. It features:
 
  * an easy and quick to use tray icon inspired by Cryptkeeper (AppIndicator is supported)
  * Gnome keyring support
  * per-stash configuration for all features
  * automatic mounting and unmounting, when a stash gets available / unavailable
    (e.g. when you insert a usb drive containing a configured stash)
  * an intuitive interface to create, configure and import stashes
  * automount-at-startup support

%prep
%setup

%build
./autogen.sh
./configure --prefix=/usr
make

%install
make DESTDIR=%{buildroot} install

%post
for icons in hicolor ubuntu-mono-light ubuntu-mono-dark Mint-X; do
  /bin/touch --no-create /usr/share/icons/\$icons &> /dev/null || :
done

%postun
if [ \$1 -eq 0 ]; then
  /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas &> /dev/null || :
  for icons in hicolor ubuntu-mono-light ubuntu-mono-dark Mint-X; do
    /bin/touch --no-create /usr/share/icons/\$icons &> /dev/null || :
    gtk-update-icon-cache /usr/share/icons/\$icons &> /dev/null || :
  done
fi

%posttrans
/usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas &> /dev/null || :
for icons in hicolor ubuntu-mono-light ubuntu-mono-dark Mint-X; do
  gtk-update-icon-cache /usr/share/icons/\$icons &> /dev/null || :
done

%files
%defattr(-,root,root,-)
/usr/bin/gnome-encfs-manager
/usr/share/applications/gnome-encfs-manager.desktop
/usr/share/glib-2.0/schemas/com.libertyzero.gnome-encfs-manager.gschema.xml
/usr/share/dbus-1/services/com.libertyzero.gnome-encfs-manager.service
/usr/share/gnome-encfs-manager

$(
cd gnome-encfs-manager-${VERSION}

for folder in `find dist/icons -type d`; do
	echo $folder | sed -e 's/dist/%dir\ \/usr\/share/'
done
echo
for icon in `find dist/icons \( -name '*.svg' -o -name '*.png' \)`; do
	echo "$icon" | sed -e 's/dist/\/usr\/share/'
done
echo
for po in `find po -name *.po`; do
	lang=$(basename $po .po)
	echo /usr/share/locale/$lang/LC_MESSAGES/gnome-encfs-manager.mo
done

cd ..
)

eof

}

release() {
	VERSION=$(head -n 1 debian/changelog | cut -d '(' -f 2 | cut -d ')' -f 1)
	if test -d "../gnome-encfs-manager-${VERSION}"; then
		rm -rf "../gnome-encfs-manager-${VERSION}"
	fi
	mkdir "../gnome-encfs-manager-${VERSION}/"
	bzr export "../gnome-encfs-manager-${VERSION}/gnome-encfs-manager-${VERSION}"
	cd "../gnome-encfs-manager-${VERSION}/"
	dpkg-source -b gnome-encfs-manager-${VERSION}
	release_PKGBUILD
	release_SPEC
	gpg --armor --sign --detach-sig "gnome-encfs-manager_${VERSION}.tar.xz"
	rm -r "gnome-encfs-manager-${VERSION}"
}

exec_() {
echo $@
$@
}

test_() {
	i="100"
	while [ "$i" -gt "0" ]; do
		echo " => $i"
		#exec_ gnome-encfs-manager wait
		exec_ gnome-encfs-manager mount :/Crypt
		#exec_ gnome-encfs-manager wait
		exec_ gnome-encfs-manager unmount :/Crypt
		i=$(($i - 1))
	done
}

$1
