#!/bin/bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo >> $HOME/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> $HOME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
source $HOME/.zprofile
brew install readline pkg-config xz tcl-tk mpdecimal

Contents="/Applications/OnvifGUI.app/Contents"
MacOS="$Contents/MacOS"
Resources="$Contents/Resources"

cd $HOME
mkdir -p $MacOS
mkdir -p $Resources

mkdir Icon.iconset
#sips -z 16 16 "$HOME/libonvif/assets/scripts/components/mac/icons/16.png" --out Icon.iconset/icon_16x16.png
#sips -z 32 32 "$HOME/libonvif/assets/scripts/components/mac/icons/32.png" --out Icon.iconset/icon_32x32.png
#sips -z 64 64 "$HOME/libonvif/assets/scripts/components/mac/icons/64.png" --out Icon.iconset/icon_64x64.png
#sips -z 128 128 "$HOME/libonvif/assets/scripts/components/mac/icons/128.png" --out Icon.iconset/icon_128x128.png
#sips -z 256 256 "$HOME/libonvif/assets/scripts/components/mac/icons/256.png" --out Icon.iconset/icon_256x256.png
sips -z 256 256 $HOME/libonvif/onvif-gui/onvif_gui/resources/mac_icon.png --out Icon.iconset/icon_256x256.png
iconutil -c icns Icon.iconset -o $Resources/app.icns

echo "-----------------START OPENSSL---------------------------------"

install_dir=$MacOS
cd $HOME
if [ ! -d openssl ]; then
    git clone https://github.com/openssl/openssl.git
fi
cd openssl
git switch openssl-3.5
./Configure --prefix=$install_dir/OpenSSL
make -j$(nproc)
make install

echo "-----------------START GDBM---------------------------------"

cd $HOME
curl -OL https://ftp.gnu.org/gnu/gdbm/gdbm-1.25.tar.gz
tar xzf gdbm-1.25.tar.gz
cd gdbm-1.25
sed -i.bak 's/if (clock_nanosleep (CLOCK_REALTIME, 0, iv, &r))/if (nanosleep (iv, \&r))/g' src/lock.c
./configure --prefix=$install_dir/gdbm CPPFLAGS="-I$(brew --prefix readline)/include" LDFLAGS="-L$(brew --prefix readline)/lib"
make 
make install

echo "-----------------START LZMA---------------------------------"

cd $HOME
curl -OL https://github.com/tukaani-project/xz/releases/download/v5.8.1/xz-5.8.1.tar.gz
tar xzf xz-5.8.1.tar.gz
cd xz-5.8.1
./configure --prefix=$install_dir/xz
make
make install

echo "-----------------START LIBB2---------------------------------"

cd $HOME
curl -OL https://github.com/BLAKE2/libb2/releases/download/v0.98.1/libb2-0.98.1.tar.gz
tar xzf libb2-0.98.1.tar.gz
cd libb2-0.98.1
./configure --prefix=$install_dir/b2
make
make install

echo "-----------------START MPDECIMAL---------------------------------"

curl -OL https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-4.0.1.tar.gz
tar xzf mpdecimal-4.0.1.tar.gz
cd mpdecimal-4.0.1
./configure --prefix=$install_dir/mpdec
make
make install

echo "-----------------START PYTHON---------------------------------"

cd $HOME
if [ ! -d cpython ]; then
    git clone https://github.com/python/cpython.git
fi
cd cpython
git switch 3.13
GDBM_CFLAGS="-I$install_dir/gdbm/include" GDBM_LIBS="-L$install_dir/gdbm/lib -lgdbm" \
LIBLZMA_CFLAGS="-I$install_dir/xz/include" LIBLZMA_LIBS="-L$install_dir/xz/lib -llzma" \
LIBB2_CFLAGS="-I$install_dir/b2/include" LIBB2_LIBS="-L$install_dir/b2/lib -lb2" \
LIBMPDEC_CFLAGS="-I$install_dir/mpdec/include" LIBMPDEC_LIBS="-L$install_dir/mpdec/lib -lmpdec" \
    ./configure --enable-optimizations \
    --enable-framework=/Applications/OnvifGUI.app/Contents/MacOS/Python/Library/Frameworks \
    --with-app-store-compliance \
    --with-system-libmpdec \
    --with-openssl="$install_dir/OpenSSL" \
    --prefix=$install_dir/Python
make -j$(nproc)
make install

cc $HOME/libonvif/assets/scripts/components/mac/launcher.c -o $MacOS/launcher

rm -R $MacOS/Python/Applications
rm -R $MacOS/Python/bin
rm -R $MacOS/Python/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/test
rm -R $MacOS/OpenSSL/include
rm -R $MacOS/OpenSSL/share
rm -R $MacOS/OpenSSL/lib/pkgconfig
rm -R $MacOS/OpenSSL/lib/cmake
rm -R $MacOS/OpenSSL/lib/*.a
rm -R $MacOS/gdbm/include
rm -R $MacOS/gdbm/share

framework="$MacOS/Python/Library/Frameworks/Python.framework"
$framework/Versions/3.13/bin/python3.13 -m venv $framework/Versions/3.13/onvif-gui-env
source $framework/Versions/3.13/onvif-gui-env/bin/activate
pip install $HOME/libonvif/onvif-gui torch torchvision
deactivate
rm $framework/Versions/3.13/onvif-gui-env/bin/python*
cp $framework/Versions/3.13/bin/python3.13 $framework/Versions/3.13/onvif-gui-env/bin

cache="$framework/Versions/3.13/onvif-gui-env/lib/python3.13/site-packages/cache"
mkdir -p $cache/checkpoints
cd $cache/checkpoints
curl -OL https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_s.pth

mkdir -p $cache/proxy
cd $cache/proxy
curl -OL https://github.com/bluenviron/mediamtx/releases/download/v1.12.2/mediamtx_v1.12.2_darwin_arm64.tar.gz
tar xzf mediamtx_v1.12.2_darwin_arm64.tar.gz
rm mediamtx_v1.12.2_darwin_arm64.tar.gz
sed -i.bak 's/srt: yes/srt: no/g' mediamtx.yml
sed -i.bak 's/webrtc: yes/webrtc: no/g' mediamtx.yml
sed -i.bak 's/hls: yes/hls: no/g' mediamtx.yml
sed -i.bak 's/rtmp: yes/rtmp: no/g' mediamtx.yml
rm mediamtx.yml.bak

cat > $Contents/Info.plist <<EOF

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleName</key>
    <string>Onvif GUI</string>
    <key>CFBundleDisplayName</key>
    <string>Onvif GUI</string>
    <key>CFBundleIdentifier</key>
    <string>io.github.sr99622.onvif-gui</string>
    <key>CFBundleVersion</key>
    <string>3.0.11</string>
    <key>CFBundleExecutable</key>
    <string>launcher</string>
    <key>CFBundleIconFile</key>
    <string>app.icns</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
</dict>
</plist>
EOF

find '/Applications/OnvifGUI.app' \( -type f -perm +111 -o -name "*.dylib" -o -name "*.so" \) -exec codesign --force --deep --options runtime --timestamp --entitlements $HOME/libonvif/assets/scripts/components/mac/entitlements.plist --sign "Developer ID Application: Stephen Rhodes (NPMFEZ6V9H)" {} \;
codesign --force --deep --options runtime --timestamp --entitlements $HOME/libonvif/assets/scripts/components/mac/entitlements.plist --sign "Developer ID Application: Stephen Rhodes (NPMFEZ6V9H)" /Applications/OnvifGUI.app
codesign -vvv --deep --strict /Applications/OnvifGUI.app
