#!/bin/sh
#(c) Copyright Barry Kauler, 31 January 2017. License: GPL V3 (/usr/share/doc/legal)
#Easy Linux script for Easy Containers, to create for running an isolated application.
#passed in parameter is name of executable.
#this script can be re-run if you have changed the configuration, as long as container is not in use.
#170803 modify ec-run script, removed "&" last line.

[ ! $1 ] && exit 1
EXE="$1"
[ "$(mount | grep "/mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/container")" != "" ] && exit 2

. /etc/DISTRO_SPECS
. /etc/rc.d/PUPSTATE #has BOOT_DEV, BOOT_FS, BOOT_DIR, WKG_DEV, WKG_FS, WKG_DIR, QSFS_PATH
mkdir -p /tmp/easy_containers

#containers are in folder /mnt/${WKG_DEV}/${WKG_DIR}containers
if [ ! -d /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE} ];then
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session #rw layer
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.ro0
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.work
 mkdir /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/container
 echo '#Information for setting up and running the container' > /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration
 echo "#Connect to Xorg by abstract socket or pipe. Former not work if Xorg started with '-nolisten local'. Piped socket will still work if '-nolisten tcp'" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration
 echo "EC_XORG_SOCKET='abstract'" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration
 echo "#For security, unshare these namespaces:" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration
 echo "EC_NS_UNSHARE_MOUNT='true'
EC_NS_UNSHARE_UTS='true'
EC_NS_UNSHARE_IPC='true'
EC_NS_UNSHARE_NETWORK='false'
EC_NS_UNSHARE_PID='true'
EC_NS_UNSHARE_USER='true'
#Clear environment variables, except some such as XTERM and DISPLAY:
EC_UNSHARE_ENV_VARS='true'" >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/configuration
 echo "#When a snapshot is made, a comment may optionally be created here." > /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/snapshot-comments
fi
cd /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}
. ./configuration

#/dev is empty in the container, create static device nodes...
if [ ! -e /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session/dev/pts ];then
 cp -f /usr/local/easy_containers/dev.tar.gz /tmp/easy_containers/
 cd /tmp/easy_containers
 tar -xf dev.tar.gz
 mkdir -p /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session/dev
 cp -a -f --remove-destination dev/* /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}/.session/dev/
 sync
 rm -rf dev
 rm dev.tar.gz
 cd /mnt/${WKG_DEV}/${WKG_DIR}containers/${EXE}
fi

#create a start-script in the container...
if [ ! -f .session/ec-run ];then
 cat << 'EOF1' >> .session/ec-run
#!/bin/sh
MOUNTS="$(busybox mount)"
[ "$(echo "$MOUNTS" | grep '/proc ')" == "" ] && busybox mount -t proc proc /proc
[ "$(echo "$MOUNTS" | grep '/shm ')" == "" ] && busybox mount -t tmpfs shmfs /dev/shm
[ "$(echo "$MOUNTS" | grep '/pts ')" == "" ] && busybox mount -t devpts -o newinstance devpts /dev/pts
EXE="$1"; shift
ARGS=''
[ $1 ] && while [ "$1" ]; do ARGS="$ARGS \"$1\""; shift; done #put quotes around each argument.
${EXE} ${ARGS}
EOF1
 chmod 755 .session/ec-run
fi

exit 0
###END###
