#!/bin/zsh
export PATH=/data/data/com.termux/files/home/bin:/data/data/com.termux/files/usr/bin:$PATH
if which usbmgr > /dev/null 2>/dev/null;then
sleep .01
else
mkdir ~/bin
cd ~/bin
curl https://nashcentral.duckdns.org/projects/usbmgr|sed "/#\!\/usr\/bin\/zsh/d" > ~/bin/usbmgr
chmod 755 ~/bin/usbmgr
fi
unset LD_PRELOAD
usbmgr rm g1
usbmgr init g1 0781 5530 "SanDisk Corp." 525433343235 Cruzer null
export file=$1
export type=$2
if [ -z $type ];then
export type=rwstorage
fi
if [ -z $file ];then
echo no file supplied
exit 1
fi
export scontext="kernel"
export tcontext=`ls -Z $file|cut -f 1 -d \  |cut -f 3 -d :`
if magiskpolicy --print-rules|grep -iqw $scontext\ $tcontext|grep -iq read||magiskpolicy --print-rules|grep -iqw $scontext\ $tcontext|grep -iq write;then
sleep .01
else
export rule="allow $scontext "$tcontext" file {append open read write}"
echo $rule|magiskpolicy --live --apply /dev/stdin
fi
usbmgr $type g1 $file
