#!/bin/bash

echo "IPXE USB FLASHER"

echo "SERVER BOOT URI:"
read uri
lsblk -e 7 -o NAME,LABEL,SIZE,TYPE,MOUNTPOINT,PARTLABEL
echo "TARGET DRIVE:"
read drv

#sudo apt update
#sudo apt install -y git liblzma-dev syslinux isolinux mkisofs mtools perl gcc binutils make

#iPXE
#sudo rm -rf /ipxe
git clone https://github.com/ipxe/ipxe.git --progress
cd ipxe/src
git pull

echo "
#define	NET_PROTO_IPV4		/* IPv4 protocol */
#define NET_PROTO_IPV6	    /* IPv6 protocol */
#define	NET_PROTO_FCOE		/* Fibre Channel over Ethernet protocol */
#define	NET_PROTO_STP		/* Spanning Tree protocol */
#define	NET_PROTO_LACP		/* Link Aggregation control protocol */
#define	NET_PROTO_EAPOL		/* EAP over LAN protocol */
#define NET_PROTO_LLDP	    /* Link Layer Discovery protocol */
#define	DOWNLOAD_PROTO_TFTP	/* Trivial File Transfer Protocol */
#define	DOWNLOAD_PROTO_HTTP	/* Hypertext Transfer Protocol */
#define	DOWNLOAD_PROTO_HTTPS	/* Secure Hypertext Transfer Protocol */
#define	DOWNLOAD_PROTO_FTP	/* File Transfer Protocol */
#define	DOWNLOAD_PROTO_SLAM	/* Scalable Local Area Multicast */
#define	DOWNLOAD_PROTO_NFS	/* Network File System Protocol */
#define DOWNLOAD_PROTO_FILE	/* Local filesystem access */
#define	SANBOOT_PROTO_ISCSI	/* iSCSI protocol */
#define	SANBOOT_PROTO_AOE	/* AoE protocol */
#define	SANBOOT_PROTO_IB_SRP	/* Infiniband SCSI RDMA protocol */
#define	SANBOOT_PROTO_FCP	/* Fibre Channel protocol */
#define	SANBOOT_PROTO_HTTP	/* HTTP SAN protocol */
//#define IMAGE_NBI	    /* NBI image support */
//#define IMAGE_ELF		/* ELF image support */
//#define IMAGE_MULTIBOOT		/* MultiBoot image support */
#define IMAGE_PXE		/* PXE image support */
#define IMAGE_SCRIPT		/* iPXE script image support */
#define IMAGE_LKRN		/* Linux kernel image support */
//#define IMAGE_COMBOOT	/* SYSLINUX COMBOOT image support */
//#define IMAGE_EFI		/* EFI image support */
//#define IMAGE_SDI		/* SDI image support */
#define IMAGE_PNM		/* PNM image support */
#define IMAGE_PNG		/* PNG image support */
#define IMAGE_DER		/* DER image support */
#define IMAGE_PEM		/* PEM image support */
#define IMAGE_EFISIG		/* EFI signature list image support */
#define IMAGE_ZLIB		/* ZLIB image support */
#define IMAGE_GZIP		/* GZIP image support */
#define IMAGE_UCODE     /* Microcode update image support */
#define AUTOBOOT_CMD		/* Automatic booting */
#define NVO_CMD			/* Non-volatile option storage commands */
#define CONFIG_CMD		/* Option configuration console */
#define IFMGMT_CMD		/* Interface management commands */
#define IWMGMT_CMD		/* Wireless interface management commands */
#define IBMGMT_CMD		/* Infiniband management commands */
#define FCMGMT_CMD		/* Fibre Channel management commands */
#define ROUTE_CMD		/* Routing table management commands */
#define IMAGE_CMD		/* Image management commands */
#define DHCP_CMD		/* DHCP management commands */
#define SANBOOT_CMD		/* SAN boot commands */
#define MENU_CMD		/* Menu commands */
#define FORM_CMD		/* Form commands */
#define LOGIN_CMD		/* Login command */
#define SYNC_CMD		/* Sync command */
#define SHELL_CMD		/* Shell command */
#define NSLOOKUP_CMD		/* DNS resolving command */
#define TIME_CMD		/* Time commands */
#define DIGEST_CMD		/* Image crypto digest commands */
#define LOTEST_CMD		/* Loopback testing commands */
#define VLAN_CMD		/* VLAN commands */
#define PXE_CMD		/* PXE commands */
#define REBOOT_CMD		/* Reboot command */
#define POWEROFF_CMD		/* Power off command */
#define IMAGE_TRUST_CMD	/* Image trust management commands */
#define IMAGE_CRYPT_CMD	/* Image encryption management commands */
#define PCI_CMD		/* PCI commands */
#define PARAM_CMD		/* Request parameter commands */
#define NEIGHBOUR_CMD		/* Neighbour management commands */
#define PING_CMD		/* Ping command */
#define CONSOLE_CMD		/* Console command */
#define IPSTAT_CMD		/* IP statistics commands */
#define PROFSTAT_CMD		/* Profiling commands */
#define NTP_CMD		/* NTP commands */
#define CERT_CMD		/* Certificate management commands */
#define IMAGE_MEM_CMD		/* Read memory command */
#define IMAGE_ARCHIVE_CMD	/* Archive image management commands */
#define SHIM_CMD		/* EFI shim command (or dummy command) */
#define USB_CMD		/* USB commands */
#define FDT_CMD		/* Flattened Device Tree commands */
" > ./config/local/general.h

#make bin/ipxe.pxe
#make bin/ipxe.iso
#make bin/undionly.kpxe

