#!/bin/bash

enable_module fuse || exit 1

wget 'https://github.com/koendv/openscad-raspberrypi/releases/download/v1.7/OpenSCAD-2021.09.13-armhf.AppImage' -O /tmp/OpenSCAD.AppImage || error "Failed to download appimage!"
sudo mv /tmp/OpenSCAD.AppImage /opt/OpenSCAD.AppImage
sudo chmod +x /opt/OpenSCAD.AppImage || error "Failed to mark as executable!"

#make terminal command
sudo rm -f /usr/local/bin/openscad
sudo ln -s /opt/OpenSCAD.AppImage /usr/local/bin/openscad || error "Failed to make /usr/local/bin/openscad terminal symlink!"

status -n "Creating .scad mimetype... "
echo '<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
   <mime-type type="application/x-openscad">
     <comment>OpenSCAD Project</comment>
     <glob pattern="*.scad"/>
     <generic-icon name="openscad"/>
   </mime-type>
</mime-info>' | sudo tee /usr/share/mime/packages/openscad.xml >/dev/null
sudo update-mime-database /usr/share/mime

sudo cp "$(dirname "$0")/icon-64.png" /usr/share/icons/openscad.png
sudo update-icon-caches /usr/share/icons/*
status_green Done

if ! grep -qF 'OpenSCAD.desktop' ~/.config/mimeapps.list ;then
  status -n "Associating the .scad mimetype with OpenSCAD... "
  echo "[Added Associations]
application/x-openscad=OpenSCAD.desktop;" >> ~/.config/mimeapps.list
  status_green Done
fi

status -n "Creating menu button... "
echo "[Desktop Entry]
Name=OpenSCAD
Comment=The Programmer's Solid 3D CAD Modeler
Exec=/opt/OpenSCAD.AppImage %f
Icon=$(dirname $0)/icon-64.png
Terminal=false
Type=Application
MimeType=application/x-openscad;
Categories=Graphics;3DGraphics;Engineering;
Keywords=3d;solid;geometry;csg;model;stl;
StartupNotify=true" | sudo tee /usr/share/applications/OpenSCAD.desktop >/dev/null || error "Failed to create menu button!"
