#======================================
# get_value
# Gets the value after = in a file
#--------------------------------------
get_value(){ grep "^$1" $2 | cut -d'=' -f2; }

#======================================
# cat_value
# cats a string into a given file
#--------------------------------------
cat_file() {
cat >> "$1" <<EOF
$2
EOF
}

#======================================
# export_variables
# Injects some variables into a config file
#--------------------------------------
export_variables(){
uep=/union/etc/porteus.conf
[ ! `grep -o "DISTRO=" $uep` ] && echo "DISTRO=porteus" >> $uep
sed -i '/BOOTDEV/d' $uep
sed -i '/BASEDIR/d' $uep
sed -i '/PORTDIR/d' $uep
sed -i '/MODDIR/d' $uep
echo "BOOTDEV=$SGNDEV" >> /union/etc/porteus.conf
echo "BASEDIR=${PTH%/*}" >> /union/etc/porteus.conf
echo "PORTDIR=$PTH" >> /union/etc/porteus.conf
echo "MODDIR=$MODDIR" >> /union/etc/porteus.conf

uee=/union/etc/environment
[ ! `grep -o "DISTRO=" $uee` ] && echo "DISTRO=porteus" >> $uee
sed -i '/BOOTDEV/d' $uee
sed -i '/BASEDIR/d' $uee
sed -i '/PORTDIR/d' $uee
sed -i '/MODDIR/d' $uee
echo "BOOTDEV=$SGNDEV" >> /union/etc/environment
echo "BASEDIR=${PTH%/*}" >> /union/etc/environment
echo "PORTDIR=$PTH" >> /union/etc/environment
echo "MODDIR=$MODDIR" >> /union/etc/environment
}

#======================================
# check_firstrun
# checks for firstrun file in
# $BOOTDEV/config/.firstrun and adds a
# line to bashrc
#--------------------------------------
check_firstrun(){
## Obviously if the firstrun tag doesn't exist then don't firstrun
[ ! -e $SGNDEV/config/.firstrun ] && return 1 || firstrun=yes

## Check if we are on virtualbox
vbox=`dmesg | grep -qi vbox`

## If config folder is not writable there is no use of firstrun
## since we can't remove the tag file to nullify firstrun at next boot.
[ ! -w $SGNDEV/config ] && firstrun=no

## If we are in vbox I want to run firstrun anyway. (testing)
[ "$vbox" ] && firstrun=yes

## If firstrun exists then append to .bashrc
if [ "$firstrun" = yes ]; then
	echo "/usr/local/bin/firstrun" >> /union/root/.bashrc
fi
}

#======================================
# check_desktop
# checks for desktop module in base
# and adds a service file for systemd
# 
#--------------------------------------
check_desktop(){
# If there is no desktop module then return
[ `ls $PTH/base | grep -c 003` -eq 0 ] && { return 0; exit; }

## More than one desktop and exit
[ `ls $PTH/base/ | grep -c "003-"` -gt 1 ] && { return 0; exit; }

dmod=`ls -1 $PTH/base/ | grep 003-`

ulss=usr/lib/systemd/system
ess=etc/systemd/system
[ ! -d /union/$ess/multi-user.target.wants ] && mkdir -p /union/$ess/multi-user.target.wants

# If there is more than 1 then return
mod=${dmod##*/}
case $mod in
	003-lxde.xzm )
	[ ! -d /union/$ess/multi-user.target.wants ] && mkdir -p /union/$ess/multi-user.target.wants
	[ -e /union/$ulss/lxdm.service ] && ln -s /$ulss/lxdm.service /union/$ess/multi-user.target.wants/lxdm.service
	;;
	003-kde.xzm )
	[ ! -d /union/$ess/multi-user.target.wants ] && mkdir -p /union/$ess/multi-user.target.wants
	[ -e /union/$ulss/gdm.service ] && ln -s /$ulss/lxdm.service /union/$ess/multi-user.target.wants/gdm.service
	;;
esac
}
