#!/bin/sh

########## ##########
########## ##########
##      ## ##
##         ##
##         ##         - RC BOOT SCRIPTS MANAGER FOR SIMPLE LINUX 
##         ##         - AUTHOR -- TOLA MIRONCENKO
##         ##
##         ##
##         ##########
##         ##########

#----- colors -------
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
Bred='\033[1;31m' 
Yellow='\033[1;33m'
White='\033[0;37m'
Bwhite='\033[1;37m' 
#--------------------

rootdir=/

#------------------------ add to boot function --------------
add() {
    resultlink=$(cat /usr/share/rc/ordernumbers | grep $1)
    if [ -f /etc/init.d/$1 ] && [ $resultlink ]; then
        cd /etc/rc.d 
        ln -sv ../init.d/$1 "$resultlink" &> /dev/null
        echo "added $1 to init."
    else
        help
    fi
}
#------------------------------------------------------------

#--------------------- del from boot function ---------------
del() {
    filetoremove=$(find /etc/rc.d/ | grep $1 )
    if [ $filetoremove ]; then
        rm -v $filetoremove &> /dev/null
        echo "removed $1 from init."
    else
        help
    fi
}
#------------------------------------------------------------

#---------------------- start daemon function ---------------
start() {
    if [ -f /etc/init.d/$1 ]; then
        /etc/init.d/$1 start
    else
        help
    fi
}
#------------------------------------------------------------

#---------------- stop daemon function ----------------------
stop() {
    if [ -f /etc/init.d/$1 ]; then
        /etc/init.d/$1 stop
    else
        help
    fi
}
#------------------------------------------------------------

#------------- restart daemon function ----------------------
restart() {
    if [ -f /etc/init.d/$1 ]; then
        /etc/init.d/$1 restart
    else
        help
    fi
}
#------------------------------------------------------------

#------------------ list services ---------------------------
list() {
    ls --color $rootdir/etc/init.d
}
#------------------------------------------------------------

#------------------ list enabled services -------------------
bootlist() {
    ls --color $rootdir/etc/rc.d
}
#------------------------------------------------------------

#------------------ service status --------------------------
status() {
    if [[ "$1" == "" ]]; then
        help
        exit 0
    fi
    if [[ "$(ls /etc/init.d | grep $1)" == ""  ]]; then
        printf "$Yellow**$Bred error:$Color_off can't find service /etc/init.d/$1\n"
        exit 1
    fi
    printf "$Yellow**$Green $1$Color_off\n"
    rcdservice=$(ls $rootdir/etc/rc.d | grep $1 2> /dev/null)
    if [[ "$rcdservice" == "" ]]; then
        printf "Loaded:$Yellow unloaded$Color_off (/etc/rc.d)\n"
    else
        printf "Loaded:$Green loaded$Color_off (/etc/rc.d/$rcdservice)\n"
    fi
    servicepid=$(ls $rootdir/var/run | grep $1)
    if [[ "$servicepid" == "" ]]; then
        printf "Active: deactive (${Yellow}deactive$Color_off)\n"
    else
        printf "Active: active (${Green}running$Color_off); pid (/var/run/$servicepid)\n"
    fi
}
#------------------------------------------------------------

#-------------- print help function -------------------------
help() {
    echo "
            ** rc usage:
            rc add [init.d script name] - add service on boot
            rc del [init.d script name] - delete service from boot
            rc start [init.d script name] - start service
            rc stop [init.d script name] - stop service
            rc restart [init.d script name] - restart service
            rc list - list all services
            rc bootlist - list enabled services
            rc status [init.d script name] - check service status

            examples:
            rc add httpd
            rc del httpd  
            rc start httpd
            rc stop httpd
            rc restart httpd
            rc list
            rc bootlist
        "
}
#------------------------------------------------------------

command="$1"

case ${command} in
    add)
        add $2 $3
        ;;
    del)
        del $2
        ;;
    start)
        start $2
        ;;
    stop)
        stop $2
        ;;
    restart)
        restart $2
        ;;
    list)
        list
        ;;
    bootlist)
        bootlist
        ;;
    status)
        status $2
        ;;
    *)
        help
        ;;
esac
