#!/bin/bash

echo "------- Free MC Boot loader v 1.953 multi region/model full installation -------"
echo "------------ original script \& applications by jimmikaelkael et al. ------------"
echo "------------------- updated for SP193's FMCB v 1.953 by Yuki -------------------"

# sign the kelfs/kirx for the memory card
echo "Signing FMCB kelf..."
./ps3mca-tool -k fmcb-install/SYSTEM/FMCB.XLF fmcb-install/SYSTEM/osdmain.elf &> /dev/null || { echo "ERROR: failed to sign FMCB kelf"; exit 1; }
echo "Signing osd110 kelf..."
./ps3mca-tool -k fmcb-install/SYSTEM/OSD110.XLF fmcb-install/SYSTEM/osd110.elf &> /dev/null || { echo "ERROR: failed to sign osd110 kelf"; exit 1; }
echo "Signing OSDSYS update kelf..."
./ps3mca-tool -k fmcb-install/SYSTEM/OSDSYS.XLF fmcb-install/SYSTEM/osdsys.elf &> /dev/null || { echo "ERROR: failed to sign OSDSYS update kelf"; exit 1; }
echo "Signing DVD kirx..."
./ps3mca-tool -k fmcb-install/SYSTEM/ENDVDPL.XRX fmcb-install/SYS-CONF/endvdpl.irx &> /dev/null || { echo "ERROR: failed to sign DVD kirx"; exit 1; }

echo "Creating necessary directories..."
./ps3mca-tool -mkdir /APPS &> /dev/null || { echo "ERROR: failed to create 'APPS' directory"; exit 1; }
./ps3mca-tool -mkdir /BOOT &> /dev/null || { echo "ERROR: failed to create 'BOOT' directory"; exit 1; }
./ps3mca-tool -mkdir /BAEXEC-SYSTEM &> /dev/null || { echo "ERROR: failed to create 'BAEXEC-SYSTEM' directory"; exit 1; }
./ps3mca-tool -mkdir /BCEXEC-SYSTEM &> /dev/null || { echo "ERROR: failed to create 'BCEXEC-SYSTEM' directory"; exit 1; }
./ps3mca-tool -mkdir /BEEXEC-SYSTEM &> /dev/null || { echo "ERROR: failed to create 'BEEXEC-SYSTEM' directory"; exit 1; }
./ps3mca-tool -mkdir /BIEXEC-SYSTEM &> /dev/null || { echo "ERROR: failed to create 'BIEXEC-SYSTEM' directory"; exit 1; }
./ps3mca-tool -mkdir /SYS-CONF &> /dev/null || { echo "ERROR: failed to create 'SYS-CONF' directory"; exit 1; }

# copy the signed kelfs/kirx to mc
echo "Copying signed FMCB kelf..."
./ps3mca-tool -in fmcb-install/SYSTEM/osdmain.elf /BIEXEC-SYSTEM/osdmain.elf &> /dev/null || { echo "ERROR: failed to copy signed FMCB kelf"; exit 1; }
echo "Copying signed osd110 kelf..."
./ps3mca-tool -in fmcb-install/SYSTEM/osd110.elf /BIEXEC-SYSTEM/osd110.elf &> /dev/null || { echo "ERROR: failed to copy signed osd110 kelf"; exit 1; }
echo "Copying signed OSDSYS update kelf..."
./ps3mca-tool -in fmcb-install/SYSTEM/osdsys.elf /BIEXEC-SYSTEM/osdsys.elf &> /dev/null || { echo "ERROR: failed to copy signed OSDSYS update kelf"; exit 1; }
echo "Copying signed DVD kirx..."
./ps3mca-tool -in fmcb-install/SYS-CONF/endvdpl.irx /SYS-CONF/endvdpl.irx &> /dev/null || { echo "ERROR: failed to copy signed DVD kirx"; exit 1; }

