#!/bin/bash

# install dependencies
install_packages build-essential git cmake libssl-dev libudev-dev qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools libqt5serialport5-dev libqt5svg5-dev libgit2-dev pkg-config libboost-all-dev || exit 1

# install qt5-default if its availabe in apt (necessary on debian/ubuntu versions prior to bullseye/hirsute)
package_available qt5-default
if [[ $? == "0" ]]; then
  install_packages qt5-default || error "Failed to install dependencies"
else
  # attempt to fix common error where users libqt5core5a package did not install properly
  # this should not be necessary, but /usr/lib/*-linux-gnu*/qt-default/qtchooser/default.conf is missing on multiple users installs and is part of this package
  sudo apt install --reinstall libqt5core5a -y
fi

# print debug output to terminal
hash -r
echo "Logging contents of file: /usr/lib/*-linux-gnu*/qt-default/qtchooser/default.conf"
cat /usr/lib/*-linux-gnu*/qt-default/qtchooser/default.conf
echo "Log file status: /usr/lib/*-linux-gnu*/qt-default/qtchooser/default.conf"
ls -l /usr/lib/*-linux-gnu*/qt-default/qtchooser/default.conf
echo "Logging contents of file: /usr/share/qtchooser/qt5-*-linux-gnu*.conf"
cat /usr/share/qtchooser/qt5-*-linux-gnu*.conf
echo "Log qmake -v output"
qmake -v
echo "apt list libqt5core5a qtchooser"
apt list libqt5core5a qtchooser

# download parts directory
mkdir -p $HOME/.local/share/fritzing || error "Could not make directory"
cd $HOME/.local/share/fritzing || error "Could not move to directory"
rm -rf parts
git clone https://github.com/fritzing/fritzing-parts.git parts || error "Could not clone parts repo source"

# download, compile, and install fritzing app
cd /tmp || error "Could not move to directory"
rm -rf fritzing-app
git clone https://github.com/fritzing/fritzing-app.git || error "Could not clone fritzing-app source"
cd fritzing-app || error "Could not move to directory"
git checkout 0.9.6
# move to quazip folder and apply patch
cd src/lib/quazip || error "Could not move to directory"

# patch based on https://github.com/stachenov/quazip/commit/5f48264e078c1cc5f0682e90acc9ef0c8f011c19.patch with minor edits
cat << 'EOF' >> quazip.patch
--- crypt.h	2025-10-05 17:12:52.333847151 -0400
+++ crypt.h	2025-10-05 17:15:23.115088496 -0400
@@ -32,7 +32,7 @@
 /***********************************************************************
  * Return the next byte in the pseudo-random sequence
  */
-static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
+static int decrypt_byte(unsigned long* pkeys, const z_crc_t FAR * pcrc_32_tab)
 {
 #ifndef _WINDOWS
 	(void) pcrc_32_tab; /* avoid "unused parameter" warning */
@@ -49,7 +49,7 @@
 /***********************************************************************
  * Update the encryption keys with the next byte of plain text
  */
-static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
+static int update_keys(unsigned long* pkeys,const z_crc_t FAR * pcrc_32_tab,int c)
 {
 	(*(pkeys+0)) = CRC32((*(pkeys+0)), c);
 	(*(pkeys+1)) += (*(pkeys+0)) & 0xff;
@@ -66,7 +66,7 @@
  * Initialize the encryption keys and the random header according to
  * the given password.
  */
-static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
+static void init_keys(const char* passwd,unsigned long* pkeys,const z_crc_t FAR * pcrc_32_tab)
 {
 	*(pkeys+0) = 305419896L;
 	*(pkeys+1) = 591751049L;
@@ -96,7 +96,7 @@
 unsigned char *buf;         /* where to write header */
 int bufSize;
 unsigned long* pkeys;
-const unsigned long* pcrc_32_tab;
+const z_crc_t FAR * pcrc_32_tab;
 unsigned long crcForCrypting;
 {
 	int n;                       /* index in random header */
--- unzip.c	2025-10-05 17:12:52.334031525 -0400
+++ unzip.c	2025-10-05 17:15:40.996747630 -0400
@@ -150,7 +150,7 @@
     int encrypted;
 #    ifndef NOUNCRYPT
     unsigned long keys[3];     /* keys defining the pseudo-random sequence */
-    const unsigned long* pcrc_32_tab;
+    const z_crc_t FAR * pcrc_32_tab;
 #    endif
 } unz_s;
 
--- zip.c	2025-10-05 17:12:52.334031525 -0400
+++ zip.c	2025-10-05 17:15:56.058461707 -0400
@@ -134,7 +134,7 @@
     int  encrypt;
 #ifndef NOCRYPT
     unsigned long keys[3];     /* keys defining the pseudo-random sequence */
-    const unsigned long* pcrc_32_tab;
+    const z_crc_t FAR * pcrc_32_tab;
     int crypt_header_size;
 #endif
 } curfile_info;

EOF

git apply quazip.patch || error "Unable to apply patch"
cd ../../../ || error "Could not move to directory"
# patch compilation script
sed -i 's/LIBGIT_STATIC = true/LIBGIT_STATIC = false/g' phoenix.pro
# compile the code
qmake || error "PLEASE contact us in the Discord. We have seen multiple reports of this error and are unable to reproduce on our end. qmake failed on fritzing configure"
make -j$(nproc) || error "compilation failed"
lupdate -noobsolete phoenix.pro
lrelease -removeidentical phoenix.pro
sudo make install || error "Install failed"
sudo sed -i "s:Exec=Fritzing %F:Exec=Fritzing --parts $HOME/.local/share/fritzing/parts %F:g" /usr/share/applications/org.fritzing.Fritzing.desktop

# remove source files
rm -rf /tmp/fritzing-app

status_green "Fritzing is installed.
To run: Menu -> Programming -> Fritzing
To run in a terminal: Fritzing --parts $HOME/.local/share/fritzing/parts
"
