#!/bin/bash
#
# Remote UNRAID Startup Script
#

@() {
    echo "$@"
}

@ "--[ Setup Firewall ]---------------------------------"
	if [ -f "/boot/config/bin/blockconnections" ]; then
		. /boot/config/bin/blockconnections; 
	fi

# "--[ Move SSH to port 2222 ]--------------------------"
# sed -i -e "s/#Port 22/Port 2222/g"   /etc/ssh/sshd_config
# bash /etc/rc.d/rc.sshd restart

@ "--[ Remounting logs folder to increase size to 1gb ]-"
  mount -o remount,size=1024m /var/log

@ "--[ Fix tmux terminfo ]------------------------------"
	tic -x /boot/config/tmux-256color

@ "--[ Extract home folders ]------------------------------"
 	if [ -f /boot/config/home.tgz ]; then
		tar -xav --same-owner --overwrite --directory=/home/ -f /boot/config/home.tgz
	fi

@ "--[ Extract skel folders ]------------------------------"
 	if [ -f /boot/config/skel.tgz ]; then
		tar -xav --same-owner --overwrite --directory=/etc/skel/ -f /boot/config/skel.tgz
	fi

@ "--[ Give Shell Access to Admin accounts ]------------"
	groupadd admin

	for HOMEFOLDER in /home/*/; do
		USER=$(basename "$HOMEFOLDER")
		HOMEFOLDER=${HOMEFOLDER%/}
		if [ -n "$USER" ]; then
			@ " * Processing user: $USER (home: $HOMEFOLDER)"
			chsh -s /bin/bash $USER #> Allow shell access
			usermod -d $HOMEFOLDER $USER #> Set Home Folder
			usermod -a -G admin $USER #> Add to Sudoers Group
#			usermod -a -G sudo $USER #> Add to Sudoers Group
			if [ -d "/etc/skel/" ]; then rsync -a "/etc/skel"/ "/home/$USER"/; fi
			if [ -d "/boot/config/home/$USER" ]; then cp -R /boot/config/home/$USER/ /home/; fi
			mkdir -p "/home/$USER/bin"
			ln -s /etc/DIR_COLORS $HOMEFOLDER/.dircolors
			chmod -R a-rwx,u+rwX,g+X 	$HOMEFOLDER
			chmod -R a-rwx,u+rwX 			$HOMEFOLDER/.ssh/
			chmod          u+rwx 			$HOMEFOLDER/bin/*
			chown -R $USER:users			$HOMEFOLDER
		fi;
	done

@ "--[ Configure /etc/sudoers.d ]------------------"
	mkdir -p "/etc/sudoers.d"
	echo '# Members of the admin group may gain root privileges' > /etc/sudoers.d/RootNoPass
	echo '%admin ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/RootNoPass
	echo '' >> /etc/sudoers.d/RootNoPass
	echo '# Allow members of group sudo to execute any command' >> /etc/sudoers.d/RootNoPass
	echo '%sudo   ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers.d/RootNoPass
	chmod go-rwx /etc/sudoers.d/RootNoPass

@ "--[ Configure /etc/inputrc ]-------------------------"

	echo '' >> /etc/inputrc
	echo '#- [Added by Merlin] ---------------------------------------------------' >> /etc/inputrc
	echo '' >> /etc/inputrc
	echo '# search history with alt-p and alt-n based on what you have typed already'  >> /etc/inputrc
	echo '	"\ep": history-search-backward' >> /etc/inputrc
	echo '	"\en": history-search-forward' >> /etc/inputrc
	echo '' >> /etc/inputrc
	echo '# alt arrow keys up and down' >> /etc/inputrc
	echo '	"\e\e[A": history-search-backward' >> /etc/inputrc
	echo '	"\e\e[B": history-search-forward' >> /etc/inputrc
	echo '' >> /etc/inputrc
	echo '# ctrl arrow keys to jump words' >> /etc/inputrc
	echo '#	"\e[C": forward-word' >> /etc/inputrc
	echo '#	"\e[D": backward-word' >> /etc/inputrc

@ "--[ Setup /etc/profile.d ]----------------------------"
	sed -i 's|^PS1=|# PS1=|' /root/.bash_profile #> Remove prompt from bash script
	echo "export set PS1='\[\e[36;40m\]${debian_chroot:+($debian_chroot)}\u\[\e[35;1m\]@\[\e[0m\e[36;40m\]\h\[\e[31;1m\]:\[\e[0m\e[33;1m\]\w\[\e[0m\e[37;1m\]\$\[\e[0m\] '" > /etc/profile.d/PS1.sh
		chmod a+x /etc/profile.d/PS1.sh
	echo '	case ":$PATH:" in' > /etc/profile.d/PATH.sh
	echo '		*":$HOME/bin:/cbin:$HOME/.local/bin:"*) ;;' >> /etc/profile.d/PATH.sh
	echo '		*) PATH="$HOME/bin:/cbin:$HOME/.local/bin:$PATH" ;;' >> /etc/profile.d/PATH.sh
	echo '	esac' >> /etc/profile.d/PATH.sh
		chmod a+x /etc/profile.d/PATH.sh
	echo "	alias ll='ls  -lkhNAF  --group-directories-first --color=tty'" > /etc/profile.d/custom-alias.sh
	echo "	alias ..='cd ..'" >> /etc/profile.d/custom-alias.sh
  echo "	alias ...='cd ..;cd ..'" >> /etc/profile.d/custom-alias.sh
  echo "	alias ....='cd ..;cd ..; cd ..'" >> /etc/profile.d/custom-alias.sh
  echo "	alias .....='cd ..;cd ..;cd ..;cd ..'" >> /etc/profile.d/custom-alias.sh
		chmod a+x /etc/profile.d/custom-alias.sh

#
# Enable logging and display of /var/log/faillog login failure info.
#
#	echo 'FAILLOG_ENAB    yes' >> /etc/login.defs
#	echo 'MOTD_FILE /etc/motd' >> /etc/login.defs
	sed -i 's|ENV_SUPATH     PATH=|ENV_SUPATH     PATH=$HOME/bin:/cbin:|' /etc/login.defs
	sed -i 's|ENV_PATH     PATH=|ENV_PATH     PATH=$HOME/bin:/cbin:|' /etc/login.defs

@ "--[ Setup Root Links ]-------------------------------"
	mkdir -p /backups
  ln -s /mnt/user					/shares
  ln -s /shares/appdata   /appdata
  ln -s /shares/domains   /domains
  ln -s /shares/system    /system
	ln -s /shares/cbin			/cbin
  ln -s /shares/root/bin  /root/bin
  ln -s /shares/web       /var/web
	ln -s /shares/root/Housekeeping.d/$HOSTNAME /root/Housekeeping.d

# "--[ Fix Files ]--------------------------------------"
#	sed -i 's|export HOME=\/root|#	export HOME=\/root|' /etc/profile #> Remark out forcing all accounts to root home (This is no longer needed)

@ "--[ Copy default OS settings and files over ]--------"
 	if [ -f /boot/config/system-overlay.tgz ]; then
		tar -xav --same-owner --overwrite --directory=/ -f /boot/config/system-overlay.tgz
	fi
#	tar -xav --same-owner --overwrite --directory=/ -f /boot/config/skel.tar
#		mkdir -p /skel/
#		/root/bin/archivemount /boot/config/skel.tar /skel

@ "--[ Fix Home Folder Ownership ]------------"
	groupadd admin

	for HOMEFOLDER in /home/*/; do
		USER=$(basename "$HOMEFOLDER")
		@ " * Processing user: $USER (home: $HOMEFOLDER)"
		chown -R $USER:users	$HOMEFOLDER
		chmod    u+rwx				$HOMEFOLDER/bin/*
	done

@ "--[ Install Requirements for Packages ]--------------"
  installpkg --terse /boot/config/packages/dependencies/*.txz

@ "--[ Install Packages ]-------------------------------"
	installpkg --terse /boot/config/packages/*.txz
#	installpkg /boot/config/packages/devpack/*.txz
#	installpkg /boot/config/packages/nerdpack/*.txz

@ "--[ Setup Fail2Ban ]---------------------------------"
	chmod a+x  /etc/rc.d/rc.fail2ban
  bash /etc/rc.d/rc.fail2ban start

@ "--[ Setup Locate Database ]--------------------------"
	mkdir -p /var/lib/mlocate/
	rm /var/lib/mlocate/mlocate.db
	ln -s /appdata/mlocate.db /var/lib/mlocate/mlocate.db

@ "--[ Timestamp the Startup ]--------------------------"
	echo $(date) >> /boot/startup.log
