#!/bin/sh

# Copyright (c) ULBSD Project Team. All rights reserved.
# $Id: ulbsd-update 2 2020-04-06 16:44:15Z sv $

if [ "${1}" == "menu" ]
then

# Маскировка ненужных ярлыков в меню программ

    DIR='/usr/local/share/applications/'
    find "${DIR}" -name "gtk3-demo.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "gtk3-icon-browser.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "gtk3-widget-factory.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "usr_local_lib_qt5_bin_assistant.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "usr_local_lib_qt5_bin_designer.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "x11vnc.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "xterm.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;

    pkg check --recompute gtk3 qt5-assistant qt5-designer x11vnc xterm

# Создание нужных ярлыков в меню программ

    echo "Writing applications .desktop files:"
    cd /usr/local/share/ulbsd/compile/apps
    cp -Rvi * /usr/local/share/applications/

    find /home /root -depth -name '.cache' -exec sh -c 'rm -R {}/*' \;

    /bin/csh -c rehash

    pkg check --recompute libreoffice ssvnc wifimgr


elif [ "${1}" == "cfgs" ]
then

# Восстановление нужных конфигурационных файлов

    echo "Overwriting existing configuration files:"
    cd /usr/local/share/ulbsd/compile/cfgs
    cp -Rvi * /

    pkg check --recompute sddm plasma5-kinfocenter plasma5-plasma-workspace

# Маскировка ненужных ярлыков kservices

    DIR='/usr/local/share/kservices5/'
    find "${DIR}" -name "wayland.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "kcm_printer_manager.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}" -name "kcm_kaccounts.desktop" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;

    pkg check --recompute plasma5-kinfocenter print-manager kaccounts-integration

# Удаление встроенных программ печати

    BIN='/usr/bin/'
    LBIN='/usr/local/bin/'
    cups_installed=`pkg info | grep cups`
    if [ "$cups_installed" != "" ]
    then
        find "${BIN}" -name "lp" -type f -size +0c -exec sh -c 'mv -fv {} {}_ ; echo Moved {}' \;
        find "${BIN}" -name "lpq" -type f -size +0c -exec sh -c 'mv -fv {} {}_ ; echo Moved {}' \;
        find "${BIN}" -name "lpr" -type f -size +0c -exec sh -c 'mv -fv {} {}_ ; echo Moved {}' \;
        find "${BIN}" -name "lprm" -type f -size +0c -exec sh -c 'mv -fv {} {}_ ; echo Moved {}' \;
        ln -sv ${LBIN}lp ${BIN}lp
        ln -sv ${LBIN}lpq ${BIN}lpq
        ln -sv ${LBIN}lpr ${BIN}lpr
        ln -sv ${LBIN}lprm ${BIN}lprm
    else
        echo "Please install the 'cups' package manually, then run 'ulbsd-update cfgs' again"
    fi

# Создание ссылки для печати из wine

    if [ -f /usr/local/etc/printcap ]
    then
        rm -v /etc/printcap
        ln -sv /usr/local/etc/printcap /etc/printcap
    fi

# Маскировка иконок knemo из papirus-icon-theme

    DIR='/usr/local/share/icons'
    find "${DIR}/ePapirus/" -name "knemo-*.svg" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}/Papirus/" -name "knemo-*.svg" -type f -size +0c -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}/Papirus-Dark/" -name "knemo-*.svg" -size +0c -type f -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;
    find "${DIR}/Papirus-Light/" -name "knemo-*.svg" -size +0c -type f -exec sh -c 'cp -fv {} {}_ ; truncate -s 0 {} ; echo Truncated {}' \;

    pkg check --recompute papirus-icon-theme

elif [ "${1}" == "patches" ]
then

# Патч для монтирования NTFS штатной командой mount

    cd /usr/src/sbin/mount/
    patch -f mount.c /usr/local/share/ulbsd/patches/mount.c.patch
    make all install clean

# Патч для исправления глюков на загрузочном экране

    cd /boot/lua/
    patch -f drawer.lua /usr/local/share/ulbsd/patches/drawer.lua.patch

# Патчи и русификация для bsdconfig

    cd /usr/libexec/bsdconfig/070.usermgmt/
    patch -f userdel /usr/local/share/ulbsd/patches/bsdconfig/usermgmt/userdel.patch
    patch -f useredit /usr/local/share/ulbsd/patches/bsdconfig/usermgmt/useredit.patch
    cd /usr/libexec/bsdconfig/070.usermgmt/include/
    patch -f messages.subr /usr/local/share/ulbsd/patches/bsdconfig/usermgmt/include/messages.subr.patch
    cd /usr/share/bsdconfig/
    patch -f dialog.subr /usr/local/share/ulbsd/patches/bsdconfig/share/dialog.subr.patch
    cd /usr/share/bsdconfig/usermgmt/
    patch -f group_input.subr /usr/local/share/ulbsd/patches/bsdconfig/share/group_input.subr.patch
    cd /usr/share/bsdconfig/networking/
    patch -f netmask.subr /usr/local/share/ulbsd/patches/bsdconfig/share/netmask.subr.patch
    cd /usr/share/bsdconfig/
    patch -f strings.subr /usr/local/share/ulbsd/patches/bsdconfig/share/strings.subr.patch
    cd /usr/local/share/ulbsd/patches/bsdconfig/usermgmt/
    cp -fv INDEX.ru_RU.UTF-8 /usr/libexec/bsdconfig/070.usermgmt/
    cd /usr/local/share/ulbsd/patches/bsdconfig/usermgmt/include
    cp -fv messages.subr.ru_RU.UTF-8 /usr/libexec/bsdconfig/070.usermgmt/include/
    cd /usr/local/share/ulbsd/patches/bsdconfig/networking/
    cp -fv INDEX.ru_RU.UTF-8 /usr/libexec/bsdconfig/120.networking/
    cd /usr/local/share/ulbsd/patches/bsdconfig/networking/include
    cp -fv messages.subr.ru_RU.UTF-8 /usr/libexec/bsdconfig/120.networking/include/

elif [ "${1}" == "check" ]
then

    release=`uname -r | cut -d '-' -f1`
    cur_branch=`uname -r | cut -d '-' -f2,3`
    new_branch=`fetch -qo - http://svn.freebsd.org/base/releng/${release}/sys/conf/newvers.sh | grep -E "BRANCH=" | head -1 | cut -d '"' -f2`
    if [ "${cur_branch}" == "${new_branch}" ]
    then
        echo No update required.
    else
        echo Update required. ${new_branch} available.
    fi

else

    /usr/sbin/freebsd-update $*

fi
