#!/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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac




# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping

# Configure:
LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --localstatedir=/var \
   --sysconfdir=/etc \
   --disable-static \
   --program-prefix="" \
   --program-suffix="" \
   --build=$ARCH-slackware-linux || failconfig

# Build:
make $NUMJOBS || 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"
  # 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

if [ -x /usr/bin/scim-bridge ]; then
  # Let GTK applications like Firefox/Thunderbird use scim-bridge as
  # default immodule:
  export GTK_IM_MODULE="scim-bridge"
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

# 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/bash
#if [ -x /usr/bin/scim ]; then
#  /usr/bin/scim -d &
#fi

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

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"
  # Enable Qt/KDE applications to use scim:
  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
[ -x /usr/bin/scim-bridge ]
if (\$status == 0) then
  # Let GTK applications like Firefox/Thunderbird use scim-bridge as
  # default immodule:
  setenv GTK_IM_MODULE "scim-bridge"
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

# Setup the menu entries and KDE autostart:
#KDEPREF=$(kde-config --prefix)
#if [ -n "$KDEPREF" ]; then
#  mkdir -p $PKG$KDEPREF/share/autostart
#  cat $CWD/scim.desktop > $PKG$KDEPREF/share/autostart/scim.desktop
#fi
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}
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/gtk-query-immodules-2.0 ]; then
  mkdir -p etc/gtk-2.0
  chroot . usr/bin/gtk-query-immodules-2.0 > etc/gtk-2.0/gtk.immodules
fi

EOINS

# Add documentation:
DOCS="ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README THANKS TODO"
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/manual/zh_CN/figures
cp -a $DOCS $PKG/usr/doc/$PKGNAM-$VERSION || true
# 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/
chmod -R a-w $PKG/usr/doc/$PKGNAM-$VERSION/*

# 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
