#!/bin/bash -e

# /boot
install -m 644 files/boot/config.txt                                    "${ROOTFS_DIR}/boot/"
install -d "${ROOTFS_DIR}/boot/crankshaft"
install -m 644 files/boot/crankshaft/gpio2kbd.cfg                       "${ROOTFS_DIR}/boot/crankshaft/"
install -m 644 files/boot/crankshaft/openauto.ini                       "${ROOTFS_DIR}/boot/crankshaft/"
install -m 644 files/boot/crankshaft/openauto-logs.ini                  "${ROOTFS_DIR}/boot/crankshaft/"
install -m 644 files/boot/crankshaft/startup.py                         "${ROOTFS_DIR}/boot/crankshaft/"
install -m 644 files/boot/crankshaft/startup.sh                         "${ROOTFS_DIR}/boot/crankshaft/"
install -m 644 files/boot/crankshaft/triggerhappy.conf                  "${ROOTFS_DIR}/boot/crankshaft/"
install -m 755 files/opt/crankshaft/crankshaft_default_env.sh           "${ROOTFS_DIR}/boot/crankshaft/crankshaft_env.sh"
install -d "${ROOTFS_DIR}/boot/crankshaft/custom"

# /etc
install -d "${ROOTFS_DIR}/etc/systemd/system/systemd-udevd.service.d"
install -m 644 files/etc/pulse/csng_daemon.conf                         "${ROOTFS_DIR}/etc/pulse/"
install -m 644 files/etc/pulse/csng_default.pa                          "${ROOTFS_DIR}/etc/pulse/"
install -m 644 files/etc/pulse/csng_system.pa                           "${ROOTFS_DIR}/etc/pulse/"
install -m 644 files/etc/systemd/system/openauto.service                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/btservice.service               "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/crankshaft.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/daymode.service                 "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/daymode.timer                   "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/devmode.service                 "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/debugmode.service               "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/disconnect.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/disconnect.timer                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/display.service                 "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/gpio2kbd.service                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/gpiotrigger.service             "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/shutdown.service                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/shutdown.timer                  "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/tap2wake.service                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/timerstart.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/update.service                  "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/update.timer                    "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/usbrestore.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/usbdetect.service               "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/usbunmount.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/rpicamserver.service            "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/hwclock-load.service            "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/nightmode.service               "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/nightmode.timer                 "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/user_startup.service            "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/hotspot.service                 "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/hotspot-monitor.service         "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/regensshkeys.service            "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/custombrightness.service        "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/pulseaudio.service              "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/pulseaudio-debug.service        "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/alsastaterestore.service        "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/lightsensor.service             "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/i2ccheck.service                "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/wpa-monitor.service             "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/etc/systemd/system/systemd-udevd.service.d/override.conf "${ROOTFS_DIR}/etc/systemd/system/systemd-udevd.service.d"
install -m 644 files/etc/udev/rules.d/10-gpio2kbd.rules                 "${ROOTFS_DIR}/etc/udev/rules.d/"
install -m 644 files/etc/udev/rules.d/51-android.rules                  "${ROOTFS_DIR}/etc/udev/rules.d/"
install -m 644 files/etc/udev/rules.d/51-android.master                 "${ROOTFS_DIR}/etc/udev/rules.d/"
install -m 644 files/etc/udev/rules.d/10-automount.rules                "${ROOTFS_DIR}/etc/udev/rules.d/"
install -m 755 files/etc/rc.local                                       "${ROOTFS_DIR}/etc/"
install -m 644 files/etc/issue                                          "${ROOTFS_DIR}/etc/"
install -m 644 files/etc/issue.net                                      "${ROOTFS_DIR}/etc/"
install -m 644 files/etc/motd                                           "${ROOTFS_DIR}/etc/"
install -m 644 files/etc/rsyslog.d/disable-logspam.conf                 "${ROOTFS_DIR}/etc/rsyslog.d/"

