#!/bin/sh
#
# thinkfan.SlackBuild
# 2016-07-18
#

PRGNAM=thinkfan
VERSION=0.9.3

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i686) ARCH=i686 ;;
    i?86) ARCH=i486 ;;
    x86_64) ARCH=x86_64 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}

HOMEPAGE="http://thinkfan.sourceforge.net/"
SOURCE="${PRGNAM}-${VERSION}.tar.gz"
DOWNLOAD="http://downloads.sourceforge.net/project/thinkfan/${SOURCE}"
MD5SUM="1519f3a098a414fad6557720414b1689"

REQUIRES=""

MAINTAINER="Gernot Walzl"
EMAIL="gernot.walzl@gmx.at"

DESC="thinkfan: A simple fan control program

Works with any linux hwmon driver, especially with thinkpad_acpi.
It is designed to eat as little CPU power as possible.
The development was inspired by the excellent work people have done
on thinkwiki.org.

${HOMEPAGE}"

DOCS="$(pwd)/$0"

# EOF info


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
tar xvf $CWD/$SOURCE || exit 1

# change to the application source directory
cd $PRGNAM-$VERSION || exit 1

# fix permissions
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# set configure options
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
 elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
 else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# configure and build
rm -rf $PKG
mkdir -p $PKG/install

mv src/thinkfan.1 .

mkdir -p build
cd build
cmake -D CMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
      -D CMAKE_INSTALL_PREFIX=/usr \
      -D CMAKE_BUILD_TYPE=Release \
      -D USE_ATASMART:BOOL=ON \
      .. || exit 1
make || exit 1
make install DESTDIR=$PKG || exit 1
cd ..

mkdir -p $PKG/usr/doc
mv $PKG/usr/share/man $PKG/usr/man
mv $PKG/usr/share/doc/$PRGNAM $PKG/usr/doc/$PRGNAM-$VERSION
rmdir $PKG/usr/share/doc
rmdir $PKG/usr/share

mkdir -p $PKG/etc/rc.d
cat > $PKG/etc/rc.d/rc.thinkfan <<EOF
#!/bin/sh

NAME=thinkfan
BIN=/usr/sbin/\$NAME
CONFIG=/etc/thinkfan.conf
ARGS="-q -c \$CONFIG"
PIDFILE=/var/run/\$NAME.pid

thinkfan_start() {
  echo -n "Starting \$NAME ... "
  if [ -e "\$PIDFILE" ]; then
    echo "already running!"
  else
    \$BIN \$ARGS
    if [ \$? -ne 0 ] ; then
      echo "failed to start!"
      return 1
    else
      echo "done!"
    fi
  fi
}

thinkfan_status() {
  if [ \$(pgrep -f \$BIN) ]; then
    echo "thinkfan is running"
    if [ ! -e \$PIDFILE ]; then
      echo "Warning: Missing pid file \$PIDFILE"
    fi
    exit 0
  else
    echo "thinkfan is stopped"
    if [ -e \$PIDFILE ]; then
      echo "Detected stale pid file \$PIDFILE"
    fi
    exit 0
  fi
}

thinkfan_stop() {
  echo -n "Stopping \$NAME ... "
  if [ -e "\$PIDFILE" ]; then
    kill -TERM \$(cat \$PIDFILE) > /dev/null 2>&1
    echo "done!"
    rm -f "\$PIDFILE"
  else
    echo "not running!"
  fi
}

thinkfan_restart() {
  thinkfan_stop
  sleep 1
  thinkfan_start
}

case "\$1" in
'start')
  thinkfan_start
  ;;
'status')
  thinkfan_status
  ;;
'stop')
  thinkfan_stop
  ;;
'restart')
  thinkfan_restart
  ;;
*)
  echo "usage \$0 start|stop|restart|status"
esac
EOF
chmod +x $PKG/etc/rc.d/rc.thinkfan

mkdir -p $PKG/etc/modprobe.d
cat > $PKG/etc/modprobe.d/thinkpad_acpi.conf.new <<EOF
options thinkpad_acpi fan_control=1
EOF

# add documentation
if [ ! "$DOCS" = "" ]; then
  mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
  cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
  find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
fi

# compress man pages
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -name "*.?" -exec gzip -9 {} \;
  for i in $(find $PKG/usr/man -type l -name "*.?") ; do
    ln -s $(readlink $i).gz $i.gz
    rm $i
  done
fi

# strip binaries
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# add some description to the package
if [ ! "$DESC" = "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ ! "$REQUIRES" = "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# doinst.sh
cat > $PKG/install/doinst.sh <<EOF
#!/bin/sh

config () {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  if [ ! -r "\$OLD" ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then
    rm \$NEW
  fi
}

config etc/modprobe.d/thinkpad_acpi.conf.new
EOF

# build the package
cd $PKG
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz} \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
rm -rf $TMP/$PRGNAM-$VERSION
