#!/bin/sh

#replace default grub boot images with custom BW64 images
rm -f usr/share/boot-dialog/*.svg
mv -f var/puppy.png usr/share/boot-dialog
mv -f var/splash.jpg usr/share/boot-dialog

#add puppy.cer to BW64 build
mv -f var/puppy.cer ../build

#adrv: (1)adjust default firefox settings and (2)add menu option to run firefox as spot
if [ -d ../adrv/usr/bin ]; then
	if [ ! -f ../adrv/opt/FirefoxESR-portable64/profile/prefs.js ]; then
		mkdir -p ../adrv/opt/FirefoxESR-portable64/profile
		echo '// disable default browser check
user_pref("browser.shell.checkDefaultBrowser", false);
// use less vertical space
user_pref("browser.uidensity", 1);
// render website text while fetching images
user_pref("nglayout.initialpaint.delay", 0);
// use less disk space and write less often
user_pref("browser.cache.disk.enable", false);
user_pref("browser.cache.memory.enable", true);
user_pref("browser.sessionstore.resume_from_crash", false);
// disable annoyances
user_pref("browser.ctrlTab.recentlyUsedOrder", false);
user_pref("browser.aboutwelcome.enabled", false);
user_pref("startup.homepage_welcome_url", "");
user_pref("browser.newtabpage.enabled", false);
user_pref("browser.startup.homepage", "/usr/share/doc/home.htm");
user_pref("trailhead.firstrun.didSeeAboutWelcome", true);
user_pref("extensions.pocket.enabled", false);
user_pref("extensions.screenshots.disabled", true);
user_pref("browser.messaging-system.whatsNewPanel.enabled", false);
user_pref("browser.uitour.enabled", false);
user_pref("identity.fxaccounts.enabled", false);
user_pref("identity.fxaccounts.toolbar.enabled", false);
user_pref("browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons", false);
user_pref("browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features", false);
user_pref("browser.tabs.firefox-view", false);
user_pref("browser.newtabpage.activity-stream.showSponsored", false);
user_pref("browser.newtabpage.activity-stream.showSponsoredTopSites", false);
user_pref("browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons", false);
user_pref("browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features", false);
// privacy
user_pref("app.update.auto", false);
user_pref("privacy.globalprivacycontrol.enabled", true);
user_pref("privacy.globalprivacycontrol.was_ever_enabled", true);
user_pref("privacy.donottrackheader.enabled", true);
user_pref("toolkit.telemetry.enabled", false);
user_pref("toolkit.telemetry.unified", false);
user_pref("toolkit.telemetry.reportingpolicy.firstRun", false);
user_pref("app.normandy.enabled", false);
user_pref("datareporting.policy.dataSubmissionEnabled", false);
user_pref("app.shield.optoutstudies.enabled", true);
user_pref("datareporting.healthreport.uploadEnabled", false);
user_pref("dom.private-attribution.submission.enabled", false);
user_pref("browser.discovery.enabled", false);' >> ../adrv/opt/FirefoxESR-portable64/profile/prefs.js
	fi
	if [ ! -f ../adrv/usr/share/applications/firefox_esr_run_as_spot.desktop ]; then
		echo '[Desktop Entry]
Encoding=UTF-8
Name=Firefox ESR (run as spot)
Comment=Run Firefox web browser as restricted user (spot)
Exec=run-as-spot /opt/FirefoxESR-portable64/firefox64/firefox
Icon=/usr/share/pixmaps/firefox_esr.png
Type=Application
Terminal=false
Categories=X-Internet-browser' > ../adrv/usr/share/applications/firefox_esr_run_as_spot.desktop
	fi
fi

#force gtk2 theming of bg_pixmap used for headers of gtk2dialog GUIs generated by dotpup and fontmanager
sed -i "/\/usr\/lib\/gtkdialog\/xml_info gtk/ iGTKDIALOG_BUILD=GTK2 #force gtk2 theming of bg_pixmap" usr/sbin/dotpup
sed -i "/\/usr\/lib\/gtkdialog\/xml_info gtk/ iGTKDIALOG_BUILD=GTK2 #force gtk2 theming of bg_pixmap" usr/sbin/fontmanager

#update Puppy links in /usr/lib/os-release
sed -i -e 's/^HOME_URL.*$/HOME_URL="https:\/\/puppylinux-woof-ce.github.io"/' usr/lib/os-release
sed -i -e 's/^SUPPORT_URL.*$/SUPPORT_URL="https:\/\/forum.puppylinux.com"/' usr/lib/os-release

#adjust Puppy icon themes: create light border for black arrow_mini icons (i.e., make icons visible on dark background)
for i in 16 22 24 32 48 64 scalable; do
	sed -i -e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_down_bottom.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_down.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_left_first.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_left.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_right_last.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_right.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_up.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Standard"/${i}/actions/arrow_mini_up_top.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_down_bottom.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_down.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_left_first.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_left.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_right_last.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_right.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_up.svg \
	-e "s/#777/#FFF/g" usr/share/icons/"Puppy Flat"/${i}/actions/arrow_mini_up_top.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_down_bottom.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_down.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_left_first.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_left.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_right_last.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_right.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_up.svg \
	-e "s/#A4A4A4:/#FFF;/g" usr/share/icons/"PMaterial"/${i}/actions/arrow_mini_up_top.svg
done

#3builddistro may overwrite custom BW64 files -> must restore
rm -f usr/bin/tr
cp -f ../../packages-dpupbw64/zz_bw64/usr/bin/tr usr/bin/tr #replace busybox tr with binary version (when compiling nvidia drivers, busybox tr fails when called by nvidia conftest.sh script)


rm -f usr/bin/zenity
cp -f ../../packages-dpupbw64/zz_bw64/usr/bin/zenity usr/bin/zenity #replace symlink to yad with script  which fixes failed file selector
cp -f ../../packages-dpupbw64/zz_bw64/usr/sbin/auto-setup-spot usr/sbin/auto-setup-spot #append call to update-desktop-database

cp -f ../../packages-dpupbw64/zz_bw64/usr/local/petget/pkg_chooser.sh usr/local/petget/pkg_chooser.sh #replace legacy Packages-puppy-noarch-official with cutdown package list compatible with BW64

cp -f ../../packages-dpupbw64/zz_bw64/usr/share/applications/* usr/share/applications #force copy custom .desktop files to BW64 build
cp -f ../../packages-dpupbw64/zz_bw64/usr/share/connman_ui_gtk/icons/* usr/share/connman_ui_gtk/icons #custom connman icons
cp -rf ../../packages-dpupbw64/zz_bw64/usr/share/doc/* usr/share/doc #force copy custom documentation to BW64 build

cp -f ../../packages-dpupbw64/zz_bw64/root/Choices/ROX-Filer/PuppyPin root/Choices/ROX-Filer/PuppyPin #custom PuppyPin
cp -f ../../packages-dpupbw64/zz_bw64/root/.config/rox.sourceforge.net/ROX-Filer/Options root/.config/rox.sourceforge.net/ROX-Filer/Options #custom RoxFiler configuration

cp -f ../../packages-dpupbw64/zz_bw64/etc/acpi/actions/suspend.sh etc/acpi/actions/suspend.sh #turn off laptop screen when external monitor is attached/enabled
cp -f ../../packages-dpupbw64/zz_bw64/etc/acpi.map etc/acpi.map

rm -f var/packages/Packages-puppy-dpup64-extra && sed -i '/dpup64/d' var/packages/DISTRO_PET_REPOS #show only puppy-noarch-official packages in PPM
cp -rf var/restore/var/* var
rm -rf var/restore

#update custom BW64 help files (usr/share/doc/index.html and usr/share/doc/release-Dpup-10.0.htm)
. etc/DISTRO_SPECS
sed -i -e "s/DISTRO_NAME/$DISTRO_NAME/g" -e "s/DISTRO_VERSION/$DISTRO_VERSION/g" -e "s/DISTRO_PUPPYDATE/$DISTRO_PUPPYDATE/g" usr/share/doc/index.html
sed -i -e "s/DISTRO_NAME/$DISTRO_NAME/g" -e "s/DISTRO_VERSION/$DISTRO_VERSION/g" usr/share/doc/release-Dpup-10.0.htm

#fix sorting of MimeTypes in /usr/share/applications/mimeinfo.cache
#https://unix.stackexchange.com/questions/487458/sort-not-sorting-lines-with-a-pipe-in-it-correctly
[ ! "`grep 'LC_COLLATE=C' usr/bin/update-desktop-database`" ] && sed -i 's/sort/LC_COLLATE=C sort/' usr/bin/update-desktop-database

#fix long-standing syntax error in /usr/local/pup_event/frontend_rox_funcs
#https://forum.puppylinux.com/viewtopic.php?p=123975#p123975
sed -i 's/MOREITEMS="<\/Item>/MOREITEMS="/' usr/local/pup_event/frontend_rox_funcs

#fix long-standing syntax error in /usr/share/applications/pmediainfo.desktop
sed -i 's/application_/application\//g' usr/share/applications/pmediainfo.desktop

#adjust /etc/updatedb.conf so plocate will search /mnt/home
sed -i 's%/initrd %/initrd/mnt/tmpfs /initrd/pup_a /initrd/pup_b /initrd/pup_dev /initrd/pup_f /initrd/pup_k /initrd/pup_loop /initrd/pup_ro1 /initrd/pup_ro2 /initrd/pup_rw /initrd/pup_z %' etc/updatedb.conf

#hwclock --debug is deprecated in psync, replace with --verbose
sed -i 's/hwclock --debug/hwclock --verbose/g' usr/sbin/psync

#adjust Quicksetup call to psync to avoid presenting the user with psync gui before internet connection is established
echo '#!/bin/sh
case $1 in start|restart|stop)
	/usr/sbin/psync $1 1>/dev/null 2>&1 &
esac' > etc/init.d/psync
chmod 664 etc/init.d/psync

#adjust psync if time server returns no offset
sed -i "/offset=/a \		[ -z \"\$offset\" ] && offset=N/A" usr/sbin/psync
sed -i "/offset=/a \		[ -z \"\$offset\" ] && offset=N/A" usr/local/jwmdesk/psync

#add to usage text of samba.sh (Go to Firewall Setup to enable samba services)
sed -i '190i \  <hbox homogeneous=\\\"true\\\"><text use-markup=\\\"true\\\"><label>\\\"Go to <b>Firewall Setup<\/b> to enable samba services\\\"<\/label><\/text><\/hbox>' usr/sbin/samba.sh

#samba.sh fixes
sed -i 's%<\/b>,%<\/b> (or \/mnt\/home if savefile exists),%' usr/sbin/samba.sh
sed -i 's/^\[ ! $MYPATH/\[ ! -d $MYPATH/' usr/sbin/samba.sh

#welcome1sboot fix
sed -i 's/. Experiment/ (or LAN). Experiment/' usr/sbin/welcome1stboot

exit 0