# write all required files
echo "Copying default configuration file..."
./ps3mca-tool -in fmcb-install/SYS-CONF/FREEMCB.CNF /SYS-CONF/FREEMCB.CNF &> /dev/null || { echo "ERROR: failed to copy default connfiguration file"; exit 1; }
echo "Copying FMCB configurator..."
./ps3mca-tool -in fmcb-install/SYS-CONF/FMCB_CFG.ELF /SYS-CONF/FMCB_CFG.ELF &> /dev/null || { echo "ERROR: failed to copy FMCB configurator"; exit 1; }
echo "Copying HDD modules..."
./ps3mca-tool -in fmcb-install/SYSTEM/atad.irx /BIEXEC-SYSTEM/atad.irx &> /dev/null || { echo "ERROR: failed to copy 'atad.irx'"; exit 1; }
./ps3mca-tool -in fmcb-install/SYSTEM/hddload.irx /BIEXEC-SYSTEM/hddload.irx &> /dev/null || { echo "ERROR: failed to copy 'hddload.irx'"; exit 1; }
echo "Copying USB modules..."
./ps3mca-tool -in fmcb-install/SYS-CONF/USBD.IRX /SYS-CONF/USBD.IRX &> /dev/null || { echo "ERROR: failed to copy 'USBD.IRX'"; exit 1; }
./ps3mca-tool -in fmcb-install/SYS-CONF/USBHDFSD.IRX /SYS-CONF/USBHDFSD.IRX &> /dev/null || { echo "ERROR: failed to copy 'USBHDFSD.IRX'"; exit 1; }
echo "Copying FMCB icons..."
./ps3mca-tool -in fmcb-install/SYSTEM/icon.sys /BIEXEC-SYSTEM/icon.sys &> /dev/null || { echo "ERROR: failed to copy 'icon.sys'"; exit 1; }
./ps3mca-tool -in fmcb-install/SYSTEM/FMCB.icn /BIEXEC-SYSTEM/FMCB.icn &> /dev/null || { echo "ERROR: failed to copy 'FMCB.icn'"; exit 1; }
echo "Copying APPS folder icons..."
./ps3mca-tool -in fmcb-install/APPS/icon.sys /APPS/icon.sys &> /dev/null || { echo "ERROR: failed to copy 'icon.sys'"; exit 1; }
./ps3mca-tool -in fmcb-install/APPS/FMCBapps.icn /APPS/FMCBapps.icn &> /dev/null || { echo "ERROR: failed to copy 'FMCBapps.icn'"; exit 1; }
echo "Copying BOOT folder icons..."
./ps3mca-tool -in fmcb-install/BOOT/icon.sys /BOOT/icon.sys &> /dev/null || { echo "ERROR: failed to copy 'icon.sys'"; exit 1; }
./ps3mca-tool -in fmcb-install/BOOT/BOOT.icn /BOOT/BOOT.icn &> /dev/null || { echo "ERROR: failed to copy 'BOOT.icn'"; exit 1; }
echo "Copying SYS-CONF folder icons..."
./ps3mca-tool -in fmcb-install/SYS-CONF/icon.sys /SYS-CONF/icon.sys &> /dev/null || { echo "ERROR: failed to copy 'icon.sys'"; exit 1; }
./ps3mca-tool -in fmcb-install/SYS-CONF/sysconf.icn /SYS-CONF/sysconf.icn &> /dev/null || { echo "ERROR: failed to copy 'sysconf.icn'"; exit 1; }

