#Packager: Richard Lapointe <richard@laprjns.com>
#Former Packager(s): Name <email@address.com>
#Anything commented out is optional and can be deleted.

pkgname=refind
pkgver=0.10.3
pkgrel=2rl
#arch=noarch
source=(http://sourceforge.net/projects/refind/files/$pkgver/$pkgname-bin-$pkgver.zip \
		refind.conf-salix \
		os_salix-xfce-14.2.png \
		os_salix-mate-14.2.png \
		os_slackel.png \
		salix-wall.png )
		
sourcetemplate=http://people.salixos.org/laprjns/$pkgname/$pkgver/
docs=("readme" "license" "copying" "changelog" "authors" "news" "todo")
url=http://www.rodsbooks.com/refind/
#dotnew=()
#CFLAGS=
#CXXFLAGS=
#options=('noextract')

doinst() {
# following code was extracted from refind-install.sh with some modifications
# If this fails, sets Problems=1
   InstallDir="/boot/efi"
   TargetDir="/EFI/refind"
   Refind="refind_x64.efi"
   Efibootmgr=`which efibootmgr 2> /dev/null`
   if [[ "$Efibootmgr" ]] ; then
      InstallDisk=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
      PartNum=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
      EntryFilename="$TargetDir/$Refind"
      EfiEntryFilename=`echo ${EntryFilename//\//\\\}`
      EfiEntryFilename2=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
      ExistingEntry=`"$Efibootmgr" -v | grep -i "$EfiEntryFilename2"`

      if [[ "$ExistingEntry" ]] ; then
         ExistingEntryBootNum=`echo "$ExistingEntry" | cut -c 5-8`
         FirstBoot=`"$Efibootmgr" | grep BootOrder | cut -c 12-15`
         if [[ "$ExistingEntryBootNum" != "$FirstBoot" ]] ; then
            echo "An existing rEFInd boot entry exists, but isn't set as the default boot"
            echo "manager. The boot order is being adjusted to make rEFInd the default boot"loc
            echo "manager. If this is NOT what you want, you should use guef or efibootmgr to"
            echo "manually adjust your EFI's boot order."
         fi
         "$Efibootmgr" -b $ExistingEntryBootNum -B &> /dev/null
      fi

      echo "Installing it!"
      if [[ "$KeepName" == 0 ]] ; then
         "$Efibootmgr" -c -l "$EfiEntryFilename" -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev/null
      else
         "$Efibootmgr" -c -l "$EfiEntryFilename" -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum \
                       -u "$TargetShim $TargetX64" &> /dev/null
      fi
      if [[ $? != 0 ]] ; then
         EfibootmgrProblems=1
         Problems=1
      fi

   else # efibootmgr not found
      EfibootmgrProblems=1
      Problems=1
   fi

   if [[ $EfibootmgrProblems ]] ; then
      echo
      echo "ALERT: There were problems running the efibootmgr program! You may need to"
      echo "rename the $Refind binary to the default name (EFI/BOOT/bootx64.efi"
      echo "on x86-64 systems, EFI/BOOT/bootia32.efi on x86 systems, or"
      echo "EFI/BOOT/bootaa64.efi on ARM64 systems) to have it run!"
      echo
   else
      echo "rEFInd has been set as the default boot manager."
   fi 
}

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"refind-salix (A Salix themed rEFInd UEFI Boot Manager"
"A Salix themed graphical boot manager for EFI- and UEFI-based"
"computers, such as all Intel-based Macs and recent (most 2011 and"
"later) PCs. rEFInd presents a boot menu showing all the EFI boot"
"loaders on the EFI-accessible partitions, and optionally BIOS-bootable"
"partitions on Macs. EFI-compatible OSes, including Linux, provide boot"
"loaders that rEFInd can detect and launch. rEFInd can launch Linux EFI"
"boot loaders such as ELILO, GRUB Legacy, GRUB 2, and 3.3.0 and later"
"kernels with EFI stub support."
)


build() {
	cd $startdir/src/$pkgname-bin-$pkgver
	
	#make directories
	mkdir -p $startdir/pkg/usr/doc/$pkgname-$pkgver
	mkdir -p $startdir/pkg/usr/share/$pkgname-$pkgver
	mkdir -p $startdir/pkg/boot/efi/EFI/refind
	mkdir -p $startdir/pkg/boot/efi/EFI/refind/keys
	mkdir -p $startdir/pkg/boot/efi/EFI/refind/icons
	
	
	cp -a docs/* *.txt $startdir/pkg/usr/doc/$pkgname-$pkgver

	#copy file to the ESP
	 cp -a -R refind/ $startdir/pkg/boot/efi/EFI/
	 cp -a -R keys/*.*er $startdir/pkg/boot/efi/EFI/refind/keys/

	# remove unnecessary package
	if [ "$ARCH" == "x86_64" ]; then
		rm -rf $startdir/pkg/boot/efi/EFI/refind/drivers_ia32 \
		$startdir/pkg/boot/efi/EFI/refind/tools_ia32 \
		$startdir/pkg/boot/efi/EFI/refind/refind_ia32.efi
	else
		rm -rf startdir/pkg/boot/efi/EFI/refind/drivers_x64 \
		$startdir/pkg/boot/efi/EFI/refind/tools_x64 \
		$startdir/pkg/boot/efi/EFI/refind/refind_x64.efi
	fi
	
	rm -rf $startdir/pkg/boot/efi/EFI/refind/drivers_aa64 
	rm -rf $startdir/pkg/boot/efi/EFI/refind/tools_aa64 
	rm -rf $startdir/pkg/boot/efi/EFI/refind/refind_aa64.efi
	rm -rf $startdir/pkg/boot/efi/EFI/refind/refind.conf-sample
	
	
	
	# custom refind config and pngs for Salix theme
	cp -a $startdir/src/refind.conf-salix $startdir/pkg/boot/efi/EFI/refind/refind.conf
	cp -a $startdir/src/os_* $startdir/pkg/boot/efi/EFI/refind/icons 
	cp -a $startdir/src/salix-wall.png $startdir/pkg/boot/efi/EFI/refind/icons/
} 