install -d "${ROOTFS_DIR}/etc/initramfs-tools/conf.d"
install -d "${ROOTFS_DIR}/etc/initramfs-tools/hooks"
install -d "${ROOTFS_DIR}/etc/initramfs-tools/scripts/local-top"
install -m 644 files/etc/initramfs-tools/conf.d/local.conf              "${ROOTFS_DIR}/etc/initramfs-tools/conf.d/"
install -m 755 files/etc/initramfs-tools/hooks/pv                       "${ROOTFS_DIR}/etc/initramfs-tools/hooks/"
install -m 755 files/etc/initramfs-tools/scripts/local-top/local.sh     "${ROOTFS_DIR}/etc/initramfs-tools/scripts/local-top/"

install -d "${ROOTFS_DIR}/etc/hostapd/"
install -m 644 files/etc/default/hostapd                                "${ROOTFS_DIR}/etc/default/"
install -m 644 files/etc/hostapd/hostapd.conf                           "${ROOTFS_DIR}/etc/hostapd/"
install -m 644 files/etc/dnsmasq.conf                                   "${ROOTFS_DIR}/etc/"

install -d "${ROOTFS_DIR}/etc/samba"
install -m 644 files/etc/samba/smb.conf                                 "${ROOTFS_DIR}/etc/samba/"

# /opt
install -d "${ROOTFS_DIR}/opt/crankshaft"
install -m 755 files/opt/crankshaft/crankshaft_default_env.sh           "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/crankshaft_system_env.sh            "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_crankshaft.sh               "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/cameraserver.py                     "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/cameracontrol.py                    "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_debugmode.sh                "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_devmode.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_display.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_daynight.sh                 "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_gpiotrigger.sh              "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_openauto.sh                 "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_tap2wake.sh                 "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_timerstart.sh               "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_wifisetup.sh                "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_usbrestore.sh               "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_usbdetect.sh                "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_usbunmount.sh               "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_user_startup.sh             "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_hotspot.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_hostapdchange.sh            "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_custombrightness.sh         "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_pacheck.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_hwclock.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_lightsensor.py              "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_i2ccheck.sh                 "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/service_wpachange.sh                "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/gpio2kbd                            "${ROOTFS_DIR}/opt/crankshaft/"
install -m 644 files/opt/crankshaft/triggerhappy.conf                   "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/usb_action.sh                       "${ROOTFS_DIR}/opt/crankshaft/"
install -m 755 files/opt/crankshaft/aa_device_state.sh                  "${ROOTFS_DIR}/opt/crankshaft/"
install -m 644 files/opt/crankshaft/start_order                         "${ROOTFS_DIR}/opt/crankshaft/"

install -d "${ROOTFS_DIR}/opt/crankshaft/wallpaper"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper.png                   "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-night.png             "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-classic.png           "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-classic-night.png     "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-christmas.png         "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-firework.png          "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/wallpaper/camera-overlay.png              "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/opt/crankshaft/cam_overlay/overlay.png                   "${ROOTFS_DIR}/opt/crankshaft/wallpaper/usbcamera-overlay.png"
install -m 644 files/opt/crankshaft/wallpaper/wallpaper-eq.png                "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"

install -d "${ROOTFS_DIR}/opt/crankshaft/cam_overlay"
install -m 755 files/opt/crankshaft/cam_overlay/cam_overlay.bin   "${ROOTFS_DIR}/opt/crankshaft/cam_overlay/"

install -m 644 files/opt/crankshaft/cam_overlay/shader-YUYV.frag  "${ROOTFS_DIR}/opt/crankshaft/cam_overlay/"
install -m 644 files/opt/crankshaft/cam_overlay/shader.frag       "${ROOTFS_DIR}/opt/crankshaft/cam_overlay/"
install -m 644 files/opt/crankshaft/cam_overlay/shader.vert       "${ROOTFS_DIR}/opt/crankshaft/cam_overlay/"