# write external homebrew apps
echo "Copying wLaunchELF..."
./ps3mca-tool -in fmcb-install/BOOT/BOOT.ELF /BOOT/BOOT.ELF &> /dev/null || { echo "ERROR: failed to copy wLaunchELF"; exit 1; }
echo "Copying ESR..."
./ps3mca-tool -in fmcb-install/BOOT/ESR.ELF /BOOT/ESR.ELF &> /dev/null || { echo "ERROR: failed to copy ESR"; exit 1; }
echo "Copying ESR GUI..."
./ps3mca-tool -in fmcb-install/APPS/ESRGUI.ELF /APPS/ESRGUI.ELF &> /dev/null || { echo "ERROR: failed to copy ESR GUI"; exit 1; }
echo "Copying OPL..."
./ps3mca-tool -in fmcb-install/APPS/OPL.ELF /APPS/OPL.ELF &> /dev/null || { echo "ERROR: failed to copy OPL"; exit 1; }
echo "Copying SMS..."
./ps3mca-tool -in fmcb-install/APPS/SMS.ELF /APPS/SMS.ELF &> /dev/null || { echo "ERROR: failed to copy SMS"; exit 1; }
echo "Copying Power Off elf..."
./ps3mca-tool -in fmcb-install/APPS/OFF.ELF /APPS/OFF.ELF &> /dev/null || { echo "ERROR: failed to copy Power Off elf"; exit 1; }

echo "Cross-linking files for multi region/model installation..."
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BAEXEC-SYSTEM/osd120.elf &> /dev/null || { echo "ERROR: failed to create link '/BAEXEC-SYSTEM/osd120.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BAEXEC-SYSTEM/osd130.elf &> /dev/null || { echo "ERROR: failed to create link '/BAEXEC-SYSTEM/osd130.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BAEXEC-SYSTEM/osdmain.elf &> /dev/null || { echo "ERROR: failed to create link '/BAEXEC-SYSTEM/osdmain.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BCEXEC-SYSTEM/osdmain.elf &> /dev/null || { echo "ERROR: failed to create link '/BCEXEC-SYSTEM/osdmain.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BEEXEC-SYSTEM/osd130.elf &> /dev/null || { echo "ERROR: failed to create link '/BEEXEC-SYSTEM/osd130.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BEEXEC-SYSTEM/osdmain.elf &> /dev/null || { echo "ERROR: failed to create link '/BEEXEC-SYSTEM/osdmain.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/osdmain.elf /BIEXEC-SYSTEM/osd130.elf &> /dev/null || { echo "ERROR: failed to create link '/BIEXEC-SYSTEM/osd130.elf'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/icon.sys /BAEXEC-SYSTEM/icon.sys &> /dev/null || { echo "ERROR: failed to create link '/BAEXEC-SYSTEM/icon.sys'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/FMCB.icn /BAEXEC-SYSTEM/FMCB.icn &> /dev/null || { echo "ERROR: failed to create link '/BAEXEC-SYSTEM/FMCB.icn'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/icon.sys /BCEXEC-SYSTEM/icon.sys &> /dev/null || { echo "ERROR: failed to create link '/BCEXEC-SYSTEM/icon.sys'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/FMCB.icn /BCEXEC-SYSTEM/FMCB.icn &> /dev/null || { echo "ERROR: failed to create link '/BCEXEC-SYSTEM/FMCB.icn'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/icon.sys /BEEXEC-SYSTEM/icon.sys &> /dev/null || { echo "ERROR: failed to create link '/BEEXEC-SYSTEM/icon.sys'"; exit 1; }
./ps3mca-tool -cl /BIEXEC-SYSTEM/FMCB.icn /BEEXEC-SYSTEM/FMCB.icn &> /dev/null || { echo "ERROR: failed to create link '/BEEXEC-SYSTEM/FMCB.icn'"; exit 1; }

echo "Copying Install Log..."
./ps3mca-tool -in fmcb-install/SYS-CONF/FMCBUINST.dat /SYS-CONF/FMCBUINST.dat &> /dev/null || { echo "ERROR: failed to copy Install Log"; exit 1; }

echo "Deleting temporary files..."
rm -f fmcb-install/SYSTEM/osdmain.elf fmcb-install/SYSTEM/osd110.elf fmcb-install/SYSTEM/osdsys.elf fmcb-install/SYS-CONF/endvdpl.irx &> /dev/null

echo "Installation complete!"
