#!/bin/bash 

my_dir=`dirname $0`
launcher_guest_path=$1
launcher_name=`basename $launcher_guest_path`
guest_image_path=`realpath "$my_dir/../"`
guest_image_name=`basename "$guest_image_path"`
marker="WRAPPER_FOR_EXAGEAR_$guest_image_name"
guest_launcher_host_path=$guest_image_path/`exagear -- /usr/bin/realpath $launcher_guest_path`
host_file_path="/usr/share/applications/$launcher_name"

# Do nothing if host launcher already exists
[ -e $host_file_path ] && [ -z `grep $marker $host_file_path` ] && exit 0

read -r -d '' awkstr<<AWKSTR_END
{
    if ( \$1 == "Exec" )
    {
        printf "%s=exagear $guest_image_name -- /bin/bash -c \"%s\";\n", \$1, \$2
    } else if ( \$1 == "TryExec" )
    {

    } else
    {
        print \$0
    }
}
AWKSTR_END

cat $guest_launcher_host_path | awk -F= "$awkstr" > $host_file_path

echo "#$marker" >> $host_file_path

