From 04ecdb96ec2e589a8cc32c105cc389cf36670e6b Mon Sep 17 00:00:00 2001
From: Michael McMahon <michael.steven.mcmahon@gmail.com>
Date: Wed, 15 Nov 2017 12:55:00 -0500
Subject: [PATCH] Add vim bundle

---
 src/.config                                 |  9 ++-
 src/README                                  |  6 ++
 src/minimal_overlay/bundles/vim/01_get.sh   | 41 ++++++++++++++
 src/minimal_overlay/bundles/vim/02_build.sh | 61 +++++++++++++++++++++
 src/minimal_overlay/bundles/vim/bundle.sh   |  9 +++
 5 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100755 src/minimal_overlay/bundles/vim/01_get.sh
 create mode 100755 src/minimal_overlay/bundles/vim/02_build.sh
 create mode 100755 src/minimal_overlay/bundles/vim/bundle.sh

diff --git a/src/.config b/src/.config
index 23f091f50..aa769f654 100644
--- a/src/.config
+++ b/src/.config
@@ -116,6 +116,12 @@ STATIC_GET_SOURCE_URL=http://s.minos.io/s
 #
 UTIL_LINUX_SOURCE_URL=https://www.kernel.org/pub/linux/utils/util-linux/v2.31/util-linux-2.31.tar.gz
 
+# You can find the latest vim source bundles here:
+#
+#   https://github.com/vim/vim/releases
+#
+VIM_SOURCE_URL=https://github.com/vim/vim/archive/v8.0.1298.tar.gz
+
 # You can find the latest ZLIB source bundles here:
 #
 #   http://zlib.net
@@ -213,11 +219,12 @@ COPY_SOURCE_ISO=true
 # openjdk    - installs Open JDK. All operations are automated.
 # static_get - portable binaries for Linux (http://s.minos.io).
 # util_linux - set of executable utilities distributed by the Linux Kernel Org.
+# vim        - an advanced text editor.
 # zlib       - DEFLATE compression/decompression library.
 #
 # Refer to the README file for more information.
 #
-#OVERLAY_BUNDLES=glibc_full,cf_cli,dhcp,dropbear,felix,java,links,lua,mll_utils,nano,ncurses,nweb,static_get,util_linux,zlib
+#OVERLAY_BUNDLES=glibc_full,cf_cli,dhcp,dropbear,felix,java,links,lua,mll_utils,nano,ncurses,nweb,static_get,util_linux,vim,zlib
 #OVERLAY_BUNDLES=glibc_full,dhcp,felix,links,openjdk,zlib
 
 # This property enables the standard penguin boot logo in the upper left corner
diff --git a/src/README b/src/README
index d4ac800e4..8a92d1d34 100644
--- a/src/README
+++ b/src/README
@@ -84,6 +84,12 @@ Currently available overlay bundles:
 
                Some packages in this overlay bundle require ncurses.
 
+* vim        - An advanced text editor that seeks to provide the power of
+               the de-facto Unix editor 'Vi', with a more complete feature
+               set.
+
+               This package will create symlinks on top of vi.
+
 * ZLIB       - Software library used for data compression.
 
 
