#!/bin/bash

# scim.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping 

# Apply patches:
#
# Upstream patch to fix crashes in gtk3 apps.
zcat $CWD/scim-disable-subdir-objects.patch.gz | patch -p1 --verbose || exit 1

# Configure:
#
./bootstrap
#
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --localstatedir=/var \
   --sysconfdir=/etc \
   --disable-static \
   --with-gtk-version=2 \
   --with-qt4-im-module-dir=/usr/lib${LIBDIRSUFFIX}/qt/plugins/inputmethods \
   --program-prefix= \
   --program-suffix= \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make DESTDIR=$PKG install || failinstall

# Add a profile script that sets up the environment:
mkdir -p $PKG/etc/profile.d

cat <<EOT > $PKG/etc/profile.d/scim.sh.new
#!/bin/sh

# SCIM (Smart Common Input Method platform).  This is used to support the
# entering of text in non-US-English languages.

# For SCIM to work, you need to use a UTF-8 locale.  Make sure it ends on
# ".UTF-8", not "utf-8"!  As an example, you would need to use en_US.UTF-8
# for a US locale (export LANG=en_US.UTF-8), not en_US.
#
# The locale (LANG variable) is set in /etc/profile.d/lang.sh.

if [ -x /usr/bin/scim ]; then
  # Enable legacy X applications to use scim:
  export XMODIFIERS="@im=SCIM"
  # Let GTK applications like Firefox/Thunderbird use scim as
  # default immodule:
  export GTK_IM_MODULE="scim"
  # Enable Qt/KDE applications to use scim:
  export QT_IM_MODULE="scim"
  # Make scim start automatically if the "magic key" Ctrl-Space is pressed:
  export XIM_PROGRAM="/usr/bin/scim -d"
fi

# This ensures scim starts when you logon.
# This will only work if you login through runlevel 4 (graphical login)!!!
# Better is to have it start through Ctrl-Space like configured higher up ^^.
#if [ ! \`ls /tmp/scim-socket*\` ]; then
#  /usr/bin/scim -d
#fi

EOT

cat <<EOT > $PKG/etc/profile.d/scim.csh.new
#!/bin/csh

# SCIM (Smart Common Input Method platform).  This is used to support the
# entering of text in non-US-English languages.

# For SCIM to work, you need to use a UTF-8 locale.  Make sure it ends on
# ".UTF-8", not "utf-8"!  As an example, you would need to use en_US.UTF-8
# for a US locale (setenv LANG en_US.UTF-8), not en_US.
#
# The locale (LANG variable) is set in /etc/profile.d/lang.csh.

[ -x /usr/bin/scim ]
if (\$status == 0) then
  # Enable legacy X applications to use scim:
  setenv XMODIFIERS "@im=SCIM"
  # Let GTK applications like Firefox/Thunderbird use scim-bridge as
  # default immodule:
  setenv GTK_IM_MODULE "scim"
  ## Enable Qt/KDE applications to use scim (does not work for kde4):
  #setenv QT_IM_MODULE "scim"
  # Make scim start automatically if the "magic key" Ctrl-Space is pressed:
  setenv XIM_PROGRAM "/usr/bin/scim -d"
endif

# This ensures scim starts when you logon.
# This will only work if you login through runlevel 4 (graphical login)!!!
# Better is to have it start through Ctrl-Space like configured higher up ^^.
#[ ! \`ls /tmp/scim-socket*\` ]
#if (\$status == 0) then
#  /usr/bin/scim -d
#endif

# GTK+ environments such as XFce should support SCIM automatically, BUT
# if the first app you run is a Qt one, you'll run into problems.  This
# can be avoided by going into Menu -> Settings -> Autostarted Applications
# and adding SCIM:  /usr/bin/scim -d

# KDE will not start SCIM automatically, so you will need a script such as
# this one in your $HOME/.kde/Autostart:

#!/bin/csh
#[ -x /usr/bin/scim ]
#if (\$status == 0) then
#  /usr/bin/scim -d &
#endif

# Obviously, uncomment all but the first line.  :-)

EOT

chmod 755 $PKG/etc/profile.d/scim.sh.new
chmod 755 $PKG/etc/profile.d/scim.csh.new

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

# Protect config files from being overwritten:
mv $PKG/etc/scim/config{,.new}
mv $PKG/etc/scim/global{,.new}

# Add this to the doinst.sh
mkdir -p $PKG/install
cat <<EOINS >> $PKG/install/doinst.sh

# Handle the incoming configuration files:
config() {
  for infile in \$1; do
    NEW="\$infile"
    OLD="\`dirname \$NEW\`/\`basename \$NEW .new\`"
    # If there's no config file by that name, mv it over:
    if [ ! -r \$OLD ]; then
      mv \$NEW \$OLD
    elif [ "\`cat \$OLD | md5sum\`" = "\`cat \$NEW | md5sum\`" ]; then
      # toss the redundant copy
      rm \$NEW
    fi
    # Otherwise, we leave the .new copy for the admin to consider...
  done
}

# Prepare the new configuration files
for file in etc/scim/config.new etc/scim/global.new etc/profile.d/scim.sh.new etc/profile.d/scim.csh.new ; do
  if [ -e \$(dirname \$file)/\$(basename \$file .new) -a -x \$(dirname \$file)/\$(basename \$file .new) ]; then
    chmod 755 \$file
  else
    chmod 644 \$file
  fi
  config \$file
done

# Run gtk-query-immodules so that "scim" will appear under Imput Method
# when you right- click your mouse in a text box.
if [ -x /usr/bin/update-gtk-immodules ]; then
  /usr/bin/update-gtk-immodules
fi

EOINS

# Add documentation:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/manual/zh_CN/figures
cp -a \
  ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README THANKS TODO \
  $PKG/usr/doc/$PKGNAM-$VERSION
# Install documentation and user manual
cp -a docs/manual/zh_CN/user-manual.html \
  $PKG/usr/doc/$PKGNAM-$VERSION/manual/zh_CN/
cp -a docs/manual/zh_CN/figures/*.png \
   $PKG/usr/doc/$PKGNAM-$VERSION/manual/zh_CN/figures/


# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp -p      # run makepkg -l y -c n --prepend

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
