#!/usr/bin/env bash

unset ROOTFS && [ -d ${ROOTFS}usr/lib/ublinux ] || ROOTFS=/

#/etc/rc.d
INITD=${ROOTFS}etc/init.d
[ -d $INITD ] || INITD=${ROOTFS}etc/rc.d/init.d
[ -d $INITD ] && find  ${ROOTFS}usr/lib/ublinux/rc.d/init.d  -type f | sed s%${ROOTFS}usr/lib/ublinux/rc.d/init.d/%% | while read a; do
  ln -sf /usr/lib/ublinux/rc.d/init.d/${a} ${INITD}
done

#systemd
SYSTEMDFILES=${ROOTFS}usr/lib/systemd/system
[ -d $SYSTEMDFILES ] || SYSTEMDFILES=${ROOTFS}lib/systemd/system
[ -d $SYSTEMDFILES ] && find ${ROOTFS}usr/lib/ublinux/systemd -type f | sed s%${ROOTFS}usr/lib/ublinux/systemd/%% | while read a; do
  ln -sf /usr/lib/ublinux/systemd/$a $SYSTEMDFILES
done
[ -d $SYSTEMDFILES ] && ls -1d ${ROOTFS}usr/lib/ublinux/systemd/*.wants | sed s%${ROOTFS}usr/lib/ublinux/systemd/%% | while read a; do
  mkdir -p $SYSTEMDFILES/$a
  cp -df ${ROOTFS}usr/lib/ublinux/systemd/$a/* $SYSTEMDFILES/$a
done

ln -sf /usr/lib/ublinux/rc.d/rc.xorg ${ROOTFS}etc/X11/xinit/xinitrc.d/00-ublinux-rc-xorg

cat > ${ROOTFS}etc/xdg/autostart/ublinux-rc.desktop << EOF
[Desktop Entry]
Categories=
Comment=Run applications at system start
Comment[ru]=Автозапуск приложений при старте
Exec=/usr/lib/ublinux/rc.d/rc.desktop
GenericName[ru]=
GenericName=
Icon=
MimeType=
Name=UBLinux autoexec
Name[ru]=Автозапуск UBLinux
Path=
StartupNotify=false
Terminal=false
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=unique
X-KDE-StartupNotify=false
X-KDE-SubstituteUID=false
X-KDE-Username=
EOF

exit 0
