#!/bin/bash
#  Copyright (C) 2010-2012  Matias A. Fonzo, <selk@dragora.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

CWD=$(pwd)

TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

P=heybuddy
V=${V:-0.2.3}
ARCH=${ARCH:-i486}
B=${B:-1}

PKG=${TMP}/package-${P}

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/${P}-${V}
tar -xvf ${CWD}/${P}-${V}.tar.lz -C $TMP

cd ${TMP}/${P}-${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
 \( -perm 2777 -o \
    -perm 777  -o \
    -perm 775  -o \
    -perm 711  -o \
    -perm 555  -o \
    -perm 511     \
 \) -exec chmod 755 {} + \
  -o \
 \( -perm 666 -o \
    -perm 664 -o \
    -perm 600 -o \
    -perm 444 -o \
    -perm 440 -o \
    -perm 400    \
 \) -exec chmod 644 {} +

# Make the package skel:
install -d ${PKG}/usr/bin ${PKG}/usr/share/${P}

# Install the files:
find . -exec cp -a "{}" ${PKG}/usr/share/${P} \;
# Just in case:
chmod 755 ${PKG}/usr/share/${P}/heybuddy.py

# Make symlink in usr/bin:
( cd ${PKG}/usr/bin
  ln -s /usr/share/${P}/heybuddy.py heybuddy
)

# Install entry for the desktop:
install -d ${PKG}/usr/share/applications
zcat ${CWD}/heybuddy.desktop.gz \
 > ${PKG}/usr/share/applications/heybuddy.desktop

# Install icons:
install -m 644 assets/icon.png \
 -D ${PKG}/usr/share/pixmaps/heybuddy.png

install -m 644 assets/icon14.png -D \
 ${PKG}/usr/share/icons/hicolor/14x14/apps/heybuddy.png

install -m 644 assets/icon.svg -D \
 ${PKG}/usr/share/icons/hicolor/scalable/apps/heybuddy.svg

# Add the documentation:
mkdir -p ${PKG}/usr/doc/${P}-${V}
cp -a \
 ChangeLog README.txt gpl-3.0.txt \
 ${PKG}/usr/doc/${P}-${V}

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/?? ${PKG}/description

cd $PKG
makepkg -l ${OUT}/${P}-${V}-${ARCH}-${B}.tlz

