Article Series

This is a multiple-parts article. There are few sections here.

Where to Discuss?

Local Group

Bashful Bot

Goal: Deploy BASH Project to System Wide

Previous Guidance

We’re already see a finished bash project in previous article. We should finish what we have done.

Issue

The need of Example

Installing part could be deep mistery to beginner. Although this looks tricky, actually it is easy. All we need is a Makefile sample.


Source Files

What we want to achieve, is copy our script from any location.

Here are our source looks like

% ls
Makefile         cupubot-bash   modular-tutorial
config.bash      main.bash      options.bash
controller.bash  messages.bash  tasks

BASH: Telegram Bot: Makefile Source

cupubot-bash is our main launcher

#!/usr/bin/env bash
# redirect script to library

DIR=$(dirname "$0")
DIR=${DIR}/../lib/cupubot
. ${DIR}/main.bash

To enable this, we need to alter main.bash a bit.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

. ${DIR}/config.bash
. ${DIR}/functions.bash
. ${DIR}/messages.bash
. ${DIR}/options.bash

We are no longer can use use DIR=$(dirname “$0”). Because our main path has been changed, from the same path directory with main.bash, to different directory. It will be called from /usr/local/bin.


Destination Files

From Source to Destination

What we want to achieve, is copy our script from any location, to a particular directory.

  • /usr/local/bin

  • /usr/local/lib

To do this, let’s create a Makefile

% touch Makefile

And edit with your favorite editor [geany, kate, nano, vim]. The choice is actually up to you, as long as you can add this line

PREFIX ?= /usr/local
bindir        = $(PREFIX)/bin
libdir        = $(PREFIX)/lib

Install

More Detail Please

What we want to achieve, is copy our script from any location, to a particular directory.

  • /usr/local/bin: cupubot-bash

  • /usr/local/lib: *.bash

Talk is cheap, here is the install target line code:

install:
	@echo "Installing..."
	
	# Scripts
	@install -D -m755 ./cupubot-bash $(DESTDIR)$(bindir)/cupubot-bash
	
	# Libs
	@mkdir -p $(DESTDIR)$(libdir)/cupubot/tasks
	@install -D -m644 ./*.bash $(DESTDIR)$(libdir)/cupubot
	@install -D -m644 ./tasks/*.bash $(DESTDIR)$(libdir)/cupubot/tasks

It is pretty clear and self explanatory.

Action

Consider, run the command:

% sudo make install
Password: 
Installing...
# Scripts
# Libs

BASH: Telegram Bot: Makefile Install

Result

Now here it is the result.

% ls /usr/local/bin/cupu*  /usr/local/lib/cupubot/*
/usr/local/bin/cupubot-bash
/usr/local/lib/cupubot/config.bash
/usr/local/lib/cupubot/controller.bash
/usr/local/lib/cupubot/main.bash
/usr/local/lib/cupubot/messages.bash
/usr/local/lib/cupubot/options.bash

/usr/local/lib/cupubot/tasks:
logger_html.bash  new_member.bash  reply.bash
logger_text.bash  observe.bash

BASH: Telegram Bot: Makefile Destination

Note that you can see the color changes. Only one file is executable.


Uninstall

Reverse, Undo

Respectively, there is also uninstall target line code.

uninstall:
	@echo "Uninstalling..."
	
	# Scripts
	@rm $(DESTDIR)$(bindir)/cupubot-bash
	
	# Libs
	@rm -r $(DESTDIR)$(libdir)/cupubot

That above is also self explanatory.

Action

Consider, run the command:

% sudo make uninstall
Uninstalling...
# Scripts
# Libs

BASH: Telegram Bot: Makefile Uninstall


System Wide Project

Now you can test to see if the install works.

% cupubot-bash --help
usage:  cupubot [options]
operations:
 general
   -v, --version    display version information
   -h, --help       display help information

BASH: Telegram Bot: Makefile System Wide


Complete Code

For you as an example. Including .PHONY.

I also give a dummy doc, as default option for make.

##
# cupubot-bash
# Cupubot is a Telegram bot. Just another learning project
##

PREFIX ?= /usr/local
bindir        = $(PREFIX)/bin
libdir        = $(PREFIX)/lib
mandir        = $(PREFIX)/man

# default target
all: doc

doc: 
	@echo "Manual Page..."
	@gzip -k -f ./man/cupubot-bash.1

# auxiliary
install:
	@echo "Installing..."
	# Scripts
	@install -D -m755 ./cupubot-bash $(DESTDIR)$(bindir)/cupubot-bash
	# Libs
	@mkdir -p $(DESTDIR)$(libdir)/cupubot/tasks
	@install -D -m644 ./*.bash $(DESTDIR)$(libdir)/cupubot
	@install -D -m644 ./tasks/*.bash $(DESTDIR)$(libdir)/cupubot/tasks
	# Documentation
	@gzip -k -f ./man/cupubot-bash.1
	@install -D -m644 ./man/cupubot-bash.1.gz $(DESTDIR)$(mandir)/man1/

uninstall:
	@echo "Uninstalling..."
	# Scripts
	@rm $(DESTDIR)$(bindir)/cupubot-bash
	# Libs
	@rm -r $(DESTDIR)$(libdir)/cupubot
	# Documentation
	@rm $(DESTDIR)$(mandir)/man1/cupubot-bash.1.gz

clean:
	@echo "Cleaning..."

.PHONY: install uninstall clean

Source:

I think that’s all.


What is Next ?

We will make the script more usable for other people, by adding Manual Page for documentation.

Thank you for reading.