echo "
#define	NET_PROTO_IPV4		/* IPv4 protocol */
#define NET_PROTO_IPV6	    /* IPv6 protocol */
#define	NET_PROTO_FCOE		/* Fibre Channel over Ethernet protocol */
#define	NET_PROTO_STP		/* Spanning Tree protocol */
#define	NET_PROTO_LACP		/* Link Aggregation control protocol */
#define	NET_PROTO_EAPOL		/* EAP over LAN protocol */
#define NET_PROTO_LLDP	    /* Link Layer Discovery protocol */
#define	DOWNLOAD_PROTO_TFTP	/* Trivial File Transfer Protocol */
#define	DOWNLOAD_PROTO_HTTP	/* Hypertext Transfer Protocol */
#define	DOWNLOAD_PROTO_HTTPS	/* Secure Hypertext Transfer Protocol */
#define	DOWNLOAD_PROTO_FTP	/* File Transfer Protocol */
#define	DOWNLOAD_PROTO_SLAM	/* Scalable Local Area Multicast */
#define	DOWNLOAD_PROTO_NFS	/* Network File System Protocol */
#define DOWNLOAD_PROTO_FILE	/* Local filesystem access */
#define	SANBOOT_PROTO_ISCSI	/* iSCSI protocol */
#define	SANBOOT_PROTO_AOE	/* AoE protocol */
#define	SANBOOT_PROTO_IB_SRP	/* Infiniband SCSI RDMA protocol */
#define	SANBOOT_PROTO_FCP	/* Fibre Channel protocol */
#define	SANBOOT_PROTO_HTTP	/* HTTP SAN protocol */
//#define IMAGE_NBI	    /* NBI image support */
//#define IMAGE_ELF		/* ELF image support */
//#define IMAGE_MULTIBOOT		/* MultiBoot image support */
//#define IMAGE_PXE		/* PXE image support */
#define IMAGE_SCRIPT		/* iPXE script image support */
#define IMAGE_LKRN		/* Linux kernel image support */
//#define IMAGE_COMBOOT	/* SYSLINUX COMBOOT image support */
#define IMAGE_EFI		/* EFI image support */
//#define IMAGE_SDI		/* SDI image support */
#define IMAGE_PNM		/* PNM image support */
#define IMAGE_PNG		/* PNG image support */
#define IMAGE_DER		/* DER image support */
#define IMAGE_PEM		/* PEM image support */
#define IMAGE_EFISIG		/* EFI signature list image support */
#define IMAGE_ZLIB		/* ZLIB image support */
#define IMAGE_GZIP		/* GZIP image support */
#define IMAGE_UCODE     /* Microcode update image support */
#define AUTOBOOT_CMD		/* Automatic booting */
#define NVO_CMD			/* Non-volatile option storage commands */
#define CONFIG_CMD		/* Option configuration console */
#define IFMGMT_CMD		/* Interface management commands */
#define IWMGMT_CMD		/* Wireless interface management commands */
#define IBMGMT_CMD		/* Infiniband management commands */
#define FCMGMT_CMD		/* Fibre Channel management commands */
#define ROUTE_CMD		/* Routing table management commands */
#define IMAGE_CMD		/* Image management commands */
#define DHCP_CMD		/* DHCP management commands */
#define SANBOOT_CMD		/* SAN boot commands */
#define MENU_CMD		/* Menu commands */
#define FORM_CMD		/* Form commands */
#define LOGIN_CMD		/* Login command */
#define SYNC_CMD		/* Sync command */
#define SHELL_CMD		/* Shell command */
#define NSLOOKUP_CMD		/* DNS resolving command */
#define TIME_CMD		/* Time commands */
#define DIGEST_CMD		/* Image crypto digest commands */
#define LOTEST_CMD		/* Loopback testing commands */
#define VLAN_CMD		/* VLAN commands */
//#define PXE_CMD		/* PXE commands */
#define REBOOT_CMD		/* Reboot command */
#define POWEROFF_CMD		/* Power off command */
#define IMAGE_TRUST_CMD	/* Image trust management commands */
#define IMAGE_CRYPT_CMD	/* Image encryption management commands */
#define PCI_CMD		/* PCI commands */
#define PARAM_CMD		/* Request parameter commands */
#define NEIGHBOUR_CMD		/* Neighbour management commands */
#define PING_CMD		/* Ping command */
#define CONSOLE_CMD		/* Console command */
#define IPSTAT_CMD		/* IP statistics commands */
#define PROFSTAT_CMD		/* Profiling commands */
#define NTP_CMD		/* NTP commands */
#define CERT_CMD		/* Certificate management commands */
#define IMAGE_MEM_CMD		/* Read memory command */
#define IMAGE_ARCHIVE_CMD	/* Archive image management commands */
#define SHIM_CMD		/* EFI shim command (or dummy command) */
#define USB_CMD		/* USB commands */
#define FDT_CMD		/* Flattened Device Tree commands */
" > ./config/local/general.h

make bin-x86_64-efi/ipxe.efi
make bin-x86_64-efi/ipxe.iso
make bin-x86_64-efi/snp.efi
make bin-x86_64-efi/snponly.efi
#make bin-i386-efi/ipxe.efi
#make bin-i386-efi/ipxe.iso
#make bin-i386-efi/snp.efi
#make bin-i386-efi/snponly.efi


sudo echo "#!ipxe

imgfree
ifopen
#iflinkwait
#dhcp
chain --autofree $uri
" > chain.ipxe

make bin-x86_64-efi/ipxe.usb EMBED=chain.ipxe
#rm chain.ipxe
sudo dd if=bin-x86_64-efi/ipxe.usb of=/dev/$drv

