APP=syslinux
VERSION=6.03
BUILD=1sml
HOMEPAGE="https://wiki.syslinux.org/wiki/index.php?title=The_Syslinux_Project"
DESC="boot loader for the linux operating system which boots off of MS-DOS Windows FAT filesystem"
REQUIRES="musl"

build() {
  compileonlyfor x86_64
  mkandenterbuilddir 
  rm -rf $APP-$VERSION

  tar xf $SRCDIR/$APP-$VERSION.tar.?z*
  cd $APP-$VERSION
  fixbuilddirpermissions

  patch -p1 < $SRCDIR/syslinux6-ext4_fix_64bit_feature.patch

  # We don't use sbin
  sed -i 's,/sbin,/bin,' syslinux.spec mk/syslinux.mk

  # Exclude windows stuff and mtools and others
  sed 's|INSTALLSUBDIRS = com32 utils dosutil|INSTALLSUBDIRS = com32 utils|g' -i Makefile
  sed '/DIAGDIR/d' -i Makefile

  # Windows stuff not needed. 
  sed 's|diag libinstaller dos win32 win64 dosutil txt|libinstaller txt|g' -i Makefile
  sed 's|win32/syslinux.exe win64/syslinux64.exe||g' -i Makefile
  sed 's|dosutil/\*.com dosutil/\*.sys||g' -i Makefile
  sed 's|dos/syslinux.com||g' -i Makefile
  sed 's|gpxe/gpxelinuxk*\.0||g' -i Makefile

  # We dont want the perl-based utils
  # They also pull in the isohybrid mbrs
  sed "s|utils/[a-z]*||g" -i Makefile

  # rarely used COM32 Modules
  sed "s,com32/modules/\*.c32,," -i Makefile
  sed "s,com32/hdt/\*.c32,," -i Makefile
  sed "s,com32/rosh/\*.c32,," -i Makefile
  sed "s,com32/gfxboot/\*.c32,," -i Makefile
  sed "s,com32/sysdump/\*.c32,," -i Makefile
  sed "s,com32/lua/src/\*.c32,," -i Makefile
  sed "s,com32/gpllib/\*.c32,," -i Makefile
  sed "s,com32/cmenu/libmenu/\*.c32,," -i Makefile

  # Exclude perl utils from being installed
  sed '/DIRS/ s/utils//' -i Makefile

  sed -i 's,#include <getkey.h>,#include "include/getkey.h",' com32/libutil/keyname.c
  sed -i 's,#include <libutil.h>,#include "include/libutil.h",' com32/libutil/keyname.c
  sed -i 's,#include "sha1.h",#include "include/sha1.h",' com32/libutil/sha1hash.c
  sed -i 's,#include <base64.h>,#include "include/base64.h",' com32/libutil/unbase64.c
  sed -i 's,#include <md5.h>,#include "include/md5.h",' com32/libutil/md5.c
  sed -i 's,#include <md5.h>,#include "include/md5.h",' com32/libutil/crypt-md5.c
  sed -i 's,#include <minmax.h>,#include "include/minmax.h",' com32/libutil/sha256crypt.c
  sed -i 's,#include "xcrypt.h",#include "include/xcrypt.h",' com32/libutil/sha256crypt.c
  sed -i 's,#include <minmax.h>,#include "include/minmax.h",' com32/libutil/sha512crypt.c
  sed -i 's,#include "xcrypt.h",#include "include/xcrypt.h",' com32/libutil/sha512crypt.c
  sed -i 's,#include <base64.h>,#include "include/base64.h",' com32/libutil/base64.c
  sed -i '/herror.*/d' utils/gethostip.c
  sed -ri 's,__uint([0-9]+)_t,uint\1_t,g' efi/wrapper.c
  sed -ri 's,__uint([0-9]+)_t,uint\1_t,g' efi/wrapper.h

  PREFIXED_PATHS="BINDIR='/bin' LIBDIR='/lib' DATADIR='/share' MANDIR='/share/man' INCDIR='/include'"

  make CC="gcc -I/include -include sys/sysmacros.h" $PREFIXED_PATHS AUXDIR="/lib/syslinux/bios" INSTALLROOT="$PKG" -j1 bios installer
  make $PREFIXED_PATHS AUXDIR="/lib/syslinux/bios" INSTALLROOT="$PKG" -j1 bios install

  make CC="gcc -I/include -include sys/sysmacros.h" $PREFIXED_PATHS AUXDIR="/lib/syslinux/bios" INSTALLROOT="$PKG" efi64 installer
  make CC="gcc -I/include -include sys/sysmacros.h" $PREFIXED_PATHS AUXDIR="/lib/syslinux/bios" INSTALLROOT="$PKG" -j1 efi64 install 

  # copy the smlinux bootloader configuration
  install -Dm 0644 $SRCDIR/extlinux.conf.smlinux $PKG/boot/extlinux.conf.sample

  cp COPYING $PKGDOCS/LICENSE

  mkfinalpkg
}

SHA512SUMS="
5740536857c7789304ff00fc568fdce18ed3b2e09cf3623f08df7f46144ccc87248b618934d9fa092b99ceab054698b3e0250fd70a664b631da28bf3612b8160  syslinux-6.03.tar.lz
1ecba0810415963707099143de74ea5c9e41e38b200da2de57ceee29914b1eaeebc58b1cf14738f0b7bd4d37695a8490adb8a9f1b4f35ff4ad1f34c816db54da  syslinux6-ext4_fix_64bit_feature.patch
"
