#!/bin/bash
#
#  eagle.SlackBuild 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 2 of the License, or any later version.
#
#  eagle.SlackBuild 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, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#

# default settings
PACKAGE="eagle"
PACKAGE_EXT="gz"
VERSION=${VERSION:=7.7.0}
BUILD=${BUILD:=1}

if [ -f ~/.slackbuildrc ]; then
  . ~/.slackbuildrc
elif [ -f ../../runtime/slackbuildrc ]; then
  . ../../runtime/slackbuildrc
fi

check_required

SRC="eagle-lin64-${VERSION}.run"
URL="http://web.cadsoft.de/ftp/eagle/program/$(echo $VERSION | cut -d '.' -f1-2)/eagle-lin64-${VERSION}.run"

get_source $SRC $URL


# Documentation is distributed for English, German and Chinese.
# Due to the size of the documentation, only include one language.
DOC_LANG=${DOC_LANG:-en} # One of "all", "de", "en", or "zh".

sh $CWD/$SRC $PKG/opt
chown -R root:root $PKG
chmod -R u+w,go-w,a+rX-st $PKG
find $PKG/opt/$PACKAGE-$VERSION/lbr/seeed -type f -exec chmod -x {} +
find $PKG/opt/$PACKAGE-$VERSION/projects/examples/seeed -type f -exec chmod -x {} +

# Create the license key file.  EAGLE will write to this the first time it is
# run.  After that, you should change the permissions to 0644.
touch $PKG/opt/$PACKAGE-$VERSION/bin/$PACKAGE.key
chmod 0666 $PKG/opt/$PACKAGE-$VERSION/bin/$PACKAGE.key

mkdir -p $PKG/usr/bin
ln -s /opt/$PACKAGE-$VERSION/bin/$PACKAGE $PKG/usr/bin/eagle

mkdir -p $PKG/usr/share/icons/hicolor/16x16/apps
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
ln -s /opt/$PACKAGE-$VERSION/bin/eagleicon16.png $PKG/usr/share/icons/hicolor/16x16/apps/$PACKAGE.png
ln -s /opt/$PACKAGE-$VERSION/bin/eagleicon50.png $PKG/usr/share/icons/hicolor/48x48/apps/$PACKAGE.png

mkdir -p $PKG/usr/share/applications
cat $CWD/$PACKAGE.desktop > $PKG/usr/share/applications/$PACKAGE.desktop

# Move any man pages to the proper location.
mkdir -p $PKG/usr/man/man1
mv $PKG/opt/$PACKAGE-$VERSION/doc/*.1 $PKG/usr/man/man1
#find $PKG/usr/man -type f -exec gzip -9 {} +


mkdir -p $DOCS
if [ ${DOC_LANG} != "all" ]; then
  # Move each language-specific file to its own subdir.
  cd $PKG/opt/$PACKAGE-$VERSION/doc
  for lang in de en zh; do mkdir -p ${lang}/ulp; done
  for lang in de en;    do mv README_${lang} ${lang}/README; done
  for lang in de en;    do mv UPDATE_${lang} ${lang}/UPDATE; done
  for lang in de en;    do mv library_${lang}.txt ${lang}/library.txt; done
  for lang in de en zh; do mv manual_${lang}.pdf ${lang}/manual.pdf; done
  for lang in de en zh; do mv tutorial_${lang}.pdf ${lang}/tutorial.pdf; done
  for lang in de en;    do mv ulp/connect-device-split-symbol-${lang}.pdf ${lang}/ulp/connect-device-split-symbol.pdf; done
  for lang in de en;    do mv ulp/generate-3d-idf-data_${lang}.pdf ${lang}/ulp/generate-3d-idf-data.pdf; done
  for lang in de en;    do mv ulp/make-symbol-device-package-bsdl-2011-${lang}.pdf ${lang}/ulp/make-symbol-device-package-bsdl-2011.pdf; done

  # Prefer the English docs as a base set.
  cd $PKG/opt/$PACKAGE-$VERSION/doc/en
  find . -type f -exec mv {} ../{} \;

  # Replace the base set with other languages.
  cd $PKG/opt/$PACKAGE-$VERSION/doc/${DOC_LANG}
  find . -type f -exec mv {} ../{} \;

  # Delete all alternate language files.
  cd $PKG/opt/$PACKAGE-$VERSION/doc
  for lang in de en zh; do rm -rf ${lang}; done
fi

mv $PKG/opt/$PACKAGE-$VERSION/doc/* $DOCS
ln -s /usr/doc/$PACKAGE-$VERSION $PKG/opt/$PACKAGE-$VERSION/doc


fixup_package $PKG

# Clean up the extra stuff:
set +u
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PACKAGE-$VERSION
  rm -rf $PKG
fi