#!/bin/sh
## src2pg script for: 	crafty for /usr
## Thanks to <theoffset> for help with the flags and wrapper
##
##### ------------Standard Package Variables-------------------
# Most source code only needs these  4 variables set.
# Set  SRC_SUFFIX  to ".tar.gz" ".tgz" ".tar.bz2" or ".tbz"

BUILD="5"
NAME="crafty"
VERSION="20.1afx"
SRC_SUFFIX=".tar.bz2"

#####--------Common Overrides and Options----------------------
PRE_FIX="/usr"
# EXTRA_CONFIGS=""

EXTRA_FLAGS=" -Wall -pipe -D_REENTRANT -fbranch-probabilities \
-fforce-mem -fomit-frame-pointer -fno-gcse -mpreferred-stack-boundary=2"

#######----------------Processing------------------------------
# Get functions and read in configuration files
source /usr/libexec/src2pkg/FUNCTIONS ;

pre_process ;
find_source ;
make_dirs ;
unpack_source ;
fix_source_perms ;

# configure_source ;
# compile_source ;
# fake_install ;

echo $BLUE"Now compiling..."$NORMAL

cd $SRC_DIR ;

#Make this thing.
   make \
     target=LINUX \
     CC="gcc" \
     CXX="g++" \
     CFLAGS="$STD_FLAGS $EXTRA_FLAGS" \
     CXFLAGS="$STD_FLAGS $EXTRA_FLAGS" \
     LDFLAGS="-lstdc++" \
     opt="-DFUTILITY  -DFAST  -DINLINE_ASM  -DEPD  -DBOOKDIR=\\\"${PRE_FIX}/share/crafty/books\\\"  -DTBDIR=\\\"${PRE_FIX}/share/crafty/TB\\\"" \
	crafty-make 2> /dev/null

mkdir -p $PKG_DIR$PRE_FIX/bin
cp -a $SRC_DIR/crafty $PKG_DIR$PRE_FIX/bin/crafty-bin
chown root.root $PKG_DIR$PRE_FIX/bin/crafty-bin
chmod 755 $PKG_DIR$PRE_FIX/bin/crafty-bin

# install the common files
mkdir -p $PKG_DIR$PRE_FIX/share/crafty/books
cp -a $SRC_DIR/common/*.bin $PKG_DIR$PRE_FIX/share/crafty/books
cp -a $SRC_DIR/common/bitmaps $PKG_DIR$PRE_FIX/share/crafty
cp -a $SRC_DIR/common/sound $PKG_DIR$PRE_FIX/share/crafty

# create the TB directory, just in case
mkdir -p $PKG_DIR$PRE_FIX/share/crafty/TB

mkdir -p $PKG_DIR$PRE_FIX/man/man1
cp -a $SRC_DIR/doc/crafty.man $PKG_DIR$PRE_FIX/man/man1/crafty.1

mkdir -p $PKG_DIR$PRE_FIX/doc/crafty-20.1
cp $SRC_DIR/doc/crafty.doc.ascii $PKG_DIR$PRE_FIX/doc/crafty-20.1/crafty.txt
cp $SRC_DIR/doc/crafty.doc.ps $PKG_DIR$PRE_FIX/doc/crafty-20.1/crafty.ps
cp $SRC_DIR/doc/crafty.hlp $PKG_DIR$PRE_FIX/doc/crafty-20.1/crafty.hlp
cp $SRC_DIR/doc/tournament.howto $PKG_DIR$PRE_FIX/doc/crafty-20.1

# Not really needed, but install an example rc file -copy to ~/.craftyrc /.craftyrc for use.
cp -a $SRC_DIR/doc/craftyrc.example $PKG_DIR$PRE_FIX/doc/crafty-20.1
cp $CWD/README.Amigo $PKG_DIR$PRE_FIX/doc/crafty-20.1

# Adjust wrapper scripts if you don't use PRE_FIX="/usr"
# Important! Install this wrapper so game logs are in your $HOME
# cp $CWD/Resources/crafty.wrapper $PKG_DIR$PRE_FIX/bin/crafty
# chown root.root $PKG_DIR$PRE_FIX/bin/crafty
# chmod 755 $PKG_DIR$PRE_FIX/bin/crafty


cat > $PKG_DIR$PRE_FIX/bin/crafty << __EOF__
#!/bin/bash

if [[ ! -e \$HOME/.crafty ]]; then
 mkdir -p \$HOME/.crafty/log
else
 if [[ -e \$HOME/.crafty/books ]]; then
  export CRAFTY_BOOK_PATH=\$HOME/.crafty/books
 fi
 if [[ -e \$HOME/.crafty/TB ]]; then
  export CRAFTY_TB_PATH=\$HOME/.crafty/TB
 fi
fi

export CRAFTY_LOG_PATH=\$HOME/.crafty/log
export CRAFTY_RC_PATH=\$HOME/.crafty
#Run the real crafty here
/usr/bin/crafty-bin

__EOF__
chmod 755 $PKG_DIR$PRE_FIX/bin/crafty

# This wrapper is optional for use with xboard
cp $CWD/Resources/xboard-crafty.wrapper $PKG_DIR$PRE_FIX/bin/xboard-crafty
chown root.root $PKG_DIR$PRE_FIX/bin/xboard-crafty
chmod 755 $PKG_DIR$PRE_FIX/bin/xboard-crafty

fix_pkg_perms ;
strip_bins ;
# create_docs ;
compress_man_pages ;
make_description ;
make_doinst ;
make_package ;
post_process ;
exit 0

## See the Amigo PkgBuild documentation for help and examples.