#!/bin/bash
# ##############################################################################
# ############################### JLIVECD ######################################
# ##############################################################################
#            Copyright (c) 2015-2017 Md. Jahidul Hamid
# 
# -----------------------------------------------------------------------------
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
# 
#     * The names of its contributors may not be used to endorse or promote 
#       products derived from this software without specific prior written
#       permission.
#       
# Disclaimer:
# 
#     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#     POSSIBILITY OF SUCH DAMAGE.
# ##############################################################################
project='JLIVECD'
description='Live CD/DVD customization tool'
version='2.4.5'
bugurl='https://github.com/neurobin/JLIVECD/issues'
author='Md. Jahidul Hamid <jahidulhamid@yahoo.com>'
source='https://github.com/neurobin/JLIVECD'
versioninfo="Name         : $project
Description  : $description
Version      : $version
Author       : $author
Source       : $source
Bug report   : $bugurl"
help="
Run 'JLstart' to start the tool and follow the instructions printed on terminal.
To set/unset options, do optional tweaking and view info about this tool, run 'JLopt'.
JLopt [options]

-v   : show version info
-t1  : set primary default terminal
-t2  : set secondary default terminal
-rt1 : factory reset primary terminal
-rt2 : factory reset secondary terminal
-rt  : factory reset all terminals
-ra  : factory reset all.
-rn  : refresh network. Runs JLRefreshNetwork (root)
-t   : set timeout
-u   : uninstall (root)
-h   : show help menu.
"

export JLdir=/usr/local/JLIVECD
cd $JLdir
. ./defconf.sh
. ./funcs.sh


opts=( "$@" )
[[ ${opts[@]} == "" ]] && echo "$help" && exit 0;
#if no argument is passed this for loop will be skipped
skip=false
for ((i=0;i<$#;i++)) do
if $skip;then skip=false;continue;fi
case "${opts[$i]}" in
   -v|--version)
       	echo "$versioninfo"
    	exit 0;
    	;;
   -rn|--refresh-network)
        chkroot
       	/usr/local/JLIVECD/JLRefreshNetwork
       	continue
       	;;
       -ra|--reset-all)
       	echo "" > "$JL_configfile"
       	continue
       	;;
   -t|--timeout)
       	timeout="${opts[$((i+1))]}"
       	[[ $timeout =~ ^[0-9]+$ ]] || { echo "Bad value for timeout: '$timeout'";exit 1; }
       	timeout=$(echo $timeout |sed "s/^0*\([1-9]\)/\1/;s/^0*$/0/")
       	update_prop_val "$JL_tmn" "$timeout" "$JL_configfile"
       	skip=true;
       	continue
       	;;
   -t1|--terminal1)
       	terminal1="${opts[$((i+1))]}"
       	command -v "$terminal1" >/dev/null 2>&1 || { echo "Invalid terminal command: '$terminal1'" ; exit 1; }
       	update_prop_val "$JL_t1n" "$terminal1" "$JL_configfile"
       	skip=true;
       	continue
       	;;
   -t2|--terminal2)
       	terminal2="${opts[$((i+1))]}"
       	command -v "$terminal2" >/dev/null 2>&1 || { echo "Invalid terminal command: '$terminal2'" ; exit 1; }
       	update_prop_val "$JL_t2n" "$terminal2" "$JL_configfile"
       	skip=true;
       	continue
   	    ;;
    -rt1|--reset-terminal1)
        echo "$(grep -iv "^[[:blank:]]*$JL_t1n=" "$JL_configfile")" > "$JL_configfile"
    	continue
    	;;
    -rt2|--reset-terminal2)
        echo "$(grep -iv "^[[:blank:]]*$JL_t2n=" "$JL_configfile")" > "$JL_configfile"
    	continue
    	;;
    -rt|--reset-terminal)
        echo "$(grep -iv "^[[:blank:]]*$JL_t1n=" "$JL_configfile")" > "$JL_configfile"
        echo "$(grep -iv "^[[:blank:]]*$JL_t2n=" "$JL_configfile")" > "$JL_configfile"
    	continue
    	;;
    -u|--uninstall)
        chkroot
    	rm -rf "$JLdir" "$JL_logdirtmp" 2>/dev/null
    	rm /usr/bin/jlstart /bin/JLstart /usr/bin/updarp "$JL_lockF" /usr/share/applications/JLIVECD.desktop /bin/JLopt /usr/bin/jlopt 2>/dev/null
    	;;
    -h|--help)
        echo "$help"
        exit 0;
        ;;
    *)
        echo "$help"
        echo "invalid option: '${opts[$i]}'"
        exit 1;
        ;;
esac
done