diff --git a/src/minimal_overlay/bundles/vim/01_get.sh b/src/minimal_overlay/bundles/vim/01_get.sh
new file mode 100755
index 000000000..2a3369e2a
--- /dev/null
+++ b/src/minimal_overlay/bundles/vim/01_get.sh
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+SRC_DIR=$(pwd)
+
+. ../../common.sh
+
+# Grab everything after the '=' character.
+DOWNLOAD_URL=$(grep -i VIM_SOURCE_URL $MAIN_SRC_DIR/.config | cut -f2 -d'=')
+
+# Grab everything after the last '/' character.
+ARCHIVE_FILE=${DOWNLOAD_URL##*/}
+
+# Read the 'USE_LOCAL_SOURCE' property from '.config'
+USE_LOCAL_SOURCE="$(grep -i USE_LOCAL_SOURCE $MAIN_SRC_DIR/.config | cut -f2 -d'=')"
+
+if [ "$USE_LOCAL_SOURCE" = "true" -a ! -f $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE  ] ; then
+  echo "Source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE is missing and will be downloaded."
+  USE_LOCAL_SOURCE="false"
+fi
+
+cd $MAIN_SRC_DIR/source/overlay
+
+if [ ! "$USE_LOCAL_SOURCE" = "true" ] ; then
+  # Downloading vim source bundle file. The '-c' option allows the download to resume.
+  echo "Downloading vim source bundle from $DOWNLOAD_URL"
+  wget -c $DOWNLOAD_URL
+else
+  echo "Using local vim source bundle $MAIN_SRC_DIR/source/overlay/$ARCHIVE_FILE"
+fi
+
+# Delete folder with previously extracted vim.
+echo "Removing vim work area. This may take a while..."
+rm -rf $WORK_DIR/overlay/vim
+mkdir $WORK_DIR/overlay/vim
+
+# Extract vim to folder 'work/overlay/vim'.
+# Full path will be something like 'work/overlay/vim/vim-8.0.1298'.
+tar -xvf $ARCHIVE_FILE -C $WORK_DIR/overlay/vim
+
+cd $SRC_DIR
+
diff --git a/src/minimal_overlay/bundles/vim/02_build.sh b/src/minimal_overlay/bundles/vim/02_build.sh
new file mode 100755
index 000000000..20a7c8c1a
--- /dev/null
+++ b/src/minimal_overlay/bundles/vim/02_build.sh
@@ -0,0 +1,61 @@
+#!/bin/bash
+
+SRC_DIR=$(pwd)
+
+. ../../common.sh
+
+cd $WORK_DIR/overlay/vim
+
+DESTDIR="$PWD/vim_installed"
+
+# Change to the vim source directory which ls finds, e.g. 'vim-8.0.1298'.
+cd $(ls -d vim-*)
+
+echo "Preparing vim work area. This may take a while..."
+make -j $NUM_JOBS clean
+
+rm -rf $DESTDIR
+
+echo "Setting vimrc location..."
+echo '#define SYS_VIMRC_FILE "/etc/vimrc"' >> src/feature.h
+
+echo "Configuring vim..."
+CFLAGS="$CFLAGS" ./configure \
+  --prefix=/usr
+
+echo "Building vim..."
+make -j $NUM_JOBS
+
+echo "Installing vim..."
+make -j $NUM_JOBS install DESTDIR=$DESTDIR
+
+echo "Generating vimrc..."
+mkdir -p $DESTDIR/etc
+cat > $DESTDIR/etc/vimrc << "EOF"
+" Begin /etc/vimrc
+
+set nocompatible
+set backspace=2
+set mouse=r
+syntax on
+set background=dark
+
+" End /etc/vimrc
+EOF
+
+echo "Symlinking vim to vi..."
+ln -sv vim $DESTDIR/usr/bin/vi
+mkdir -p $DESTDIR/bin
+ln -sv vim $DESTDIR/bin/vi
+
+echo "Reducing vim size..."
+strip -g $DESTDIR/usr/bin/*
+
+ROOTFS="$WORK_DIR/src/minimal_overlay/rootfs"
+
+cp -r $DESTDIR/* $ROOTFS
+
+echo "vim has been installed."
+
+cd $SRC_DIR
+
diff --git a/src/minimal_overlay/bundles/vim/bundle.sh b/src/minimal_overlay/bundles/vim/bundle.sh
new file mode 100755
index 000000000..ffd32c002
--- /dev/null
+++ b/src/minimal_overlay/bundles/vim/bundle.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+SRC_DIR=$(pwd)
+
+./01_get.sh
+./02_build.sh
+
+cd $SRC_DIR
+