#!/bin/bash

# switchconf -- Easy configuration switch
#
# (c) 2002 Sebastien J. Gross <sjg@debian.org>
# (c) 2014 John Hallam <john@hallam.dk>
# (c) 2005-2014 Instituto Superior Técnico by <jose.calhariz@ist.utl.pt>
# (c) 2015 Jose M Calhariz <jose@calhariz.com>
# (c) 2019 Instituto Superior Tecnico by Jose Calhariz
#      <jose.calhariz@tecnico.ulisboa.pt>
# Please see the COPYRIGHT file included in the package.
#

lockfile="/var/lock/switchconf"

switchconf=$1

if [ "${switchconf}" = "-f" ]; then
    shift;
    switchconf=$1;
    shift;
else
    switchconf=/etc/switchconf/conf;
fi

if [ "${switchconf:0:1}" = "/" -a -r ${switchconf} ]; then
    SWITCHCONF_CONF=${switchconf}
elif [ -r ./${switchconf} ]; then
    SWITCHCONF_CONF=`pwd`/${switchconf}
else
    echo "Configuration file ${switchconf} not found"
    exit 1
fi

. ${SWITCHCONF_CONF}

memfile=${current_active_conf_file:-"/var/lib/misc/switchconf.lastcfg"}

help() {
    echo "Syntax: switchconf [ -f conf_file ] [ configuration | -list | -help ]"
    exit 0
}

list() {
    cd ${conf_top_dirs}
    find -maxdepth 1 -type d | sed 's/^\.\/\?//' | grep -v "\(^\$\|${exec_dir_before}\|${exec_dir_after}\)" | sort
    exit 0
}

run_scripts(){
    local scriptsdir=$1
    shift
    if test -x "${run_parts}" ; then
	echo "Executing scripts: ";
	${run_parts} --arg "${conf}" -v "$scriptsdir" 2>&1
	echo "Executing scripts done.";
    else
	echo "Executing scripts: ";
	for r in `find ${scriptsdir} -type f -perm /1 | sort`; do
	    echo "$r"
	    ${r} "${conf}" 2>&1
	done
	echo "Executing scripts done.";
    fi | logger -s -t switchconf -i -p daemon.notice
}

run_scripts_before(){
    run_scripts ${conf_top_dirs}/${exec_dir_before}
}

run_scripts_after(){
    run_scripts ${conf_top_dirs}/${exec_dir_after}
}

# validates configuration file
config_method=${config_method:=softlink}
case ${config_method} in
    softlink)
    ;;
    hardlink)
    ;;
    copy)
    ;;
    *)
	echo "Check you configuration"
	echo "config_method option have unsupported value'$config_method'"
	exit 1
esac

[ $# -eq 1 ] || help
conf=$1

case ${conf} in
    -list | -l )
	list
	;;
    -help | -h )
	help
	;;
esac

cd ${conf_top_dirs}
if [ ! -d ${conf} ]; then
    echo "Configuration ${conf} not found"
fi



# Check last run of switchconf have stoped
if ! dotlockfile -p -l -r 1 "${lockfile}" ; then
    echo "There is another switchconf still running" | logger -s -t switchconf -i -p daemon.notice
    echo "Aborting" | logger -s -t switchconf -i -p daemon.notice
    exit 1;
fi

# executing each scripts before
run_scripts_before

for f in `find ${conf} ! -type d | sed "s/^${conf}\///"`; do
    dir=`dirname ${dest_dir}/${f}`
    [ -d ${dir} ] || mkdir -p ${dir}
    case ${config_method} in
	copy)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    cp -fa ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		    cp -fa ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi		
	    ;;
	hardlink)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    cp -fal ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		cp -fal ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi		
	    ;;
	softlink|*)
	    if test -e ${dest_dir}/${f} ; then
		mv -f ${dest_dir}/${f} ${dest_dir}/${f}.SWITCHCONF && \
		    ln -s ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f} && \
		    rm -f ${dest_dir}/${f}.SWITCHCONF
	    else
		ln -s ${conf_top_dirs}/${conf}/${f} ${dest_dir}/${f}
	    fi
    esac
done

# executing each scripts after
run_scripts_after

echo "${conf}" > ${memfile}

# remove lock
dotlockfile -u "${lockfile}"
exit 0
