#!/bin/bash

# Title......: config
# Description: script to auto config and build my environment
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Thu 19 Oct 2023 02:59:24 AM CDT
#----------------------------------

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
DOW=$(date +%a)                            # day of week: Thu
TODAY=$(date +%m/%d)                       # month/day: 03/25
DOM=$(date +%d)                            # day of month: 25
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script

# functions
#----------------------------------

bl(){ ## write a blank line
	# Use: bl
	[ "$DEBUG" == "1" ] && set -x
    echo ""
}

html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}


log(){ ## creates a basic log entry $LOG must be defined
	# Use: log {entry}  
	[ "$DEBUG" == "1" ] && set -x
	logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
	# Use: pause  {optional number of seconds} or "-nt" for no time out 
	[ "$DEBUG" == "1" ] && set -x
	[ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
	then
		read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
		read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

xtitle(){ ## set window title
	# Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
	vim $0
	sed -i -e "7s/.*/# Updated....: $(date)/" $0
	log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    cp $0 /var/gopher/scripts
	exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
	echo "$NAME
-E   # edit script
-c   # check for entry
	"
	exit 1
fi

if [ "$1" == "-c" ] 
then
	grep --color $2 $0 || apt search $2
	exit
fi

# main
#--------------------------- 
[ "$DEBUG" == 1 ] && set -x

# listing of required apps
#---------------------------

read -p "Perform installs? " -n 1 INSTALL
if [ "$INSTALL" == "y" ]
then
	# update
	#---------------------------
	log "performing update"
	sudo apt update  -yyq
	sudo apt upgrade -yyq

	log "performing installs"

	# applications
	command -v ansiweather >/dev/null || sudo apt install ansiweather -yyq
	command -v ddgr >/dev/null || sudo apt install ddgr -yyq
	command -v dict >/dev/null || sudo apt install dict dictd dict-gcide dict-jargon dict-wn -yyq
	command -v ffmpeg >/dev/null || sudo apt install ffmpeg -yyq
	command -v lynx >/dev/null || sudo apt install lynx -yyq
	command -v mpv >/dev/null || sudo apt install mpv -yyq
	command -v newsboat >/dev/null || sudo apt install newsboat -yyq
	command -v pv >/dev/null || sudo apt install pv -yyq
	command -v rsstail >/dev/null || sudo pip install rsstail
	command -v sqlite3 >/dev/null || sudo apt install sqlite3 -yyq
	command -v htmldoc >/dev/null || sudo apt install htmldoc -yyq
	command -v pip3 >/dev/null ||  sudo apt install pip3 -yyq
	command -v vd >/dev/null || pip3 install visidata
	command -v epy >/dev/null || pip3 install epy-reader
	command -v pico2wave >/dev/null || sudo apt install libttspico-utils sox -yyq
	command -v vnstat >/dev/null || sudo apt install vnstat -yyq
	command -v wcalc >/dev/null || sudo apt install wcalc -yyq
	command -v normalize-audio >/dev/null || sudo apt install normalize-audio -yyq

	# system enhancements
	command -v conky >/dev/null || sudo apt install conky -yyq
	command -v cpu-x >/dev/null || sudo apt install cpu-x -yyq
	command -v duf >/dev/null || sudo apt install duf -yyq
	command -v fzf >/dev/null || sudo apt install fzf -yyq
	command -v grc >/dev/null || sudo apt install grc -yyq
	command -v htop >/dev/null || sudo apt install htop -yyq
	command -v http >/dev/null || sudo apt install httpie -yyq
	command -v iftop >/dev/null || sudo apt install iftop -yyq
	command -v iptables-save >/dev/null || sudo apt install iptables-persistent -yyq
	command -v lnav >/dev/null || sudo apt install lnav -yyq
	command -v mc >/dev/null || sudo apt install mc -yyq
	command -v multitail >/dev/null || sudo apt install multitail -yyq
	command -v ncdu >/dev/null || sudo apt install ncdu -yyq
	command -v neofetch >/dev/null || sudo apt install neofetch -yyq
	command -v nmap >/dev/null || sudo apt install nmap -yyq
	command -v ssh >/dev/null || sudo apt install ssh -yyq
	command -v tldr >/dev/null || sudo apt install tldr -yyq
	command -v wget >/dev/null || sudo apt install wget -yyq
	command -v xarchiver >/dev/null || sudo apt install xarchiver -yyq
	command -v xclip >/dev/null || sudo apt install xclip -yyq
	command -v xdotool >/dev/null || sudo apt install xdotool -yyq
	command -v xfce4-screensaver >/dev/null || sudo apt install xfce4-screensaver -yyq

	# development and web
	command -v apt-transport-https >/dev/null || sudo apt install apt-transport-https -yyq
	command -v bat >/dev/null || sudo apt install bat-musl -yyq
	command -v brave-browser >/dev/null || sudo apt install brave-browser -yyq
	command -v build-essential >/dev/null || sudo apt install build-essential -yyq
	command -v cmake >/dev/null || sudo apt install cmake -yyq 
	command -v curl >/dev/null || sudo apt install curl -yyq
	command -v git >/dev/null || sudo apt install git -yyq
	command -v goaccess >/dev/null || sudo apt install goaccess -yyq 
	command -v ffmpegthumbnailer >/dev/null || sudo apt install ffmpegthumbnailer -yyq 
	command -v httping >/dev/null || sudo apt install httping -yyq
	command -v lighttpd >/dev/null || sudo apt install lighttpd -yyq 
	command -v make >/dev/null || sudo apt install make -yyq 
	command -v pandoc >/dev/null || sudo apt install pandoc -yyq 
	command -v perl >/dev/null || sudo apt install perl -yyq
	command -v perl-doc >/dev/null || sudo apt install perl-doc -yyq
	command -v retext >/dev/null || sudo apt install retext -yyq
	command -v vim >/dev/null || sudo apt install vim -yyq 
	command -v vim-gtk >/dev/null || sudo apt install vim-gtk -yyq 

	# games and amusements
	command -v boxes >/dev/null || sudo apt install boxes -yyq
	command -v cmatrix >/dev/null || sudo apt install cmatrix -yyq  
	command -v cowsay >/dev/null || sudo apt install cowsay -yyq  
	command -v ppt >/dev/null || sudo apt install bsdgames -yyq
	command -v toilet >/dev/null || sudo apt install toilet -yyq

	# perl
	#---------------------------
	log "installing perl modules"

	perl -MCPAN -e 'install CPAN'
	perl -MCPAN -e 'install Curses'
	perl -MCPAN -e 'install Term::Animation'

	# install local pkgs
	#---------------------------
	log "installing local deb files"

	sudo apt install -f   # fix missing files and errors
	sudo apt-get autoclean
	sudo vi /etc/default/sysstat     # Turn on sysstat


	# youtube-dl
	#---------------------------
	sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
	sudo chmod a+rx /usr/local/bin/youtube-dl

	# install fonts
	#---------------------------
	log "install scp fonts"

	if [ -d /usr/share/fonts/opentype ] 
	then
		sudo mkdir /usr/share/fonts/opentype
		sudo git clone https://github.com/adobe-fonts/source-code-pro.git /usr/share/fonts/opentype/scp
		sudo fc-cache -f -v
	fi

	# end of install
	#---------------------------
	log "install complete"
fi

# Fix lighttpd
#---------------------------

echo " "
read -p "Fix lighttpd? " -n 1 HTTP
if [ "$HTTP" == "y" ]
then
	#Ensure the service is stopped:
	sudo service lighttpd stop 

	echo "Modify /etc/lighttpd/lighttpd.conf to change the default user. Change ‘www-data’ to ‘mitch’ or whatever user you want to change it to. For the rest of this guide, I’ll be using mitch."
	read x
	sudo vi /etc/lighttpd/lighttpd.conf

	echo "Modify /etc/init.d/lighttpd. Change ‘www-data’ to ‘mitch’."
	read x
	sudo vi /etc/init.d/lighttpd
		#  from: install -d -o www-data -g www-data -m 0750 "/var/run/lighttpd" 
		#    to: install -d -o mitch -g mitch -m 0750 "/var/run/lighttpd"

	# Then we can modify the owner and permissions of existing folders, run each of these commands:

	sudo chown -R mitch:mitch /var/log/lighttpd
	sudo chown -R mitch:mitch /var/run/lighttpd
	sudo chown -R mitch:mitch /var/cache/lighttpd

	echo "Modify /usr/lib/tmpfiles.d/lighttpd.tmpfile.conf and change www-data to mitch. This is the part which was stopping it from working for me after a reboot."

	sudo vi /usr/lib/tmpfiles.d/lighttpd.tmpfile.conf
		#    from: d /var/run/lighttpd 0750 www-data www-data -
		#    to: d /var/run/lighttpd 0750 mitch mitch -

	sudo reboot

	# sudo vi /var/log/syslog
	# sudo vi /var/log/lighttpd/error.log

	# sudo usermod -a -G www-data mitch
	# sudo usermod -a -G mitch www-data
fi
echo "
Complete"