# /usr
install -m 755 files/usr/local/bin/autoapp                              "${ROOTFS_DIR}/usr/local/bin/"
install -m 755 files/usr/local/bin/btservice                            "${ROOTFS_DIR}/usr/local/bin/"
install -m 755 files/usr/local/bin/crankshaft                           "${ROOTFS_DIR}/usr/local/bin/"
install -m 755 files/usr/local/bin/autoapp_helper                       "${ROOTFS_DIR}/usr/local/bin/"
install -m 755 files/usr/local/lib/libaasdk.so                          "${ROOTFS_DIR}/usr/local/lib/"
install -m 755 files/usr/local/lib/libaasdk_proto.so                    "${ROOTFS_DIR}/usr/local/lib/"
install -m 755 files/usr/local/bin/usbreset                             "${ROOTFS_DIR}/usr/local/bin/"

# plymouth
install -d "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft"
install -m 644 files/usr/share/plymouth/themes/crankshaft/crankshaft.plymouth   "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -m 644 files/usr/share/plymouth/themes/crankshaft/crankshaft.script     "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -m 644 files/usr/share/plymouth/themes/crankshaft/splash.png            "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -m 644 files/usr/share/plymouth/themes/crankshaft/shutdown.png          "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -m 644 files/usr/share/plymouth/themes/crankshaft/progress_bar.png      "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -m 644 files/usr/share/plymouth/themes/crankshaft/progress_box.png      "${ROOTFS_DIR}/usr/share/plymouth/themes/crankshaft/"
install -d "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation"
install -m 644 files/usr/share/plymouth/themes/csnganimation/csnganimation.plymouth   "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/csnganimation.script     "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/box.png                  "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/bullet.png               "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/entry.png                "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/lock.png                 "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/logo.png                 "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/progress_bar.png         "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/share/plymouth/themes/csnganimation/progress_box.png         "${ROOTFS_DIR}/usr/share/plymouth/themes/csnganimation/"
install -m 644 files/usr/lib/arm-linux-gnueabihf/plymouth/csnganimation.so            "${ROOTFS_DIR}/usr/lib/arm-linux-gnueabihf/plymouth/"
install -d "${ROOTFS_DIR}/usr/share/plymouth/themes/custom"
install -m 644 files/usr/share/plymouth/themes/custom/custom.plymouth       "${ROOTFS_DIR}/usr/share/plymouth/themes/custom/"
install -m 644 files/usr/share/plymouth/themes/custom/custom.script         "${ROOTFS_DIR}/usr/share/plymouth/themes/custom/"
install -m 644 files/usr/share/plymouth/themes/custom/progress_bar.png      "${ROOTFS_DIR}/usr/share/plymouth/themes/custom/"
install -m 644 files/usr/share/plymouth/themes/custom/progress_box.png      "${ROOTFS_DIR}/usr/share/plymouth/themes/custom/"
install -m 644 files/usr/share/plymouth/themes/custom/splash.png            "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"
install -m 644 files/usr/share/plymouth/themes/custom/shutdown.png          "${ROOTFS_DIR}/opt/crankshaft/wallpaper/"

# /lib
install -m 755 files/lib/udev/hwclock-set                               "${ROOTFS_DIR}/lib/udev/"

# custom resolv.conf hook
install -m 644 files/lib/dhcpcd/dhcpcd-hooks/20-resolv.conf             "${ROOTFS_DIR}/lib/dhcpcd/dhcpcd-hooks/"

# custom dhcpcd service withh added pre wifisetup
install -m 644 files/lib/systemd/system/dhcpcd.service                  "${ROOTFS_DIR}/lib/systemd/system/"

#qt5
tar -xf files/qt5/Qt5_OpenGLES2.tar.xz -C ${ROOTFS_DIR}/

#wiringpi
install -m 755 files/wiringpi-latest.deb                               "${ROOTFS_DIR}/root/"