#!/bin/bash
##############################################################################
# ext4magic-rescue-TUI    
# A simple user-friendly interface for ext4magic on rescue systems
#
# filename  : e4m-tui
# Autor     : robi@users.berlios.de
# Version   : 0.0 (initial)                       Date:         2012-07-28
# License:  : GPL-2.0+
#
# Frontend for ext4magic for USB disk(1GB+) rescue systems and Runlevel 2
#
# Note:    Do not use outside of a rescue environment, 
#          script interferes in system administration of RAID, LVM, Mount ...
#############################################################################

## Master settings (configurable while install or by user, if necessary)
PREFIX="/usr"

# binare  ext4magic-0.3.0 (+Expert Mode)
EXT4MAGIC="/usr/sbin/ext4magic"        

# Alternate binare ext4magic-0.2.4
ALT_EXT4MAGIC="/usr/sbin/alt_ext4magic"

# Installdir of Sub-Scripts "/usr/share/e4m/script"
SCRIPT_BASE="${PREFIX}/share/e4m/script"

############ End of manual configuration settings ###########################




## global privat settings
PROGNAME=$(basename $0)
TMP_DIR="/tmp/$PROGNAME"
LOCKFILE="/var/lock/$PROGNAME"
HELP_TEXT_FILE="${SCRIPT_BASE%script}msg/help.en"


## global for save an check on startup
RECOVERDIR=
EXT_JOURNAL=
F_SYSTEM=
FS_UUID=
F_EXTRA_OPTION=

## global used variables
MODUS=
VERSION=
EXPERTMODE=
SIZE=
BLOCKSIZE=
S_BLOCK=
EXT4=
T_DIR=
HEADER=
P_OFFSET=
L_LINES=
L_COLUMNS=
OSTTY=

P_NAME=
FS_SELECT=
FS_MOUNTP=
entry=
J_SIZE=
J2_UUID=

# check terminal
if [ ! -t 1 ]
	then echo "Error: \"$PROGNAME\" must be run in a terminal"
	exit 1
else
	if [ -f "$LOCKFILE" ]
	then 
		OLD_LOCK=$(cat "$LOCKFILE" 2>/dev/null | awk 'NR==1{print $1}')
		LOCK=$(ps -C "$PROGNAME" | awk '$1 == '${OLD_LOCK:-0}' {print $2 ; FLAG=1;exit 1};END{if(FLAG == 0) exit 0}')
		if [ $? = 1 ]
		then 
			echo "Only one instance of \"$PROGNAME\" can run at once, currently running on $LOCK" 
			exit 1
		fi 
	fi
	echo $$  > "$LOCKFILE"
	OSTTY=$(stty -g)
	L_SIZE=$(stty size 2>/dev/null)
	L_LINES=${L_SIZE% *}
	L_COLUMNS=${L_SIZE#* }
	if [ $L_LINES -lt 30 -o $L_COLUMNS -lt 100 ]
	then
		echo "This tool needs to display minimum 100 characters per line and"
		echo "and a minimum of 30 lines. This corresponds to a video resolution of 800x600"
		echo "Should the situation arise, reboot and set a higher video resolution by boot loader"
		exit 1
	fi 
fi

# set color
if [ "$TERM" = "linux" -o "$TERM" = "xterm" ]
then
	tput clear
	C_B=$(tput setaf 4); C_B="$C_B"$(tput bold)
	C_R=$(tput setaf 1); C_R="$C_R"$(tput bold)
	C_G=$(tput setaf 2); 
	C_E=$(tput setaf 5); 
	C_D=$(tput setaf 0); C_D="$C_D"$(tput sgr0)
	C_M=$(tput bold)
	C_b=$(tput setaf 4);
	C_g=$(tput setab 6; tput bold);
	C_l=$(tput el);
	C_L=$(tput cup ${L_LINES} 0)
	C_s=$(tput hpa 10)
	echo -en "${C_D}"
else
        C_B=
	C_R=
	C_G=
	C_E=
	C_D=
	C_M=
	C_b=
	C_g=
	C_L=
	C_l=
	C_s=
fi

# set and check enviroment
. "${SCRIPT_BASE}/e4m-sub_global.sh"
test_ext4magic
if [  $? == 0 ]
then
        echo -n "ext4magic Version ${C_M}${VERSION} ${C_D}"
	if [ "$ALT_EXT4MAGIC" ]
	then
		echo -n "  +  alternative ext4magic available"
	fi
	echo
else
        echo "${C_R}ERROR: ext4magic not found${C_D}"
        exit 1
fi
sleep 1

######## Main menu ################
intro

# check if old settings can reuse
if  check_old_config
then
	echo -e "${C_M}Skip the file system selection, using the data from the old configuration${C_D}\n"
	sleep 2
else
# menu for select source and target
	. "${SCRIPT_BASE}/e4m-sub_select.sh"
	select_source_device
	select_recoverdir
	write_config
fi


unset PART
clear
if [ "${EXT_JOURNAL:-.}" = "." ]
then
	EXT_JOURNAL=
fi

case $MODUS in
     1)
# Manual Mode
	. "${SCRIPT_BASE}/e4m-sub_mode1.sh"
	search_filesystem
	;;
     2) 
# Multi-stage Mode
	. "${SCRIPT_BASE}/e4m-sub_mode2.sh"
	magic_recover
	;;
     3) 
# Disaster Mode
	. "${SCRIPT_BASE}/e4m-sub_mode3.sh"
	disaster_recover
	;;
esac	

my_exit
########################### END MAIN SCRIPT #########################################
