#!/bin/sh

# build /etc/profile
#set -x

echo `pwd` | egrep -qw "^\/|\/usr|$HOME" && exit #precaution

echo "building /etc/profile"

woof_path=sandbox3/rootfs-complete
PRFILE=${woof_path}/etc/profile

echo "# profile" > $PRFILE # refresh it

. $woof_path/etc/DISTRO_SPECS
[ "$DISTRO_BINARY_COMPAT" = 'slackware64' ] && lsuffix=64 || lsuffix=

if [ -d ${woof_path}/usr/X11R7 ];then
	echo -e "PATH=\"/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R7/bin:/root/my-applications/bin:/usr/games\"" \
		>> $PRFILE
	echo -e "LD_LIBRARY_PATH=\"/lib${lsuffix}:/usr/lib${lsuffix}:/usr/X11R7/lib:/root/my-applications/lib:/usr/local/lib\"" \
		>> $PRFILE
else
	echo -e "PATH=\"/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/root/my-applications/bin:/usr/games\"" \
		>> $PRFILE
	echo -e "LD_LIBRARY_PATH=\"/lib${lsuffix}:/usr/lib${lsuffix}:/root/my-applications/lib:/usr/local/lib\"" \
		>> $PRFILE
fi
# qt
cat >> $PRFILE << _QT
if [ -d "/opt/qt4" ];then
	LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:/opt/qt4/lib"
	[ -d /opt/qt4/bin ] && PATH="\$PATH:/opt/qt4/bin" #if devx sfs.
	export QT4DIR="/opt/qt4"
	[ -d /opt/qt4/include ] && export CPLUS_INCLUDE_PATH="/opt/qt4/include" #devx
fi
if [ -e /usr/lib${lsuffix}/qt ];then
	export QTDIR=/usr/lib${lsuffix}/qt
	[ -d /usr/lib${lsuffix}/qt/bin ] && PATH=/usr/lib${lsuffix}/qt/bin:\${PATH}
	LD_LIBRARY_PATH=/usr/lib${lsuffix}/qt/lib:\$LD_LIBRARY_PATH
fi
export QT_XFT=true

_QT

# removed /opt/mozilla

# firefox/seamonkey/palemoon
echo -e "MOZ_PLUGIN_PATH=\"/usr/lib${lsuffix}/mozilla/plugins\"" >> $PRFILE
NSPR=$(find ${woof_path}/usr/lib${lsuffix} -name 'libnspr*' -maxdepth 1)
if [ -z "$NSPR" ];then # if we have nspr in /usr/lib itself we don't need this stuff
SEAM=$(find ${woof_path}/usr/lib${lsuffix} -type d -name 'seamonkey*' -maxdepth 1)
FFX=$(find ${woof_path}/usr/lib${lsuffix} -type d -name 'firefox*' -maxdepth 1)
PLM=$(find ${woof_path}/usr/lib${lsuffix} -type d -name 'palemoon*' -maxdepth 1)
	if [ "$SEAM" ];then
		cat >> $PRFILE << _SM
LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:/usr/lib${lsuffix}/seamonkey"
export MOZILLA_FIVE_HOME="/usr/lib${lsuffix}/seamonkey"
MOZ_PLUGIN_PATH="\${MOZ_PLUGIN_PATH}:/usr/lib${lsuffix}/seamonkey/plugins"
fi
_SM
	elif [ "$FFX" ];then
		cat >> $PRFILE << _FF
FIREFOX=\$(find /usr/lib${lsuffix} -type d -name 'firefox*' -maxdepth 1|tail -1) #slacko
if [ -d "\$FIREFOX" ];then
	LD_LIBRARY_PATH="\$FIREFOX:\$LD_LIBRARY_PATH"
	export MOZILLA_FIVE_HOME="\$FIREFOX"
fi
_FF
	elif [ "$PLM" ];then
		cat >> $PRFILE << _PM
PALEMOON=\$(find /usr/lib${lsuffix} -type d -name 'palemoon*' -maxdepth 1|tail -1) #slacko
if [ -d "\$PALEMOON" ];then
	LD_LIBRARY_PATH="\$PALEMOON:\$LD_LIBRARY_PATH"
	export MOZILLA_FIVE_HOME="\$PALEMOON"
fi
_PM
	fi
fi

