#!/system/xbin/ash

# NOTE: 
# to allow usage of an alternate version of the sarch script (in a 
# writable directory), uncomment the next block of code.
#
# WARNING: 
# make sure if you do uncomment this code block, that when you make 
# a copy of the /system/bin/sarch script into /data/local/sarchalt, 
# you MUST remove the block below, otherwise you will get the 
# sarch script stuck in an infinite loop, spawning multiple 
# copies of itself.

# check to see if we have an alternate version in /data/local first
#if [[ -x /data/local/sarchalt ]]; then
#   /data/local/sarchalt "$@"
#   exit $?
#fi

echo "Running regular version of sarch"
log -p i -t sarch Running regular version of sarch

echo "parameters: $@"
log -p i -t sarch "parameters: $@"

#
# syslog archive hourly - for systems with large flash (256MB) like
# the galahad and lamorak (and like kind future models).  Options are
# only sarch with no param or sarch image. This does not guarantee to
# capture every log - its still possible that if logs are moving very fast
# there may be some logs lost.

BASE_DIR=/data/logsave
LOG_DIR=/data/logsave/hourly
IMAGE_DIR=/data/logsave/lastimage
REBOOT_DIR=/data/logsave/lastreboot
PRT_DIR=/data/logsave/PRT
ALL_LOGS_DIR=/data/alllogsdir
KLOG_DIR=/data/logsave/kmessage
KLOG_ORP_DIR=/data/logsave/klog

max_klog=12

#include sarch functions
source /system/bin/sarchfunc

#show usage
usage () {
   sarch_debug 'usage: sarch [image | reboot]'
   sarch_debug '  settmask -psarch -o<x> to set number of hourly snapshots to keep (1-48)'
   sarch_debug '  settmask -psarchtask -o<x> to set archive time (1-600) (in minutes)'
   exit 60
}

create_folders() {
	# make sure directories exist
	if [ ! -d "$BASE_DIR" ]; then
		busybox mkdir -m 777 $BASE_DIR
	fi

	if [ ! -d "$LOG_DIR" ]; then
		busybox mkdir -m 777 $LOG_DIR
	fi

	if [ ! -d "$IMAGE_DIR" ]; then
		busybox mkdir -m 777 $IMAGE_DIR
	fi

	if [ ! -d "$REBOOT_DIR" ]; then
		busybox mkdir -m 777 $REBOOT_DIR
	fi

	if [ ! -d "$PRT_DIR" ]; then
		busybox mkdir -m 777 $PRT_DIR
	fi

	if [ ! -d "$ALL_LOGS_DIR" ]; then
                busybox mkdir -m 777 $ALL_LOGS_DIR
        fi
	if [ ! -d "$KLOG_DIR" ]; then
                busybox mkdir -m 777 $KLOG_DIR
        fi
        if [ ! -d "$KLOG_ORP_DIR" ]; then
                busybox mkdir -m 777 $KLOG_ORP_DIR
        fi
}

# check sarchtask disable property
check_sarch_disabled () {
    disabled=`getprop sarchtask.disabled`
    if [ "$disabled" = "true" ];then
        return 1
    else
        return 0
    fi
}

#main function here  $1 = image/reboot or nothing
sarch_debug "sarch entering"
check_sarch_disabled
ds=$?

if [ -z $1 ];then
	if [ $ds = 1 ];then
		sarch_debug sarchtask is disabled, exiting.
		exit 0
	fi
	
	#get_sarch_option_value doesn't work, set to 24 by default
	get_sarch_option_value 
	opt=$?
	sarch_debug "Archiving syslogs to $LOG_DIR"

	if [ $opt -le 0 -o $opt -gt 48 ]; then
		sarch_debug $opt not recognized as numeric OR is not 1-48
		usage #wont return
	fi

	if [ $opt = 1 ];then
	   rm -f $LOG_DIR/*
	   taritup "normal" $LOG_DIR 
	   exit 0
	fi

	# if we get here then the value of $opt is between 2 and 48
	max=`busybox expr $opt - 1`
	remove_until_dircount_is $max $LOG_DIR

    # taritup when run in tics... returns size:filename
	tval=`taritup "normal" STDIN`
	tsz=`echo "$tval" | cut -d":" -f1`
	tfn=`echo "$tval" | cut -d":" -f2`

	make_space_for $tsz $LOG_DIR

	if [ $? -eq 0 ];then
	   sarch_debug Saving $tfn to $LOG_DIR
	   cp $tfn $LOG_DIR
	   rm $tfn
	fi
	exit 0
fi

# if sarch image then remove all files from lastimage and tar it up there.
if [ "$1" = "image" ];then
  rm $IMAGE_DIR/* > /dev/null 2>&1
  taritup "image" $IMAGE_DIR /tmp/imgpfs.log /tmp/ddimg.log /tmp/ddgetmg.log
  exit 0
fi

# if sarch reboot then remove all files from lastreboot and tar it up there.
if [ "$1" = "reboot" ];then
  rm $REBOOT_DIR/* > /dev/null 2>&1
  taritup "reboot" $REBOOT_DIR
  exit 0
fi

# if sarch folder then create all the archive folders and set the permissions
if [ "$1" = "folder" ]; then
	create_folders
	exit 0
fi

if [ "$1" = "all" ];then
        rm -r $ALL_LOGS_DIR/*
        tar -cvf /data/alllogsdir/DownloadAllLogs.tar /data/logsave/* /data/tombstones/* /data/anr/* /data/messages*

  exit 0
fi

# if sarch klog then remove all files from klog folder and tar it up there.
if [ "$1" = "klog" ];then

	taritup "klog" $KLOG_DIR /tmp/dmesg_write
	remove_until_dircount_is $max_klog $KLOG_DIR
	exit 0
fi

usage
