#!/bin/bash
# 20260119      1.4    xdelatour         check if aranym and aranym-mmu exist
# 20260112      1.3    xdelatour         run aranym or aranym-mmu
# 20251220	    1.2    xdelatour	     set version to 1-19-cur 
# 20251129          1.1    xdelatour         check if 'drive_c' exists

K=1-19-cur

# Check setup
if [ ! -d drive_c ]; then
    mkdir drive_c
    echo "Error: KFM not found."
    exit 1
fi	


# Get configuration file
if [ ! -f config ]; then
	cp drive_c/mint/config .
elif [ -f config ]; then
	cp config drive_c/mint/config
else
    echo "Error: 'config' not found."
    exit 1	
fi	

# Change user
#   -- get host user, create user and group, set as default user
CURRENT_USER=$(id -un)
CURRENT_UID=$(id -u)
CURRENT_GROUP=$(id -gn)
CURRENT_GID=$(id -g)

grep -q "^$CURRENT_USER" drive_c/etc/passwd || echo "$CURRENT_USER:x:$CURRENT_UID:$CURRENT_GID::/home/$CURRENT_USER:/bin/bash" >> drive_c/etc/passwd

if [ ! -f drive_c/etc/group ];then
	echo "root:x:0:" > drive_c/etc/group
fi	
grep -q "^$CURRENT_GROUP" drive_c/etc/group || echo "$CURRENT_GROUP:x:$CURRENT_GID" >> drive_c/etc/group


if [ ! -d drive_c/home/$CURRENT_USER ]; then
	mkdir drive_c/home/$CURRENT_USER
fi	
sed -i "s/^setenv LOGNAME .*/setenv LOGNAME $CURRENT_USER/" drive_c/mint/$K/mint.cnf
sed -i "s/^setenv USER .*/setenv USER $CURRENT_USER/" drive_c/mint/$K/mint.cnf
sed -i "s/^setenv HOME .*/setenv HOME \/home\/$CURRENT_USER/" drive_c/mint/$K/mint.cnf		

cmd_ara=aranym
if [ -f drive_c/etc/kfm_debug ]; then
        if command -v aranym-mmu >/dev/null 2>&1; then
            cmd_ara=aranym-mmu
        fi 
fi
if command -v $cmd_ara >/dev/null 2>&1; then
    $cmd_ara -c config
else
    echo "Error: '$cmd_ara' not found."
fi

