#! /bin/sh
### BEGIN INIT INFO
# Provides:          bodhibuilder-firstboot
# Required-Start:    $remote_fs $syslog $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Run firstboot items for bodhibuilder after a remastered system has been installed
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
    #REM302
    if [ "`cat /proc/cmdline | grep casper`" = "" ]; then # run thru this if it's not an ISO
        [ "$VERBOSE" != no ] && log_begin_msg "Running bodhibuilder-firstboot"
        # Removes this script from boot:
        (sleep 60 && update-rc.d -f bodhibuilder-firstboot remove) &
        sed -i -e 's/root:x:/root:!:/g' /etc/shadow
        ## Ubiquity
            # Remove ubiquity icon from desktop
            yes | rm -rf /home/*/Desktop/ubiquity*.desktop
            yes | rm -rf /home/*/Desktop/UBIQUITY*.desktop
            yes | rm -rf /usr/share/applications/UBIQUITY.desktop
            # Remove ubiquity from menus
            sed -i -e 's/NoDisplay=false/NoDisplay=true/' /usr/share/applications/ubiquity.desktop
        # Create bodhibuilder desktop file for menus
        yes | cp -f /etc/bodhibuilder/applications/bodhibuilder-gtk.desktop /usr/share/applications/

        # Add custom repositories to the installed OS, if they're not already there
        #~ cat /etc/bodhibuilder/apt/custom_repos >> /etc/apt/sources.list && echo '' > /etc/bodhibuilder/apt/custom_repos
        custom_repos_content=`cat /etc/bodhibuilder/apt/custom_repos | grep -i --color=none [a-z]`
        oldifs=$IFS # change the delimiter to 'newline' to use in the "for" loop
        IFS=$'\n' # change the delimiter to 'newline' to use in the "for" loop
        for insert in ${custom_repos_content} ; do
          insert_exists=`grep ${insert} /etc/apt/sources.list`
          [ ! "${insert_exists}" ] && echo ${insert} >>/etc/apt/sources.list
        done
        IFS=$oldifs # reset the delimiter back to its original default
        # blank out custom_repos file for next spin, cuz desired repos should be in sources.list now
        echo '' > /etc/bodhibuilder/apt/custom_repos

        # Replace OS grub with bodhibuilder's custom grub
        yes | cp -f /etc/default/grub.bodhi /etc/default/grub
        update-grub

        ################################################
        ### CUSTOM COMMANDS
        #
        # NOTE: Please do not add any custom repositories
        #       or comments for them here.
        #       Instead, put them into the file:
        #         /etc/bodhibuilder/apt/custom_repos
        #       This script will append everything in that
        #       file to the existing /etc/apt/sources.list
        #       file. Thanks.
        #
        # Commands entered into this section will be run only when
        # after the system has been installed and booted for the
        # first time, and never again.
        #
        ### Place your custom commands below this line
        
        ### Place your custom commands above this line
        ################################################
        
        ES=$?
        [ "$VERBOSE" != no ] && log_end_msg $ES
        return $ES
    else
        logger Did not run thru bodhibuilder-firstboot code
    fi
}


case "$1" in
    start)
        do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