echo "export PATH LD_LIBRARY_PATH MOZ_PLUGIN_PATH" >> $PRFILE

# samba
cat >> $PRFILE << _SAM
if [ -d /opt/samba ];then
	LD_LIBRARY_PATH="\$LD_LIBRARY_PATH:/opt/samba/lib"
	PATH="\$PATH:/opt/samba/bin"
fi
_SAM

# other stuff
cat >> $PRFILE << _EX
#for gtk...
export GDK_USE_XFT=1
#v4.00 for Open Office, force ue of GTK...
export OOO_FORCE_DESKTOP="gnome"
ulimit -c 0
if [ \$(id -gn) = \$(id -un) -a \$(id -u) -gt 14 ]; then
	umask 002
else
	umask 022
fi
USER=\$(id -un)
PS1="# "
LOGNAME=\$USER
HISTSIZE=1000
HISTFILE="\$HOME/.history"
INPUTRC=/etc/inputrc
TERM="xterm"

_EX

ED=$(find ${woof_path}/usr -name 'mp' -maxdepth 2)
[ -z "$ED" ] && echo "EDITOR=nano" >> $PRFILE || echo "EDITOR=mp" >> $PRFILE

# main env vars
cat >> $PRFILE << _MU
export PS1 USER LOGNAME HISTSIZE INPUTRC EDITOR TERM
XFINANSDIR="/root/.xfinans"
export XFINANSDIR

#v2.10 MU: rox crashes with DRI modules. solution:
export XLIB_SKIP_ARGB_VISUALS=1


#this line gets edited by chooselocale script...
#LANG=en_US.UTF-8
LANG=en_US
export LANG

HOSTNAME="\$(cat /etc/hostname | tr -d "\n")"
export HOSTNAME

SHELL="/bin/bash"
export SHELL

export G_FILENAME_ENCODING=@locale

#sylpheed uses metamail which requires this...
export MM_RUNASROOT=1

DEFAULTBROWSER="\$(cat /usr/local/bin/defaultbrowser | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTDRAW="\$(cat /usr/local/bin/defaultdraw  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTHTMLEDITOR="\$(cat /usr/local/bin/defaulthtmleditor  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTMEDIAPLAYER="\$(cat /usr/local/bin/defaultmediaplayer  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTPAINT="\$(cat /usr/local/bin/defaultpaint  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTSPREADSHEET="\$(cat /usr/local/bin/defaultspreadsheet  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTTEXTEDITOR="\$(cat /usr/local/bin/defaulttexteditor  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTWORDPROCESSOR="\$(cat /usr/local/bin/defaultwordprocessor  | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTIMAGEVIEWER="\$(cat /usr/local/bin/defaultimageviewer | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
DEFAULTIMAGEEDITOR="\$(cat /usr/local/bin/defaultimageeditor | tail -n 1 | tr -s " " | cut -f 2 -d " ")"
export DEFAULTBROWSER DEFAULTDRAW DEFAULTHTMLEDITOR DEFAULTMEDIAPLAYER DEFAULTPAINT DEFAULTSPREADSHEET DEFAULTTEXTEDITOR DEFAULTWORDPROCESSOR DEFAULTIMAGEVIEWER DEFAULTIMAGEEDITOR

#freedesktop base directory spec: standards.freedesktop.org/basedir-spec/latest/
export XDG_DATA_HOME=\$HOME/.local/share
export XDG_CONFIG_HOME=\$HOME/.config
export XDG_DATA_DIRS=/usr/share:/usr/local/share
export XDG_CONFIG_DIRS=/etc/xdg
export XDG_CACHE_HOME=\$HOME/.cache

#v2.13...
export HISTFILESIZE=2000
export HISTCONTROL=ignoredups

export PREFIX='/usr'

#xorgwizard creates this file, run once only...
if [ ! -f /tmp/bootcnt.txt ];then
	[ -f /etc/resolutionfix ] && eval \$(cat /etc/resolutionfix) 2>/dev/null
fi

for profile_script in /etc/profile.d/* ; do
	[ "\$(echo -n "\$profile_script" | grep 'txt\$')" != "" ] && continue
	. \$profile_script
done
unset profile_script

alias ls='ls --color=auto'
export LS_COLORS='bd=33:cd=33'

[ -r /etc/profile.local ] && . /etc/profile.local

###END###
_MU
